Deploy to Cloud Run using GitLab CI

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

--

--

--

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Recommended from Medium

Nested Fields: Spark3 Way

Goal-setting with OKR for engineering teams

Trading: Can you move the TP and how big should be SL?

XAttacker-Tool Website Vulnerability Scanner Auto Exploiter

org.hibernate.orm.deprecation

Create multi-arch Docker images using Jenkins Pipeline

Sharing My Blockchain Development Learning Experience: Building a Cryptocurrency Exchange App and…

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
Timothy

Timothy

Software / DevOps Engineer | Google Developer Expert for Cloud | https://www.codementor.io/@timtech4u

More from Medium

How to deploy microservice to Google Kubernetes (GKE) with sed in Github matrix

Solutions for Operation not permitted error when Postgresql is running on Docker using Vagrant’s…

How To Deploy Nuxt Js App On Docker Swarm Cluster

Installing AWS CLI with Dockers