An easy guide to automatically deploy your Google App Engine project with Gitlab CI

Continuous Integration is generally considered to be a good thing. Effective. Efficient. And makes your updates smooth and safe. But that’s none of the reasons for why I did this.

No attribution needed, I single handedly crafted this collage

I did this simply because I got carried away. I’ve recently started to use Gitlab for my project hostings and I’ve probably spent a little bit of an unhealthy amount of time and joy playing around with it. One of the things that really had me excited was the built in continuous integration (CI).

I found that there were lots of good articles on how to get setup with automatic build for Node.js, Jekyll sites etc. But I was lacking a complete guide for deploying to Google App Engine (GAE). So here it goes.

GAE Developer Console

Assuming that you already have a GAE project, or at least know enough about it to just skip ahead, all you’ll need is a service account and your project id to get all of this rolling.

In this example, I am using the project id “ordinal-cacao-141615”. Replace that random name with whatever your project is called.

Then go to the “IAM & Admin” menu and create a service account that is having “Editor” role in your project. Name the account to something that makes sense for you. In this case, I only use the account specifically for the automatic deployment.

Service account for Gitlab CI deployment access

Pay attention to the key file. Create a JSON export, download it and store it temporarily. The content of the file looks something like below. You’ll need this for the Gitlab configuration. Don’t store it anywhere else! Delete the file on disk.

{
“type”: “service_account”,
“project_id”: “ordinal-cacao-141615”,
“private_key_id”: “02cca4dd86b0f210ae696564e5d41470b4255c89”,
“private_key”: “ — — -BEGIN PRIVATE KEY — — -\nVIIEvwIBADANBgkqhkiG9w0BAOEFAASCBKkwggSlAgEAAoIBAODK6pryUY0g5htF\n
...
TL;DR
...
yiK+aiA9tXVCk7APwTg+\nSzVPVi2007FzF0K9vZDTs9yvl+a8H57gkjL9RoVGNSHYuUUdEUExvyCHj57/fllq\nhvgEXOe1ARtEyxO4enSfXIVAYg==\n — — -END PRIVATE KEY — — -\n”,
“client_email”: “gitlab-ci@ordinal-cacao-141615.iam.gserviceaccount.com”,
“client_id”: “109498122000701669157”,
“auth_uri”: “https://accounts.google.com/o/oauth2/auth",
“token_uri”: “https://accounts.google.com/o/oauth2/token",
“auth_provider_x509_cert_url”: “https://www.googleapis.com/oauth2/v1/certs",
“client_x509_cert_url”: “https://www.googleapis.com/robot/v1/metadata/x509/gitlab-ci%40ordinal-cacao-141615.iam.gserviceaccount.com"
}

Update 2016–09–23

I got some awesome troubleshooting guidelines from reader Abbie, who ran into some problems with authentication during deployment. Please see the conversation thread that lead up to this guide:

Gitlab

Time to start setting things up in Gitlab. Head on over to the environment variable section in your Gitlab project and define the following two variables for the information that you got from your GAE project.

  1. PROJECT_ID_PRODUCTION: your GAE project id (e.g. ordinal-cacao-141615)
  2. DEPLOY_KEY_FILE_PRODUCTION: copy/paste the content of that JSON file.
Screenshot of the Gitlab project variables

Now, all you have to do is simply to copy/paste all the code from the gist below into your .gitlab-ci.yml file. It will just work! Please pay attention to the choice of debian distribution for the cloud SDK installation package. You should not need to make any changes to this, but if you have special needs, then you can have look at the Debian distribution list and make the choice which suites you better.

Now, simply submit that Gitlab CI configuration file and watch how your build instantly triggers and in a couple of minutes you’ll find your app automatically deployed to Google App Engine.

For extra credits, you should probably set up the configuration file with additional build rules, branch tags, staging environment etc. All those things that might apply to your specific project.