/dev/urandom

/dev/urandom

Pseudorandom thoughts generator.

I'm Niccolò Maggioni.
Student, geek and developer.

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

  1. In the IAM & Admin > Service Accounts panel of your Cloud Console, click the Create service account button at the top.
  2. Give it a name and ID (gitlab-cd, for example), a description (GitLab CD, maybe) and Create it.
  3. 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
  4. 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.
  5. Complete the configuration clicking the Done button.

Step 2 - Configure the CI variables

  1. In your GitLab repo go into Settings > CI/CD and expand the Variables section.
  2. Create two new variables:
    1. Type: File, Key: GAE_DEPLOY_KEY, Value: the raw contents of the JSON key you’ve downloaded above.
    2. Type: Variable, Key: GAE_PROJECT_ID, Value: your GCP project’s ID.
  3. 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!

.gitlab/delete_old_deployments.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# ./delete_old_deployments.sh {PROJECT_ID} {SERVICE_ID} {VERSIONS_TO_KEEP}

VERSIONS=$(gcloud --project "$1" app versions list --service "$2" --sort-by '~version' --format 'value(version.id)')
COUNT=0
echo "Deleting old versions of the '$2' service (keeping the $3 most recent versions)"
for VERSION in $VERSIONS; do
((COUNT++))
if [ $COUNT -gt $3 ]; then
echo ".../!\\... Deleting version $VERSION"
gcloud --quiet --project "$1" app versions delete "$VERSION" --service "$2"
else
echo "......... Keeping version $VERSION"
fi
done

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
2
3
4
5
6
7
8
9
10
11
12
Deploy to GAE:
# stage: deploy
# only:
# - master
image: google/cloud-sdk:alpine
environment:
name: GAE
url: https://YOUR-PROJECT-ID.appspot.com
script:
- gcloud auth activate-service-account --key-file "$GAE_DEPLOY_KEY"
- ./.gitlab/delete_old_deployments.sh "$GAE_PROJECT_ID" default 2
- gcloud --quiet --project "$GAE_PROJECT_ID" app deploy

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.

Share this