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.
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.
As pointed out by readers of the article, the role in the service account below does not need to be “Editor” but can instead be set to only “App Engine Deployer”.
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.
“private_key”: “ — — -BEGIN PRIVATE KEY — — -\nVIIEvwIBADANBgkqhkiG9w0BAOEFAASCBKkwggSlAgEAAoIBAODK6pryUY0g5htF\n
yiK+aiA9tXVCk7APwTg+\nSzVPVi2007FzF0K9vZDTs9yvl+a8H57gkjL9RoVGNSHYuUUdEUExvyCHj57/fllq\nhvgEXOe1ARtEyxO4enSfXIVAYg==\n — — -END PRIVATE KEY — — -\n”,
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:
Click on service account on left pane and select newly created accountmedium.com
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.
- PROJECT_ID_PRODUCTION: your GAE project id (e.g. ordinal-cacao-141615)
- DEPLOY_KEY_FILE_PRODUCTION: copy/paste the content of that JSON file.
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.