Deploying to Google Cloud Run with GitLab CI/CD: A Step-by-Step Guide
Tags: gitlabc, googlecloud, continousintegration
Published: 2023-04-16 16:47:43

Google Cloud Run is a powerful platform that allows developers to run stateless HTTP containers without worrying about the underlying infrastructure. With GitLab CI/CD, you can automate your build, test, and deployment process to Cloud Run, making it a perfect match for modern application development.

In this article, I will walk you through the process of setting up a GitLab CI/CD pipeline to deploy your code to Google Cloud Run.

**NOTE: If you want to use Github Actions instead of Gitlab CI/CD, see my other article** [**here**](https://medium.com/@oluwafemiakinde/deploying-containerized-web-apps-to-google-cloud-run-using-github-actions-777590c8bda5)**.**

Let’s continue….

## Prerequisites

Before we get started, make sure that you have the following:

*   A Google Cloud account
*   A GitLab account with a repository containing your code
*   The Google Cloud SDK installed on your local machine
*   Docker installed on your local machine

## Step 1: Create a Google Cloud Run Service

First, we need to create a Google Cloud Run service that will host our application. To do this, follow these steps:

1.  Open the Google Cloud Console and navigate to the Cloud Run page.
2.  Click the “+ Create Service” button.
3.  Choose your preferred region and select the “Deploy one revision from an existing container image” option.
4.  Enter a name for your service and select the container image you want to deploy.
5.  Click “Create” to create your Cloud Run service.

## Step 2: Authenticate the Google Cloud SDK

To deploy your code to Cloud Run, you need to authenticate the Google Cloud SDK on your local machine. To do this, follow these steps:

1.  Open your terminal and run the following command:

```
gcloud auth login
```

2\. Follow the prompts to log in to your Google Cloud account.

## Step 3: Create a GitLab CI/CD Pipeline


Now that we have our Cloud Run service set up and authenticated the Google Cloud SDK, we can create a GitLab CI/CD pipeline to automate our deployment process.

1.  In your GitLab repository, create a new file called `.gitlab-ci.yml`.
2.  Add the following code to the file:

```
image: docker:latest  
  
services:  
  - docker:dind  
  
before\_script:  
  - docker login -u $CI\_REGISTRY\_USER -p $CI\_REGISTRY\_PASSWORD $CI\_REGISTRY  
  
deploy:  
  image: google/cloud-sdk:latest  
  script:  
    - gcloud auth activate-service-account --key-file=google-creds.json  
    - gcloud config set project $PROJECT\_ID  
    - gcloud builds submit --tag gcr.io/$PROJECT\_ID/$CI\_PROJECT\_NAME:$CI\_COMMIT\_SHA  
    - gcloud run deploy --image=gcr.io/$PROJECT\_ID/$CI\_PROJECT\_NAME:$CI\_COMMIT\_SHA --platform=managed --region=$CLOUD\_RUN\_REGION --allow-unauthenticated --update-env-vars=VAR1=value1,VAR2=value2 --quiet  
  only:  
    - master
```

3\. Replace `$PROJECT_ID` with your Google Cloud project ID and `$CLOUD_RUN_REGION` with your preferred region.

4\. Add any environment variables you need to the `--update-env-vars` flag.

5\. Commit and push your changes to your GitLab repository.

## Step 4: Configure GitLab CI/CD Variables

Finally, we need to configure some variables in GitLab CI/CD to authenticate our Google Cloud account and registry. To do this, follow these steps:

1.  In your GitLab repository, navigate to “Settings” > “CI/CD” > “Variables”.
2.  Add the following variables:

*   `GOOGLE_APPLICATION_CREDENTIALS` - the contents of your Google Cloud service account key file.
*   `PROJECT_ID` - your Google Cloud project ID.
*   `CI_REGISTRY_USER` - your GitLab username.
*   `CI_REGISTRY_PASSWORD` - your GitLab personal access token.

## Conclusion

Congratulations! You now have a fully automated GitLab CI/CD pipeline that deploys your code to Google Cloud Run. With this setup, you can focus on writing code and let GitLab and Google Cloud handle the rest.

If you liked this article, please leave a clap or even a comment and don’t forget to follow me to get updated when I publish another one. Thanks!