CI/CD Pipeline on GCP using Cloud Source Repositories, Cloud Build, Artifact Registry and App Engine

Taka
4 min readJan 16, 2022
CI/CD Pipeline on GCP Architecture example

In this article, I will show you a CI/CD pipleline configuration focusing Cloud Build(cloudbuild.yaml) on GCP. In this example, Cloud Source Repositories, Cloud Build, Artifact Registry and App Engine are used.

I will show you command based in case you would like to check build and deployment with command base as an appendix.

Cloud Source Repositories

A single place for your team to store, manage, and track code.

Cloud Source Repositories is a source code management tool on GCP. Generally, as other service, Github is known well.

Cloud Build

Build, test, and deploy on our serverless CI/CD platform.

Cloud build helps you build the image such as a Docker image, push the image to a repository then deploy the application on a platform.

Artifact Registry

The next generation of Container Registry. Store, manage, and secure your build artifacts.

This seems an upper compatibility of the previous service, Container Registry.

App Engine

Build highly scalable applications on a fully managed serverless platform.

App Engine is a serverless platform on GCP as written.

For instance, we develop an application like a Frontend server or an API server running as a Docker container on our local PC. When we provide a service for your customers, we probably deploy these applications on cloud service. App Engine is one of the solutions to deploy these application servers.

As other serverless platform, there is Cloud Run which is the latest serverless service on GCP. I do not refer to it here.

Prerequisites

  • Your source code is push into Cloud Source Repositories (or it is fine into other services such as Github etc but the configuration on Cloud Build is slightly different).
  • The application is developed as it runs as Docker Container.
  • The repository for docker images is created on Artifact Repository. (I have not checked Container Registry is also fine yet.)
  • Application is running on App Engine.

Configure CI/CD Pipeline — Cloud Build

In order to deploy the applications automatically, three points must be done before pushing the source code.

  • Cloud Build trigger configuration
  • Define cloudbuild.yaml file
  • Define app.yaml file

Step1: Trigger

Let's open the Cloud Build page and create new trigger.

Name is trigger unique name in the project, Tags is just for arranging the triggers you will create in the project.

In the Event filed, select "Push to a branch". This means when you push the source code to one branch then the trigger is invoked.

In the Source filed, you can choose the repository you want Cloud Build to watch. For instance, the repository on Cloud Source Repositories or if you want to use Github repository, choose "CONNECT NEW REPOSITORY" and select what you want to use. The Authentication will run.

Branch input box, you indicate what exactly branch with a regular expression. For example, in my case I want the trigger to run when I push the source code to main branch so I wrote "^main$" in the input box. If you just write "main"(only main), when you push a code to even "maintenance" branch, the trigger will run.

In the Configuration filed, select "cloud Build configuration file (YAML or JSON)" to use cloudbuild.yaml in your git repository. "cloudbuild.yaml" will be defined automatically in "Cloud Build configuration file location" box.

Finally, you create!!

Step2: Build & Push an image

The next is defining cloudbuild.yaml file. The bellow is the example.

steps is for the jobs. In this case, after a trigger invoked, the jobs run from the top in order. Firstly, the build step runs(this seems almost same as usual docker build) then push the built image to the artifact repository you created before, then finally deploy the application by using gcloud app deploy command and app.yaml file which I explain from now.

Step3: Deploy an application

Deployment itself is invoked in Cloud Build trigger but the environment where the application is deployed must be defined.

In this case, the configuration is defined in app.yaml file because App Engine is used.

The documentation about app.yaml for App Engine is here.

When you deploy the image on Cloud Run instead of App Engine, gcloud run deploy command should be defined in cloudbuild.yaml.

Building and deploying a container

This is the CI/CD Pipeline on GCP using Cloud Source Repositories, Cloud Build, Artifact Registry and App Engine. When you check the CI/CD pipeline running status, open the Build History page from the History tab on Cloud Build page.

Well done.

Appendix

Command base build and deployment

Build an image using a build config file

Follow the instruction on the above then execute gcloud app deploy(or gclou run deploy) command on your terminal as it is defined in cloudbuild.yaml.

Thank you.

--

--