Skip to content

Instantly share code, notes, and snippets.

@bcoughlan
Created April 11, 2025 22:43
Show Gist options
  • Save bcoughlan/8dfbcf7dec613c5824359f06a9381c81 to your computer and use it in GitHub Desktop.
Save bcoughlan/8dfbcf7dec613c5824359f06a9381c81 to your computer and use it in GitHub Desktop.
Continuous versioning: headver in GitHub Actions
name: Build
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
# This is needed to enable computing the next tag
fetch-depth: 0
- name: Version and tag
id: version
run: .github/headver.sh
- name: Build and push image
run: |
docker build -t my/image:${{ env.VERSION }} .
docker push my/image:${{ env.VERSION }}
#!/bin/bash
# Enable Bash strict mode
set -euox pipefail
# Continuous delivery versioning inspired by https://github.com/line/HeadVer
# Format is <head>.YYYYMMDD.<build>
# For hotfix, a branch should be created off the tag to hotfix as hotfix/<tag>, e.g. hotfix/1.20230821.1.
# Hotfix tags will be versioned as <tag>-hotfix-<n>, where <n> is an incremental number starting from 1.
head=1
function getNextNumber() {
PATTERN=$1
latestTag=$(git tag -l ${PATTERN} | sort -V | tail -n 1)
if [ -z "$latestTag" ]; then
echo "1"
else
build=$(echo $latestTag | awk -F'[.-]' '{print $NF}')
echo "$((build + 1))"
fi
}
if [[ $(git symbolic-ref --short HEAD) == hotfix/* ]]; then
suffix=$(git symbolic-ref --short HEAD | sed 's/^hotfix\///')
n=$(getNextNumber "${suffix}-hotfix-*")
version="${suffix}-hotfix-${n}"
else
ymd=$(date +"%Y%m%d")
build=$(getNextNumber "${head}.${ymd}.*")
version="${head}.${ymd}.${build}"
fi
echo "Tag: $version"
git tag "$version"
git push origin $version
echo "VERSION=${version}" >> $GITHUB_ENV
echo "version=${version}" >> $GITHUB_OUTPUT
@bcoughlan
Copy link
Author

Versioning system for continuous delivery, inspired by https://github.com/line/headver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment