How to deploy to cloud RUN from GitLab CI

Nejmeddine Ben Ouarred
2 min readDec 29, 2020

--

In this article, I will demonstrate the list of steps to deploy to Cloud Run from your GitLab repository.

For that, you need to have:

  • Gitlab repository
  • GCP project

In the GCP project, you MUST follow steps bellow:

  • activate API fro Cloud RUN
  • create a service account having as IAM roles: Cloud Run Admin & Run Service Agent
  • create key (json format) for the created service account

In your Gitlab repository, you MUST create:

  • .gitlab-ci.yml
  • upload the service account key file in the (CI / CD Settings -> variable).

I decided to pusblish docker image to Cloud Registry (I will move to Cloud Artifact Registry asap), then I deploy the last published image to cloud Run.

Below, the two stages that allow you to do that:

  • Fist, I build the docker image, tag it and then push it to Cloud Registry
docker-build:
stage: package
script:
- echo "******** Starting docker build & gcr push ********"
- docker login -u _json_key --password-stdin https://eu.gcr.io < "$GCR_CREDENTIAL"
- docker build -t api .
- docker tag api eu.gcr.io/$GCP_PROJECT_ID/api
- docker push eu.gcr.io/$GCP_PROJECT_ID/api
  • Then, I deploy it to a cloud RUN (already created using GCP Consol)
deploy:
when: manual
stage: deploy
image: google/cloud-sdk
services:
- docker:dind
script:
- gcloud config set project $GCP_PROJECT_ID
- gcloud auth activate-service-account --key-file "$CLOUD_RUN_DEPLOYER"
- gcloud run deploy CLOUD_RUN__SERVICE_NAME --image eu.gcr.io/$GCP_PROJECT_ID/api:latest --platform=managed --region=europe-west1 --project=$GCP_PROJECT_ID

Let’s build :) !

--

--

Nejmeddine Ben Ouarred

Certified GCP Architect | GCP Authorized trainer | Certified Google APIGEE | APIGEE Authorized trainer