GitLab CI/CD — Deploy to two different AppEngine instances and projects

Omisha Gupta
Oct 11, 2018 · 3 min read

We recently had to build a deployment pipeline in such a way that the latest code is deployed to two different AppEngine instances across different project. One to our sandbox environment for our internal dev teams and the other to customer’s UAT/Prod environments.

Background: GitLab uses Runner which hunts for .gitlab-ci.yml in the repository whenever there are any latest commits in the repo and executes it automatically to sync up with the changes made and reflect it on AppEngine. GitLab supports only one .gitlab-ci.yml file per repository.

So how do we workaround this problem and deploy to two instances of appengines in a single pipeline?

Requirements:

  • Google service account with editor access + generated key in JSON format
  • GitLab account with maintainer/owner access to set Environment variables

step 1: Login to GCP console, choose the relevant project you want to deploy your code in and make a service account with editor role. Generate a new private key associated with this account, and download it in JSON format. Don’t forget to download to the key we have just created.

creating a service account with private key associated to it.

step 2: Login to your GitLab project, make sure you have maintainer/owner access. Go to the setting section in the sidebar of the project followed by clicking further on CI/CD. Click on variables further to declare the Environment variables.

key1: PROJECT_ID_PRODUCTION, value: Project ID

key2: DEPLOY_KEY_FILE_PRODUCTION, value: Downloaded JSON key

This one was for deploying an application in AppEngine #1.

Key3: PROJECT_ID, value: Project ID

Key4: DEPLOY_KEY_FILE, value: Downloaded JSON key for another service account

This was for deploying an application in AppEngine #2.

we can add more keys, but we CANNOT add random names to the key. I used different keywords to store the service accounts to deploy the application to 2 different app engines.

Step 3: Make a .gitlab-ci.yml file in the branch we want the code to be pulled from for deploying in AppEngine.

The script below takes care of deploying this across multiple projects and appengine instances

image: python:2.7before_script:
— export CLOUD_SDK_REPO=”cloud-sdk-jessie”
— echo “deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main” | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
— curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
— apt-get update -y && apt-get install -y google-cloud-sdk
.bundle_project1: &bundle_project1 |
echo $DEPLOY_KEY_FILE > /tmp/$CI_PIPELINE_ID.json
gcloud auth activate-service-account — key-file /tmp/$CI_PIPELINE_ID.json
gcloud — project $PROJECT_ID app deploy
echo “Done for project 1”

.bundle_project2: &bundle_project2 |
echo $DEPLOY_KEY_FILE_PRODUCTION > /tmp/$CI_PIPELINE_ID.json
gcloud auth activate-service-account — key-file /tmp/$CI_PIPELINE_ID.json
gcloud — project $PROJECT_ID_PRODUCTION app deploy
echo “Done for project 2”
deploy_production:
stage: deploy
environment: Production
only:
— developed (BRANCH RUNNER SHOULD LOOK FOR THE CODE)
script:
— *bundle_project1
— *bundle_project2

The runner will automatically search for .gitlab-ci.yml in the declared branch and will sync the latest code whenever there are push requests on GitLab.

Have a joyful pipelining. ;)

Searce Engineering

We identify better ways of doing things!

Omisha Gupta

Written by

More than high time, its cloud time!

Searce Engineering

We identify better ways of doing things!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade