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):
Overview of the deploy process:
- Your app is running as a service on Kubernetes
- You update the code in your local repo on your dev machine
- You commit those changes to git and push to the master branch on Github
- GCP will see the commit and update the mirrored repo in GCP source repositories.
- A GCP build trigger will see the updated master repo and build a docker container.
- GCP will not update the kubernetes service with the new docker build until you tell it to.
- You type
yarn gcpon 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:
- Your app’s code is downloaded on your machine and you have kubectl/gcloud cli installed. Your PROJECT_ID is set correctly in gcloud.
- 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.
- You have the app running as as service in kubernetes successfully.
- You are ok with linking your Github repo to GCP’s container registry build triggers if you haven’t already.
- 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.
- After saving the above file, edit
package.jsonand add the following script:
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:
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.
- Make any changes to the local source code on your dev machine.
- Commit the changes and push to your remote-origin (push to github etc).
- Login to Google cloud console, go to Container Registry -> Build History.
- You should see a new build triggered and running. Check out the details and confirm the container name looks good.
- 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.
- Once it detects the build has completed successfully, it will update the Kubernetes service with the new docker image.
- You can run
kubectl get podsto monitor the update.
- That’s it, check the pubic url and see if your new version is running.
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