CI/CD With Cloud Build & Cloud Run

Simant Thapa Magar
readytowork, Inc.
Published in
5 min readJan 7, 2024

CI (Continuous Integration) means automating the builds to test the application after changes and CD (Continuous Deployment) means deploying the changes and making the application readily available. CI/CD is the modern software development practice where incremental code changes are made frequently and reliably.

Today we will be looking at this modern software development practice where we will be using two services from Google Cloud which are Cloud Build for continuous integration and Cloud Run for continuous deployment and combine them to achieve auto build and auto deployment whenever there is a change in our code source.

Create Code Source

For CI/CD we need to have a code source which will be the GitHub repository in our case. As an example application, we will be building a simple web server using node js and docker.

Inside server.js a simple web server that returns Hello world as the response will be

const http = require('http')
const server = http.createServer((req,res) => {
console.log("New connection")
res.end("Hello World!")
})
const PORT = process.env.PORT || 8080
server.listen(PORT, () => {
console.log("Listening")
})

Dependency and application packaging inside Dockerfile will be

FROM node:latest
WORKDIR /app
ADD . /app
CMD node server.js

Starting With Cloud Build

Cloud build will build our application in the Google Cloud by importing our source code. The setup for this process will be as follows

  • Navigate to Google Cloud Console and search Cloud Build
  • If the API hasn’t been enabled then Google Cloud will prompt you to do so
  • From the menu click Triggers and then click Create Trigger
  • Follow the steps shown
    - Select the region that suits the most or closest one
    - Select Github as the source code management provider

-Login and authorize Google Cloud build to access the repository

- Select the repository that contains the application to be built

-Create a trigger by giving a unique name and customizing any fields if required otherwise it can be left as the default

After completion of these steps we can push a commit to the repo and from the history menu see the build starting as shown in the image below.

Starting With Cloud Run

Cloud Run handles the deployment aspect. To configure the cloud run the search cloud run in the search bar and navigate to the result shown.

  • Click on Create service
    - Enter the image URL or click on select and search for the built image and select it
    - Fill in other necessary fields

-For the authentication option we don’t want to enable any sort of authentication as a web application is accessible using a web browser

After creating the service it is now ready to deploy the application.

Connecting Cloud Build & Cloud Run

By now we have configured both the CI & CD aspects of the development but they are still not connected and for the process to be completed we require build & deployment to be automated after a trigger. For this, we will update our source code and create cloudbuild.yaml file where we will define our step to run i.e. build the docker image and then push the docker image. Note that when creating a trigger for cloud build, Configuration the option was left defaulted to Autodetected which means a cloudbuild.yaml or Dockerfile would be detected in the repository.

Inside cloudbuild.yaml we need to define the necessary steps of the building, pushing the docker image, and running the deployment command as shown below

steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/quick-job-japan-dev/gcp-deploy:$SHORT_SHA', '.']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/quick-job-japan-dev/gcp-deploy:$SHORT_SHA']
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'gcp-deploy', '--region=asia-east2', '--platform=managed', '--image=gcr.io/quick-job-japan-dev/gcp-deploy:$SHORT_SHA']

Note that $SHORT_SHA is a unique character auto-replaced by Google Build as we would require a unique name for each build.

After making these changes and pushing the commit, the build automatically starts and we might expect it would be the completion of the CI/CD process but unfortunately, it won’t be because as shown in the image below the build will fail.

Reason for failure?

It is a permission issue where Cloud build, a service of Google Cloud tries to run a command of Cloud run but doesn’t have access to do so. It is a default behavior where one service doesn’t have access to another service. So we need to enable permissions for Google Cloud build. For this

  • Navigate to settings from the menu and enable permission for Cloud Run
  • A prompt message will be shown to enable Service Accounts and allow permission for that as well.

Now if we commit any change to the source code then build and deployment will be automated and using the URL available from the cloud-run dashboard we can verify the CI/CD process.

--

--