Easily deploy new versions of code to Kubernetes on GCP with a single command.

This article will go over how to create a basic workflow that will allow you to push new versions of your app using a single command.

I’m referring to it as the Poor man’s continuous integration script for Kubernetes on GCP (Google Cloud Platform).

Once this is configured you will only need to type the below command to update your app (node.js):

yarn gcp

Overview of the deploy process:

  1. Your app is running as a service on Kubernetes
  2. You update the code in your local repo on your dev machine
  3. You commit those changes to git and push to the master branch on Github
  4. GCP will see the commit and update the mirrored repo in GCP source repositories.
  5. A GCP build trigger will see the updated master repo and build a docker container.
  6. GCP will not update the kubernetes service with the new docker build until you tell it to.
  7. You type yarn gcp on your dev machine, this script will monitor the build trigger and will automatically update the service on kubernetes with the new build.

This article assumes the following:

  1. Your app’s code is downloaded on your machine and you have kubectl/gcloud cli installed. Your PROJECT_ID is set correctly in gcloud.
  2. Your app is a git repo, is available on Github, and has the remote-origin url set correctly, you are able to commit and push changes to your master branch. You can adjust as needed for Bitbucket etc.
  3. You have the app running as as service in kubernetes successfully.
  4. You are ok with linking your Github repo to GCP’s container registry build triggers if you haven’t already.
  5. We will be deploying a Node.js app, but you can adjust as needed for any Docker based app.

Here is the completed build script that we will be using. Copy it to a file deploy.sh in your project root. There aren’t any secrets in this file, so you can commit it to source.

  1. After saving the above file, edit package.json and add the following script:
# package.json
{
scripts: {
"gcp": "./deploy.sh"
}
}

2. You will need to make the deploy.sh script executable on your dev machine if it won’t run later.

$ chmod +x deploy.sh

3. On Google Cloud console, visit Container Registry -> Build Triggers. Setup a build trigger for your app’s repo. Assuming Github, you will need to authenticate and add your repo using the wizard on GCP.

4. For the trigger settings, provide any name. You only need to set the branch name. You can type master if that’s the only branch you would like. For the image name, leave it as default or use the below:

gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA

5. That’s pretty much it for the build trigger, customize any other settings you may need.

6. On your dev machine, customize the deploy.sh file as needed. Read the inline comments for more info.

  • Set the NAME, LOGO and URL according to your app.
  • Make sure your PROJECT_ID is set in gcloud cli

Testing it out

Now we can update the source code and verify the build trigger is working.

  1. Make any changes to the local source code on your dev machine.
  2. Commit the changes and push to your remote-origin (push to github etc).
  3. Login to Google cloud console, go to Container Registry -> Build History.
  4. You should see a new build triggered and running. Check out the details and confirm the container name looks good.
  5. On your dev machine, type yarn gcp . You will see the deploy.sh script start to run and check on the remote build status. Depending on how long the build takes, the script will keep checking every 45 seconds.
  6. Once it detects the build has completed successfully, it will update the Kubernetes service with the new docker image.
  7. You can run kubectl get pods to monitor the update.
  8. That’s it, check the pubic url and see if your new version is running.

Closing notes

Basically anytime you need to update your app on Kubernetes, push some code to Github then run yarn gcp and wait a few minutes. Your app should update automatically.

You can modify the deploy.sh to check more frequently, or in shorter intervals etc depending on how long your build process usually takes. Currently it checks 9 times, every 45 seconds before timing out.

You can also use a different script name than “gcp” yarn gcp etc. Just update the name in package.json accordingly.