CI/CD: Google Cloud Build — Custom Scripts

December 10, 2018

What 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”.

  
# Terminal
$: npm run install
# package.json
"install": "bash ./scripts/install.bash"
# install.bash
# Install Dependencies
npm install
  

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.

  
steps:
  - name: 'gcr.io/cloud-builders/npm'
    args: ['npm', 'run', 'install']
  - name: 'gcr.io/cloud-builders/npm'
    args: ['npm', 'run', 'test']
  - name: 'gcr.io/cloud-builders/npm'
    args: ['npm', 'run', 'build']
  - name: 'gcr.io/cloud-builders/npm'
    args: ['npm', 'run', '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.

  
{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "install": "bash ./scripts/install.bash",
    "test": "bash ./scripts/test.bash",
    "build": "bash ./scripts/build.bash",
    "deploy": "bash ./scripts/deploy.bash"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
  

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.

  
# ./scripts/install.bash
# Install Dependencies
npm install
# Download something..
# Create some file for build step..
  

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.

Serverless Handbook
Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones - Founder
Ryan Jones
Founder
Speak to a Guru
arrow
Edu Marcos - CTO
Edu Marcos
Chief Technology Officer
Speak to a Guru
arrow
Mason Toberny
Mason Toberny
Head of Enterprise Accounts
Speak to a Guru
arrow

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.