Set up automated deployments with Google Cloud Run and Gitlab

niyi

Niyi

Posted on May 7, 2021

Set up automated deployments with Google Cloud Run and Gitlab

Let's look at how we can set up a continuous delivery pipeline for our Google Cloud Run projects with Gitlab CI/CD

Prerequisites
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:

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

Clone sample repo here:
https://gitlab.com/niyi/myhelloworldapp

Step 1a: Enable Service Account
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
Step 1b: Create a Google Service Account
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
  • 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
  • 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

Step 2: Setup Gitlab CICD variables
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 Alt Text

Step 3: Setup Application code
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 *
# 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
Enter fullscreen mode Exit fullscreen mode

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 and gcloud run deploy 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

Congratulations!

💖 💪 🙅 🚩
niyi
Niyi

Posted on May 7, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024