If you ever find yourself needing to compare different release versions of your source code, you’re in luck! You can automatically create tags in your Git repo during your CI/CD builds and use those to assess code variations across different environments or pinpoint the exact code that triggered an error in production. Moreover, these tags can even be aligned with the data provided by your cloud monitoring tools, making it a breeze to correlate information and gain powerful insights into a problem.
Fortunately, it is easy to automate the creation of tags during a GitHub Action. My preference is to create two types of tags. The first type is a tag with build number information that will never change and point to when a version was released, i.e. {environment}-{run_number}-{run_attempt}
. For the second type, I keep a tag that always indicates what is currently deployed to an environment, i.e. {environment}-curr
.
There are only two simple build steps to get this done. The first step, get-build-version
, constructs a nicely formatted build number that includes the environment name and can be used in other build steps. The second step, create-git-tags
, uses the GitHub CLI to:
- Create the static build number tag on the current commit.
- Remove the current environment tag if it exists on a different commit.
- Add a new current environment tag pointing to the current commit.
#file: .github/workflows/deploy-app.yml
- id: get-build-version
name: get-build-version
uses: azure/cli@v1
with:
inlineScript: |
echo build=$(printf "${{inputs.environment}}-%05g-%03g" ${{ github.run_number }} ${{ github.run_attempt }}) >> $GITHUB_OUTPUT
- name: create-git-tags
env:
GH_TOKEN: ${{ github.token }}
run: |
set -e
gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/${{ github.repository }}/git/refs -f ref='refs/tags/${{ steps.get-build-version.outputs.build }}' -f sha='${{ github.sha }}'
gh api --method DELETE -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/${{ github.repository }}/git/refs/tags/curr_${{inputs.environment}} || true
gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/${{ github.repository }}/git/refs -f ref='refs/tags/curr_${{inputs.environment}}' -f sha='${{ github.sha }}'
With this, I can now easily compare versions and environments. Here are some example git commands that I use regularly. Don’t forget to git pull --tags
.
#My current development code vs QA
#Determine why my local code isn't working as expected but QA is
git diff qa-curr
#QA Code vs Production
#Determine why QA is broken but Production works
git diff qa-curr prd-curr
#Old Production vs Current Production
#Determine what changed when new errors started to show up in production
git diff prd-99999-999 prd-curr
Building off of an earlier post, you can see the entire process in action here.