Setting up a CI/CD for GAE and Serverless with Google Cloud Build

Google Cloud Build in the GitHub Marketplace

If you don't know what is a Continuous Integration / Continuous Deployment (CI/CD), do some research and read other articles first, cause I'm gonna straight to the point here.

I discovered Google Cloud Build when I was searching for a CI/CD tool that can build a docker container, run the tests, build this same container for production and deploy it to Google App Engine (GAE) at once. For my surprise Cloud Build does even more than that, it can deploy serverless functions in parallel to Google Functions, extremely fast and without the need for a key/secret, just using IAM roles and service accounts. Finally, the price: 120 FREE minutes per day, $0.0034 per minute after that.

So let's do it! Go to the Cloud Build in the Google Cloud console, and add a new trigger:

Select a source and next a repository:

And choose the branch or tag to deploy when a new commit is pushed to it:

You can choose a Dockerfile to build a image or a cloudbuild.yaml to a deploy to a Google service. You can also set environment variables starting with a underscore and use they like below.

Testing and deploying a Nuxt.js/Vue.js or Node.js app to GAE

The above settings will:

  1. Copy the production envs from a bucket to the deployment tool
  2. Install
  3. Lint
  4. Test
  5. Build
  6. Deploy the app to the Google App Engine

Testing and deploying a Laravel app to GAE using docker

The above settings will:

  1. Build a docker image and install composer dependencies
  2. Run the tests
  3. If all tests run successfully, deploy the container to GAE.

Deploying serverless functions to cloud functions

Until here, all steps of the cloudbuild were running serially, but when you working with serverless functions, you don't need to deploy they serially, you can deploy all they at once!

As you can see above, the first step will set the env vars for the functions (copy they from a bucket), then, the other functions will waitFor that step, once complete, all the other steps will run in parallel.

That's it!