Deploying to Google App Engine with GitLab's CI
There are some useful articles out there on setting up Continuous Delivery from GitLab’s CI to Google App Engine, but the ones I found are pretty old (the cloud changes quickly!) and, while the overall concepts are still valid, little things here and there need to be worked out manually. Here’s my attempt at showing a (working on my machine ) testing and deployment pipeline that’s up to date with the latest requirements.
Step 1 - Create a service account
- In the IAM & Admin > Service Accounts panel of your Cloud Console, click the Create service account button at the top.
- Give it a name and ID (
gitlab-cd
, for example), a description (GitLab CD
, maybe) and Create it. - Give it these four roles and Continue:
- App Engine > App Engine Deployer
- App Engine > App Engine Service Admin
- Cloud Build > Cloud Build Editor
- Storage > Storage Object Admin
- Use the Create key button to generate a JSON key. Save the file you’ll be prompted to download in a safe location, this is an access token.
- Complete the configuration clicking the Done button.
Step 2 - Configure the CI variables
- In your GitLab repo go into Settings > CI/CD and expand the Variables section.
- Create two new variables:
- Type: File, Key:
GAE_DEPLOY_KEY
, Value: the raw contents of the JSON key you’ve downloaded above. - Type: Variable, Key:
GAE_PROJECT_ID
, Value: your GCP project’s ID. - Save variables.
Step 3 - Add a cleanup script
Optional but suggested.
When you deploy a new version of your code on GAE, the old one will be deactivated as your deployment strategy dictates. Since when you’ll reach the limit of disabled version you won’t be able to deploy newer ones anymore, you might want to add this little script that cleans up older versions to your repo. I made small modifications to almcc.me‘s script and staged it as .gitlab/delete_old_deployments.sh
in my tree. Remember to chmod +x
it!
1 |
|
Step 4 - Configure the CI pipelines
Create or edit the .gitlab-ci.yml
file in the root of your repo. If you don’t have already setup the CI process, use the following snippet as a template; add your testing stages however you see fit (because you do have tests to run, right?).
1 | Deploy to GAE: |
Step 5 - Profit
Commit your changes and check that everything rolls correctly! If something changed and it doesn’t, let me know and I’ll readily update this post.