Skip to content

Instantly share code, notes, and snippets.

@markhilton
Last active March 12, 2022 18:20
Show Gist options
  • Save markhilton/52d79cf8bb1ef5935556cfeec9a5d14e to your computer and use it in GitHub Desktop.
Save markhilton/52d79cf8bb1ef5935556cfeec9a5d14e to your computer and use it in GitHub Desktop.
Google Cloud Build: K8s application rolling update deployment on repository code push
steps:
# slack deployment status
- name: 'gcr.io/cloud-builders/curl'
args: [ '-X', 'POST', '-H', 'Content-type: application/json', '--data',
'{"text":"`gcr.io/$PROJECT_ID/$_DEPLOYMENT:$BRANCH_NAME$TAG_NAME` build started!"}', '${_WEBHOOK_URL}' ]
# build docker image
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '--build-arg', 'TOKEN=${_OAUTH_TOKEN}',
'-t', 'gcr.io/$PROJECT_ID/$_DEPLOYMENT:$BRANCH_NAME$TAG_NAME', '.' ]
# push docker image to the repository
- name: 'gcr.io/cloud-builders/docker'
args: [ 'push', 'gcr.io/$PROJECT_ID/$_DEPLOYMENT:$BRANCH_NAME$TAG_NAME' ]
# patch kubernetes deployment to trigger rolling update
- name: 'gcr.io/cloud-builders/kubectl'
args: [ 'patch', 'deployment', '${_DEPLOYMENT}', '-n', '${_NAMESPACE}', '-p',
'{"spec": {"template": {"metadata": {"annotations": {"build":"$BUILD_ID"}}, "spec": {"initContainers": [{"name": "app","image": "gcr.io/$PROJECT_ID/$_DEPLOYMENT:$BRANCH_NAME$TAG_NAME"}]}}}}' ]
env:
# required K8s cluster env variables
- 'CLOUDSDK_COMPUTE_ZONE=${_K8S_ZONE}'
- 'CLOUDSDK_CONTAINER_CLUSTER=${_K8S_CLUSTER}'
# check rolling deployment status
- name: 'gcr.io/cloud-builders/kubectl'
args: [ '-n', '${_NAMESPACE}', 'rollout', 'status', 'deploy/${_DEPLOYMENT}' ]
env:
# required K8s cluster env variables
- 'CLOUDSDK_COMPUTE_ZONE=${_K8S_ZONE}'
- 'CLOUDSDK_CONTAINER_CLUSTER=${_K8S_CLUSTER}'
# slack deployment status
- name: 'gcr.io/cloud-builders/curl'
args: [ '-X', 'POST', '-H', 'Content-type: application/json', '--data',
'{"text":"${_DEPLOYMENT} deployment completed!"}', '${_WEBHOOK_URL}' ]
# store artifact
images: ['gcr.io/$PROJECT_ID/$_DEPLOYMENT:$BRANCH_NAME$TAG_NAME']
# arguments
substitutions:
_K8S_ZONE: us-central1-a # K8s zone
_K8S_CLUSTER: cluster_name # K8s cluster name
_NAMESPACE: default # K8s namespace
_DEPLOYMENT: deployment_name # K8s deployment name
_WEBHOOK_URL: slack_notification_webhook # slack notifications
_OAUTH_TOKEN: composer_github_oauth_token # access to private code repos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment