Yannick Clybouw
Blog about Google Cloud
Google App Engine Pro Tip: Use Git Commit Hash as Version
Google App Engine (GAE) is a serverless approach to quickly ship your code. Typically, when you want to deploy, you only need to run one command:
gcloud app deploy
In the Google Cloud Console, you will see a new version of your app appearing, the traffic will be migrated to that new version, and the old version will be shut down.
Article continues below the image...
By default, versions have a timestamp as name, e.g. 20201204t131734. However, when you want to know what code actually has been deployed, you can only guess it based on that timestamp.
A better approach would be to use your Git commit hash as version. The time of deploy is known anyway (see 5th column in the image). In bash, you can do:
commit_hash=`git rev-parse --short HEAD`
gcloud app deploy --version=$commit_hash
In this example, commit_hash is the short version of the hash of the commit you have currently checked-out.
Of course, this would not make sense if you still have uncommitted changes. To block this, you could use a deploy script, named e.g. deploy.sh, with following content:
#!/usr/bin/env bash
set -e
if [[ -n "$(git status --porcelain)" ]]
then
echo "You have uncommitted changes. Aborting..."
exit 1
fi
commit_hash=`git rev-parse --short HEAD`
gcloud app deploy --version=$commit_hash
Now you know exactly what code has been deployed based on the version name.
There is one remaining caveat: you can still have ignored files. To avoid this, make sure your .gcloudignore file contains at least the patterns of your .gitignore file.
Update: there is a bug at Google (see issue tracker) sometimes causing the version to be parsed as a float, resulting in the deploy to fail. Example: 367e5893 is interpreted as 367·10⁵⁸⁹³ and further cast to Infinity. Workaround is to add a prefix for each version (e.g. v-367e5893).