Deploy to Cloud Run using GitLab CI

Timothy
Google Cloud - Community
3 min readJun 23, 2020
Cloud Run | GitLab

In this article, I would guide through deploying serverless containerized applications to Cloud Run, using GitLab CI and Cloud Build.

Cloud Run is a managed compute platform that enables you to run stateless serverless containers that automatically scales.

Cloud Build is a service that executes your builds on Google Cloud Platform infrastructure.

GitLab CI service is a part of GitLab that build and test the software whenever developer pushes code to application repo.

Prerequisites

Creating a Service Account for Google Cloud Build

  • On Google Cloud, navigate through Cloud Build > Settings.
  • Under Service account permissions, ensure that Cloud Run & Service Accounts are ENABLED , this allows you deploy to Cloud Run.
  • Since I have given Cloud Build sufficient permissions, I can create a Cloud Build service account on IAM & Admin > Service Accounts.
    I’ll create a service account (NAME@PROJECT.iam.gserviceaccount.com) and give it the Cloud Build Service Agent.
    On the created service account page, click on Add Key > JSON.
Service account page after creation of key

Configure GitLab CI to use Service Accounts

On the GitLab repo, navigate through Setting > CI/CD > Variables.

GitLab Repo Setting > CI/CD > Variables

As seen above, I created a variable for GCP_PROJECT_ID whose value is the Google Cloud Project ID and GCP_SERVICE_KEY whose value is the contents of the JSON service account earlier created.

Continuous Deployment to Cloud Run

With just some few steps left, my application would be continuously deployed to Cloud Run directly from our GitLab repo.

My application also has a Dockerfile which is configured to run on port 8080 (the default port for Cloud Run).

Finally, I created a cloudbuild.yaml file which contains the commands to build & deploy by Cloud Build and .gitlab-ci.yml file which triggers the deployment processes when code is pushed.

Here’s a preview of my Cloud Build CI file:

Cloud Build CI file

Here’s a preview of my GitLab CI file:

GitLab CI file

Additional Resources on Cloud Build

--

--