Looking for Senior AWS Serverless Architects & Engineers?
Let's TalkWhat is Google Cloud Build?
Cloud Build is a continuous build, test, and deploy pipeline service offered by the Google Cloud Platform. Cloud Build falls into the category of a cloud CI/CD (Continuous Integration and Continuous Deployment).
“Cloud Build lets you build software quickly across all languages. Get complete control over defining custom workflows for building, testing, and deploying across multiple environments such as VMs, serverless, Kubernetes, or Firebase.” — Documentation
Prerequisites:
- GCP Account
- Basic knowledge of CI/CD
- Knowledge of Bash Scripting
When creating CI/CD pipelines we need to be able to run custom scripts which help extend our builds and catch edge cases when doing heavy automation. Usually, these are bash scripts.
Below is an ideal scenario:
You can see below, we are saying “run this npm script defined in our package.json file and in turn run our install.bash script”.
Why do we configure it this way?
You may be wondering why we even add the npm script and don’t just run the bash script directly. Well, this is due to the wanting to leverage the Google Cloud Builds built in functionality called, Cloud Builders. Cloud Builders allow you to tap into preconfigured containers which normally you would need to define yourself. Meaning for most use cases you don’t need to ever concern yourself with building containers for your CI/CD.
For example, there is a Cloud Builder for npm. Which makes it simple to run npm commands without needing to install node on the server or container running your builds. It just works out of the box. To be fair, the point of Google Cloud Build to a large degree is that it abstracts the “build server” away completely.
Cloudbuild.yml:
This an example of what an entire CI/CD pipeline may look like with a series of npm scripts. You can see we install, build, test, and deploy.
Package.json:
The package.json can be thought of as middleware. Connecting our Google Cloud Build pipeline, or cloudbuild.yml, to the bash scripts which actually run the commands.
Install.bash:
This is a basic example, all we do is run npm install. However, now that you have access to npm. You can get much more advanced from here.
Use Cases:
Here are some common use cases, we’ve seen at Serverless Guru.
Web Application Testing and Deployment:
Google Cloud Build with the setup we’ve just shown above is great for building and deploying your web applications. By taking your existing bash scripts which make these deployments locally and putting them into Google Cloud Build you have the power of automatically starting your CI/CD off pushes to GitHub, BitBucket, or Google Source Repositories.
Backend Testing and Deployment:
If you have backend APIs which are deployed to GCP or AWS then you can leverage your existing automation framework. We love the Serverless Framework for automating AWS deployments. Normally, you would take your backend services and wrap them up into some deployable unit. This is a perfect candidate for Google Cloud Build as you can have an automated testing step which will break your build if the code that was pushed to git was broken.
That’s the end of this high-level workflow for creating CI/CDs using Google Cloud Build which are powered by bash scripts. At the end of the day. Once you’re able to run a bash script, the world is your oyster.