Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed build by adding glide spec, add AWS CloudBuild support #36

Merged
merged 9 commits into from
Jan 25, 2017
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
kube-cert-manager
kube-cert-manager.exe
vendor
*~
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ The code itself however, was entirely reimplemented to use xenolf/lego as the ba
* [Certificate Third Party Resources](docs/certificate-third-party-resource.md)
* [Certificate Objects](docs/certificate-objects.md)
* [Challenge Providers](docs/providers.md)
* [Building Container Image with AWS CodeBuild(codebuild/README.md)]
Empty file modified build-container.sh
100644 → 100755
Empty file.
Empty file modified build.sh
100644 → 100755
Empty file.
48 changes: 48 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# kube-cert-manager
# AWS CodeBuild spec to build, containerise, and push to Docker Hub
# Uses standard Docker build container image, installs go and glide
# Aaron Roydhouse <aaron@roydhouse.com>
# https://github.com/whereisaaron/
#

version: 0.1

environment_variables:
plaintext:
APP_NAME: "kube-cert-manager"
PACKAGE: "stable.k8s.psg.io"
DOCKER_REPO: "whereisaaron"
CONTAINER_VERSION: "0.4.0"
DOCKER_HUB_USERNAME: "MISSING - Specify in CodeBuild project or start command"
DOCKER_HUB_PASSWORD: "MISSING - Specify in CodeBuild project or start command"
GO_VERSION: "1.7.4"
GO: "/usr/local/go/bin/go"
WORKDIR: "go"

phases:
install:
commands:
# Install go and glide, copy source into GOPATH
- echo "Path is ${PATH}"
- curl -S https://storage.googleapis.com/golang/go${GO_VERSION}.linux-amd64.tar.gz 2>/dev/null | tar -C /usr/local -xzf -
- $GO version
- export GOPATH="${CODEBUILD_SRC_DIR}/${WORKDIR}"; mkdir -p "${GOPATH}/bin"; mkdir -p "${GOPATH}/src"
- export GOPATH="${CODEBUILD_SRC_DIR}/${WORKDIR}"; export PATH="${PATH}:/usr/local/go/bin:${GOPATH}/bin"; curl -S https://glide.sh/get 2>/dev/null | sh
- export CODE="${CODEBUILD_SRC_DIR}/${WORKDIR}/src/${PACKAGE}"; mkdir -p "${CODE}"; cp -r Dockerfile glide.* k8s *.go -t "${CODE}"
pre_build:
commands:
# Download go dependencies
- export GOPATH="${CODEBUILD_SRC_DIR}/${WORKDIR}"; export PATH="${PATH}:/usr/local/go/bin:${GOPATH}/bin"; cd "${GOPATH}/src/${PACKAGE}"; glide --no-color install
build:
commands:
# Build app, build and tag container
- export GOPATH="${CODEBUILD_SRC_DIR}/${WORKDIR}"; cd "${GOPATH}/src/${PACKAGE}"; GOARCH=amd64 GOOS=linux CGO_ENABLED=0 $GO build -o kube-cert-manager .
- docker build -t ${DOCKER_REPO}/${APP_NAME}:${CONTAINER_VERSION} -t ${DOCKER_REPO}/${APP_NAME}:latest "${CODEBUILD_SRC_DIR}/${WORKDIR}/src/${PACKAGE}"
# Login to Docker Hub and push
- docker login --username="$DOCKER_HUB_USERNAME" --password="$DOCKER_HUB_PASSWORD"
- docker push ${DOCKER_REPO}/${APP_NAME}:${CONTAINER_VERSION}
- docker push ${DOCKER_REPO}/${APP_NAME}:latest
post_build:
commands:
# Post-build commands always run, even if the build fails
57 changes: 57 additions & 0 deletions codebuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Building kube-cert-manager using AWS CodeBuild

You can use AWS CodeBuild to build this project and push the contianer image to Docker hub.
You can use the AWS console or the AWS CLI with the scripts in the `codebuild` folder.

## Console Set-up

Use the AWS CodeBuild console to create a CodeBuild project as follows

- Give the project a name
- Select the GitHub project to build
- Choose build image: 'aws/codebuild/docker:1.12.1'
- Opt to use the 'buildspec.yml' file
- Choose 'No Artifacts'
- Choose (or create) the AWS Service Account to use

## Console Build

Use the AWS CodeBuild console to start the build

1. Choose 'Start Build'
2. If necessary, enter the git branch or commit ID
3. Add or update the following environment variables
```
CONTAINER_VERSION
DOCKER_REPO
DOCKER_HUB_USERNAME
DOCKER_HUB_PASSWORD
```
4. Start the Build
5. Check the build logs afterwards

## CLI Set-up

