Set up automated deployments with Google Cloud Run and Gitlab
Published: 2021-05-07 20:03:45

---
title: Set up automated deployments with Google Cloud Run and Gitlab
published: true
description: 
tags: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y89ivh5j0x8oou9cx9bh.jpg
---
Let's look at how we can set up a continuous delivery pipeline for our Google Cloud Run projects with Gitlab CI/CD

<strong>Prerequisites</strong>
Google Cloud Run:
 > Cloud Run is a serverless, managed compute platform that enables you to run stateless containers that are invocable via web requests or Pub/Sub events.
 To run a Cloud Run service, you need to:
* Have a [Google Project](https://console.cloud.google.com/project)
* [Enable Cloud Run API](https://console.developers.google.com/apis/api/run.googleapis.com/overview)
* [Enable Cloud Build API](https://console.cloud.google.com/cloud-build/builds)


Gitlab:
 > GitLab is a web-based DevOps lifecycle tool that provides a Git-repository manager providing wiki, issue-tracking, and continuous integration and deployment pipeline features, using an open-source license, developed by GitLab Inc.
 You can create a Gitlab project here: [New Gitlab project](https://gitlab.com/projects/new)

Clone sample repo here:
[https://gitlab.com/niyi/myhelloworldapp](https://gitlab.com/niyi/myhelloworldapp)

<strong>Step 1a: Enable Service Account</strong>
A service account is a special kind of account used by an application to make authorized API calls on the GCP platform.
On your Google Cloud project, navigate through Cloud Build > Settings.
Under Service account permissions, make sure both Cloud Run and Service Accounts are enabled
<strong>Step 1b: Create a Google Service Account</strong>
We'll create a new service account for your application to use
* On your Google Cloud project, navigate through IAM & Admin > Service Accounts > Click on CREATE SERVICE ACCOUNT
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikb4jdw0pgf35uvxf4cz.jpg)
* Give your new service account any name you want and click CREATE
* Add the following roles to your service account by clicking Select Role input under task number 2
  1. Cloud Build Service Agent
  2. Service Account User
  3. Cloud Run Admin
  4. Project Viewer
* Click Create then click Done to add the account.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sse7hrlpzsezhw6no3no.jpg)
* Generate a credential file for this account by navigating to the newly created service account > Keys > Click on Add Key > Create New Key. Select JSON and click CREATE
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byg0btv07j354147jg58.jpg)

<strong>Step 2: Setup Gitlab CICD variables</strong>
In this step, we'll create variables that we'll use in our code. One for the GCP Project ID and another for the Service Account we created earlier
* Navigate to the project repository on Gitlab > Settings > CI/CD
* To add a variable, under the Variables section, click the Expand button and click on Add Variable
We need to add two variables, one names GCP_PROJECT_ID with the value of our GCP Project ID and the other named GCP_SERVICE_ACCOUNT for the content of the JSON we downloaded earlier
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01ijlhysfoenyxljfuo6.jpg)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ldxultn8bj3k19e4745.jpg)

<strong>Step 3: Setup Application code</strong>
We need to configure our code to connect to Gitlab CI/CD. We'll also use Docker to containerize our application so it runs the same across multiple platforms.  
* We've added a Dockerfile in our application that will run on PORT 8080, which is Google Cloud Run's default port
* We've also added a .gitlab-ci.yml file which is the file the triggers our [CI/CD pipeline on Gitlab](https://docs.gitlab.com/ee/ci/quick_start/index.html)
*
``` 
# File: .gitlab-ci.yml
variables:
  SERVICE_NAME: "myHelloWorldApp"

deploy:
  stage: deploy
  only:
    - master # This pipeline stage will run on this branch alone

  image: google/cloud-sdk:latest # We'll use Google Cloud SDK for Cloud Run related commands
  script:
    - echo $GCP_SERVICE_ACCOUNT > gcloud-service-key.json # Save Google cloud contents in a temporary json file
    - gcloud auth activate-service-account --key-file gcloud-service-key.json # Activate your service account
    - gcloud auth configure-docker # Configure docker environment
    - gcloud config set project $GCP_PROJECT_ID #Set the GCP Project ID to the variable name
    - gcloud builds submit --tag gcr.io/$GCP_PROJECT_ID/$SERVICE_NAME #Run the gcloud build command to build our image
    - gcloud run deploy $SERVICE_NAME --image gcr.io/$GCP_PROJECT_ID/$SERVICE_NAME --region=us-east4 --platform managed --allow-unauthenticated # Run the gcloud run deploy command to deploy our new service
```
Replace the SERVICE_NAME value with the desired name for your application and save the changes.

At the end of the file, we're running the commands [gcloud build](https://cloud.google.com/run/docs/building/containers) and [gcloud run deploy](https://cloud.google.com/run/docs/deploying) to build and deploy our application respectively.

Push your changes to the remote Gitlab repository and watch as your new baby is created.

To monitor the progress of your deployment on Gitlab navigate to CI/CD > Pipelines and click on the latest job.
To see your new application on Cloud Run, navigate to GCP > Cloud Run and search for the name of the service
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3wtnne0vnv6p76aoxn4.jpg)

Congratulations!