88 workflow_dispatch :
99 inputs :
1010 version :
11- description : ' Tag suffix (e.g. "3" for alpha- v0.1.3 ). Leave blank to auto-use the run number .'
11+ description : ' Patch number (e.g. "3" for v0.2.3-alpha ). Leave blank to auto-increment from the latest v0.2.x-alpha git tag .'
1212 required : false
1313 type : string
1414 update_latest :
@@ -24,29 +24,38 @@ jobs:
2424 build-and-push :
2525 runs-on : ubuntu-latest
2626 permissions :
27- contents : read
27+ # contents: write lets the workflow push the git tag that records each build,
28+ # which is also what the auto-increment reads on the next run.
29+ contents : write
2830 packages : write
2931 steps :
3032 - name : Checkout
3133 uses : actions/checkout@v4
34+ with :
35+ # Full history + tags so the auto-increment can see existing v0.2.x-alpha tags.
36+ fetch-depth : 0
37+ fetch-tags : true
3238
3339 # GHCR requires lowercase image names; github.repository preserves repo casing
3440 # ("jherforth/Monastery"), which would fail the push.
3541 - name : Compute lowercase image name
3642 id : image
3743 run : echo "name=${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
3844
39- # Manual runs still need SOME auto-incrementing fallback for the common case where you
40- # don't care about a specific number — the run number only advances when you actually
41- # click "Run workflow", so it stays meaningful now instead of ticking on every commit.
45+ # Versioning: v0.2.<patch>-alpha, matching the GitHub release tag format. With no
46+ # input, the patch is one past the highest existing v0.2.x-alpha git tag — and this
47+ # workflow pushes a tag for every build it publishes, so the counter only advances
48+ # when an image actually ships (unlike run_number, which ticked on every dispatch).
4249 - name : Compute version tag
4350 id : version
4451 run : |
4552 if [ -n "${{ inputs.version }}" ]; then
46- echo "tag=alpha-v0.1. ${{ inputs.version }}" >> "$GITHUB_OUTPUT "
53+ patch=" ${{ inputs.version }}"
4754 else
48- echo "tag=alpha-v0.1.${{ github.run_number }}" >> "$GITHUB_OUTPUT"
55+ latest=$(git tag -l 'v0.2.*-alpha' | sed -E 's/^v0\.2\.([0-9]+)-alpha$/\1/' | sort -n | tail -1)
56+ patch=$(( ${latest:--1} + 1 ))
4957 fi
58+ echo "tag=v0.2.${patch}-alpha" >> "$GITHUB_OUTPUT"
5059
5160 - name : Set up Docker Buildx
5261 uses : docker/setup-buildx-action@v3
7079 ${{ inputs.update_latest && format('{0}/{1}:latest', env.REGISTRY, steps.image.outputs.name) || '' }}
7180 cache-from : type=gha
7281 cache-to : type=gha,mode=max
82+
83+ # Record the shipped build as a git tag (skipped when the tag already exists, e.g. a
84+ # rebuild of a version that has a GitHub release).
85+ - name : Tag this build
86+ run : |
87+ tag="${{ steps.version.outputs.tag }}"
88+ if git rev-parse "refs/tags/${tag}" >/dev/null 2>&1; then
89+ echo "Tag ${tag} already exists — skipping tag push"
90+ else
91+ git config user.name "github-actions[bot]"
92+ git config user.email "github-actions[bot]@users.noreply.github.com"
93+ git tag "${tag}"
94+ git push origin "${tag}"
95+ fi
0 commit comments