Automated deployment with Google Compute Engine and Cloud Build

and a cup of tea

This article shows how to use a set of tools available in Google Cloud Platform that allows your team to set up a development + automated build + continuous delivery pipeline using Google Cloud Engine + Google Cloud Build.

Requirements

If you don’t have that, follow this brilliant and easy tutorial to deploy a node application online.

What is CI/CD ?

CI/CD stands for Continuous Integration and Continuous Deployment.

Big words for an easy concept : Automated deployment and testing.

Continuous integration improves collaboration and quality.

Teams using CI/CD are going through a few steps before deploying code to the end user.

The GitLab Flow proposes a pre-production and production branch before deploying code online.

With that kind of flow, automated deployment is essential to avoid spending hours on a server…

The process

The pipeline will be as follow :

Create a pull.sh file

The pull.sh file is a bash script that will execute 2 tasks :

This file will be executed after any push on Github (see next parts).

Google Cloud Build use root user to access our server, we first need to act as a root user.

Run the following command :

sudo su roottouch pull.sh sudo vim pull.sh

In Vim, press “i”, paste the following code (adapt with your data) :

Press Esc, type “:wq” and press enter.

You should have something similar to this :

Now, type :

chmod +x pull.sh

This will give root user the permission to execute the script.

To be sure everything is correctly installed, execute pull.sh :

./pull.sh

Recap :

The script is executed successfully. First, it’s pulling last updates from Github, then it restarts the process manager.

First task… Done! ✅

Create a Google Source Repository

You need to have a Github repository to push code to the main branch.

If you have your own repo, all good, if not, fork mine.

Once you have a Github repository, link it with https://source.cloud.google.com/repos.

Click on “Add repository” on the top-right corner.

Then connect to an external repository.

Select a project and authorize Google to go to your Github repository

This being done, you can see the new repository in the list :

Any update done on Github will impact this Google Source Repository.

We can now create a trigger on push on the main branch.

YAML file

You might have noticed the cloudbuild.yml file in my repository.

This file contains command lines to execute the pull.sh file we just created.

DON’T FORGET to change your server name, the zone and the user name.

Create a trigger

Short reminder of the process :

Push on main branch => pull.sh bash script executed by root user => New code pulled on server and server restart

How will pull.sh be executed? By cloudbuild.yml

How will cloudbuild.yml be executed? By a trigger

How will the trigger be executed ? By a push on main branch.

Let’s create a trigger!

Go to https://console.cloud.google.com/cloud-build/builds and click on “Triggers”

Then, click on “+ Create a trigger”

Copy the following configurations :

And you are all set!!

We can test our trigger by clicking on the RUN button :

A snackbar should be displayed : click on SHOW

In the build log, you can see the cloudbuild file executing and finally running the pull.sh file.

Verify the entire CI/CD process

When getting from http://34.105.205.74/api/v1/ping we get “pong”

Let’s change the response with “pong from CI CD process” and push the changes on main :

The trigger is now running on cloud build :

And the response is magically updated online :

So long and thanks for all the fish.

Future Unicorn Founder — Using tech for good

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store