Automatically deploy to Google App Engine with Gitlab CI

Automate the boring task 😁 of deployment

gitlab ci + appengine

All we have to do is:

  1. Create a service account (I assume you know about it)
  2. Setup Gitlab CI
  3. Magic happens!

Creating a service account

Visit iam-admin page and create the service account with required permissions and create the key.

create service account to auto deploy
  1. App Engine Admin (This is required only if your using dispatch.yaml)
  2. App Engine Deployer and App Engine Service Account (Ignore this both,if you have given 1st permission)
  3. Create the JSON key (You will need this in Gitlab configuration)
create the JSON key

Now visit storage and go to buckets:

  1. staging.PROJECT-ID.appspot.com
  2. us.artifacts.PROJECT-ID.appspot.com (Create bucket with this name, if you don’t have one)

Now add your service account as a member of this buckets and give permissions Storage Object Creator and Storage Object Viewer.

adding service account as a member

One final step in Google Cloud Developer console.

Enable cloud-build api and you will need to have billing account linked to enable this api.

Setup Gitlab CI

We will deploy master branch to production and staging branch to staging.

Visit CI/CD settings of your gitlab project.

add variable to gitlab

Let’s add two variable PROJECT_ID and SERVICE_ACCOUNT

SERVICE_ACCOUNT: Put the data of JSON key which we have downloaded while creating service account.

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

code

Changes of master and staging branches will deploy to production and staging respectively.
I have used dispatch.yaml file, if you don’t have one just remove it from the script as well.

Push the .gitlab-ci.yml in your repo and your are done.

Source code: https://github.com/mryogesh/yogesh 
In case if you want to see staging-app.yaml and dispatch.yaml file.

Magic 🎩

Now, git push automatically deploys your code to staging and production.

You have mastered your spell