Today I learned
How do I version builds by git commit count in GitHub Actions?
I wanted versions nobody has to bump: <major.minor>.<commits>, the line from package.json, the patch from git rev-list --count HEAD. On CI every build came out as 0.1.1: actions/checkout clones with fetch-depth: 1, so there is one commit to count.
Fetch the whole history without file contents, which stays fast:
- uses: actions/checkout@v5
with: { fetch-depth: 0, filter: "blob:none" }
- id: version
run: |
line=$(jq -r '.version | split(".")[:2] | join(".")' package.json)
echo "game=$line.$(git rev-list --count HEAD)" >> "$GITHUB_OUTPUT"
- Docker has no
.git. Count in the workflow, passbuild-args: GAME_VERSION=${{ steps.version.outputs.game }}, read it withARG GAME_VERSION. - Guard against shallow clones. If
git rev-parse --is-shallow-repositorysaystrue, fail or show<line>-dev, never a wrong number. - A desktop client gets its own counter:
<line>.${{ github.run_number }}, written into the app'spackage.jsonbefore packaging. A re-run keeps the same number.
Set this up for my Electron game on Steam: settings now show game: 0.1.191, steam: 0.1.30, patch: 5d4112a, and every bug report carries all three.
Docs: actions/checkout