How to Setup Gitlab CI Pipeline For Google Cloud App Engine?

Tapendra Dev
3 min readApr 14, 2021
GitLab +Google App Engine

So we will be completing the Automation in 3 Steps:

  1. Creating the Service Account or Already using the Existing one.
  2. Configure the Gitlab CL Settings.
  3. Adding Required Script in the Exiting code.
  4. All Set for Automation!

Service Account

First go to your GCP Console and navigate to IAM & Admin then to the Service Accounts. So for the new GCP Project the service account automatically get assigned or if you want give the access to the another account then you can create the new one. Now we have two cases:

Case1: Default Service account

Check the existing Service Account

Now you need to go to key tab and then create the JSON Key (That is require for Gitlab CI Configuration)

Create the JSON Key

Case2 : Creating New Service Account

  1. Create the new service account and assign the App Engine Admin (This is required for the giving the rights to Gitlab)
  2. Then create the JSON key in the same way mentioned above.
Create the New Service Account

Now visit the Storage and go to the default bucket for the App Engine with name:

  1. staging.PROJECT-ID.appspot.c
  2. us.artifacts.PROJECT-ID.appspot.com

Then add the service account as the member of this bucket while giving the permissions of Storage Object Creator and Storage Object Viewer.

Adding Service Account as Member

Last Step to enable the Cloud-build and App Engine Admin API with the associated billing account.

Configuring the Gitlab CI

Now visit the setting tab of repo and navigate to CI/CD then find the variable section.

Add 2 Variable to Gitlab

Now add two variable with name PROJECT_ID and SERVICE_ACCOUNT:

PROJECT_ID: Put the ID from Project Setting page.

SERVICE_ACCOUNT: Copy and paste the JSON key which was downloaded while creating the service account key.

Now create the .gitlab-ci.yml file in the root of your code.

image: google/cloud-sdk:alpine

deploy_production:
stage: deploy
environment: Production
only:
- master
script:
- echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
- gcloud --quiet --project $PROJECT_ID app deploy app.yaml dispatch.yaml

deploy_staging:
stage: deploy
environment: Staging
only:
- staging
script:
- echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
- gcloud --quiet --project $PROJECT_ID app deploy staging-app.yaml

after_script:
- rm /tmp/$CI_PIPELINE_ID.json

You can change the branch name for Production and Staging environment as per your requirements.

Now push the .gitlab-ci.yml file to the repo after eliminating it from .gitignore file and you are good to go.

After successful code push check the CI/CD on the Gitlab project you can find the JOB is created also you can check the logs from there.

Congo so from now onwards Git Push will now automatically going to deploy the code to respective environment on the App Engine.

All Done!

--

--

Tapendra Dev

Founder & CEO of Secure Blink — Heuristic AppSec Management Platform | Cybersecurity Researcher | Serial Entrepreneur