This requires you have [AWS CLI installed](http://docs.aws.amazon.com/cli/latest/userguide/installing.html),
a CodeBuild Service Account, and have linked CodeBuild to GitHub.

1. Update the GitHub URL and AWS Service Account in the `cloudbuild.json` file
2. Create the project, specifying your profile and the region if not your default
```
aws codebuild create-project --profile default --region=us-east-1 --cli-input-json file://codebuild-project.json
```

## CLI Build

1. Set the following environment variables or customise `build.env` and `source build.env`
```
export PROJECT_NAME="kube-cert-manager"
export PROJECT_REGION="us-east-1"
export SOURCE_VERSION=""
export CONTAINER_VERSION="0.4.0"
export DOCKER_REPO=<your Docker Hub repo name>
export DOCKER_HUB_USERNAME=<your username>
export DOCKER_HUB_PASSWORD=<your password>
```
2. Start the build with `start-codebuild.sh`
3. View the build log with `get-codebuild-log.sh`
12 changes: 12 additions & 0 deletions codebuild/build.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# CodeBuild to Docker Hub build environment
# This is just a sample, customise and load with 'source build.env'
#

export PROJECT_NAME="kube-cert-manager"
export PROJECT_REGION="us-east-1"
export SOURCE_VERSION=""
export CONTAINER_VERSION="0.4.0"
export DOCKER_REPO="whereisaaron"
export DOCKER_HUB_USERNAME=$(get-aws-profile.sh --profile=dockerhub --key)
export DOCKER_HUB_PASSWORD=$(get-aws-profile.sh --profile=dockerhub --secret)
21 changes: 21 additions & 0 deletions codebuild/codebuild-project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "kube-cert-manager",
"description": "Build app and container for kube-cert-manager",
"source": {
"type": "GITHUB",
"location": "https://github.com/XXXX Your Account XXXX/kube-cert-manager.git",
"auth": {
"type": "OAUTH"
}
},
"artifacts": {
"type": "NO_ARTIFACTS"
},
"environment": {
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/docker:1.12.1",
"computeType": "BUILD_GENERAL1_SMALL"
},
"serviceRole": "arn:aws:iam::XXXX Account Number XXXX:role/service-role/XXXX CodeBuild Role Name XXXX",
"timeoutInMinutes": 30
}
13 changes: 13 additions & 0 deletions codebuild/create-codebuild-project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

#
# AWS CodeBuild Project for kube-cert-manager
# Aaron Roydhouse <aaron@roydhouse.com>
#
# Before creating the project, you must have logged into AWS CodeBuild Console,
# created a Service Role for CodeBuild and linked your AWS account with Github.
# Then customise 'codebuild-project.json' as required and create the project
# with a command similar to below.
#

aws codebuild create-project --profile default --region=us-east-1 --cli-input-json file://codebuild-project.json
31 changes: 31 additions & 0 deletions codebuild/get-codebuild-log.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

#
# Display CloudWatch log for a CodeBuild build ID
# Aaron Roydhouse <aaron@roydhouse.com>
# https://github.com/whereisaaron/
#

: ${PROJECT_NAME:=kube-cert-manager}
: ${PROJECT_REGION:=us-east-1}

ID=$1
if [[ -z "${ID}" ]]; then
echo "No CodeBuild build ID specified, displaying log for first build ID"
ID=$(aws codebuild list-builds-for-project --project-name=${PROJECT_NAME} --region ${PROJECT_REGION} --query "ids[0]" --output text)
fi

LOG_GROUP="/aws/codebuild/${ID%%:*}"
LOG_STREAM="${ID##*:}"

if [[ -z "${LOG_GROUP}" || -z "${LOG_STREAM}" ]]; then
echo "Usage: $0 <build.id>"
echo "$0 kube-cert-manager:eb8ad990-11ee-4c16-b475-32b2dba84888"
exit 1
fi

aws logs --region=us-east-1 get-log-events \
--log-group=${LOG_GROUP} \
--log-stream=${LOG_STREAM} \
--query="events[].message" --output=text \
| sed 's/^[ \t]*\(\[Container\][ \t]*\)\?//'
56 changes: 56 additions & 0 deletions codebuild/start-codebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
set -e

#
# CodeBuild makes it extremely awkward to specify which branch to build
# You can't specify it in the project, and you can't specify it on the command line
# You have to use a JSON options file, that you can't provide on stdin
# So have to make a temporary file, put the branch name there, and then pass file to CLI
# Usage: You can optionally specify a branch or commit id to build as an argument
#
# Aaron Roydhouse <aaron@roydhouse.com>
# https://github.com/whereisaaron/
#

: ${PROJECT_NAME:=kube-cert-manager}
: ${PROJECT_REGION:=us-east-1}
if [[ -n "$1" ]]; then
SOURCE_VERSION=$1
fi

: ${CONTAINER_VERSION:="0.4.0"}
: ${DOCKER_REPO:=whereisaaron}
: ${DOCKER_HUB_USERNAME?"Must specify DOCKER_HUB_USERNAME"}
: ${DOCKER_HUB_PASSWORD?"Must specify DOCKER_HUB_PASSWORD"}

START_OPTS=$(mktemp)
cat - > ${START_OPTS} <<END
{
"projectName": "${PROJECT_NAME}",
"sourceVersion": "${SOURCE_VERSION}",
"environmentVariablesOverride": [
{
"name": "CONTAINER_VERSION",
"value": "${CONTAINER_VERSION}"
},
{
"name": "DOCKER_REPO",
"value": "${DOCKER_REPO}"
},
{
"name": "DOCKER_HUB_USERNAME",
"value": "${DOCKER_HUB_USERNAME}"
},
{
"name": "DOCKER_HUB_PASSWORD",
"value": "${DOCKER_HUB_PASSWORD}"
}
]
}
END

BUILD_ID=$(aws codebuild start-build --region ${PROJECT_REGION} --cli-input-json "file://${START_OPTS}" --query "build.id" --output=text)

rm "${START_OPTS}"

echo "${BUILD_ID}"
Loading