Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cloudbuild config for automating deployments to Kubernetes using Helm.
steps:
- name: 'gcr.io/cloud-builders/git'
# Determine if the commit that triggered this build includes a directive to destroy a previously created environment
# If so, write a file as a flag for conditional future steps, always return true to allow build to continue
entrypoint: '/bin/bash'
args:
- '-c'
- 'git log -n 1 --pretty=format:%s $COMMIT_SHA | grep -q "\[helm\-delete\]" && touch /workspace/helm_delete || true'
- name: 'gcr.io/cloud-builders/docker'
entrypoint: '/bin/bash'
args:
- '-c'
- |
if [ -f /workspace/helm_delete ]
then
# Pull the existing image so the "images" directive of this file works as expected
docker pull gcr.io/$PROJECT_ID/application_base:$BRANCH_NAME
else
# Otherwise build a new image, using a cache, and then push it so it's available in subsequent steps
docker pull gcr.io/$PROJECT_ID/application_base:$_CACHE_IMAGE_TAG
docker build --cache-from gcr.io/$PROJECT_ID/application_base:$_CACHE_IMAGE_TAG -t gcr.io/$PROJECT_ID/application_base:$BRANCH_NAME .
docker push gcr.io/$PROJECT_ID/application_base:$BRANCH_NAME
fi
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: '/bin/bash'
args:
- '-c'
- |
gcloud container clusters get-credentials cluster-1 --zone us-west1-a --project $PROJECT_ID
kubectl version #hack to properly init credentials
cp ~/.kube/config /workspace/kubeconfig
- name: 'gcr.io/cloud-builders/docker'
args:
- run
- '-e'
- 'KUBECONFIG=/root/app/kubeconfig'
- '-e'
- 'TILLER_NAMESPACE=kube-system'
- '-v'
- '/workspace:/root/app'
- '--entrypoint'
- '/bin/sh'
- 'linkyard/docker-helm'
- '-c'
- |
/bin/helm init --client-only &&
{
if [ -f /root/app/helm_delete ]
then
/bin/helm delete --purge --debug $BRANCH_NAME
else
/bin/helm upgrade --install --recreate-pods --timeout 1500 --wait --debug --set image="gcr.io/${PROJECT_ID}/application_base:${BRANCH_NAME}" $BRANCH_NAME /root/app/k8s/helm
fi
}
images: ['gcr.io/$PROJECT_ID/application_base:$BRANCH_NAME']
timeout: 1500s
@bholzer

This comment has been minimized.

Owner

bholzer commented Jan 2, 2018

I wanted to use Google's Cloud Container Builder to automate Kubernetes deployments using Helm, and this is what I ended up with.

I'm currently using a build trigger to catch pushes to branches with a name like /.+\-test-env/ to build or update test environments.

An environment is destroyed when a commit containing [helm-delete] is pushed to the corresponding branch.

This also relies on the linkyard/docker-helm image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment