How to deploy to the Google Run with Gitlab CI/CD

Peter Kracik
3 min readJun 13, 2020

--

Recently I wrote an article about using Google Cloud run a hosting option for a (small) Wordpress website. You can read it here:

And in this article I show you how to set up CI/CD to deploy this website.

1. Service account

First we need to create a service account with permissions to be able to build and deploy. Roles we need to assign to the service account are:

  • Cloud Build Editor — to be able to build the image
  • Storage admin — Container registry uses Cloud Storage to store images, our service account needs to create a bucket and also read and write object into it.
  • Editor — without this role, it wouldn’t allow us to access resources, it would throw error: ERROR: (gcloud.builds.submit) HTTPError 403: <?xml version='1.0' encoding='UTF-8'?><Error><Code>AccessDenied</Code><Message>Access denied.</Message><Details>*** does not have storage.objects.get access to <project-id>
creating service account
Adding roles to the service account

2. Service account key

We need to generate a private key in the JSON format, to be able to log in our pipeline with this service account.

Then we copy the content of the JSON file to the Gitlab CI/CD variable for our pipeline, let’s call it GCLOUD_SERVICE_KEY_FILE and the type must be set to File.

3. Create CI deploy job

We create a .gitlab-ci.yml file, where we add

  1. variables with the name of the project and Cloud Run instance name
  2. we create job, let’s call it deploy_gcloud based on official Google Cloud SDK image
  3. in before_script we use our created CI/CD variable GCLOUD_SERVICE_KEY_FILE to authenticate with our service account and select the project project
  4. And in the script we build our docker image and then deploy it. As arguments I pass the port to expose on, region, type of the platform and gitlab build id as revision.
gitlab-ci.yam

That’s it!

Now on each push to master, it will be deployed to the Google Cloud run instance.

deploying

--

--

Peter Kracik

Senior Software developer & DevOps Coordination Ciricle Lead with experience in graphic design #frontend #backend #devops -> kracik.sk