Chapter 12 · Lesson 9 of 10
CI/CD and automation

Version and tag releases with semver, git tags and gh release

Releasing by tag means you pick a semantic version, create an annotated git tag such as v1.2.0, push it, and let a workflow build, tag and publish that exact commit.

By the end you will

Cut a release by pushing an annotated version tag that triggers a workflow, tags the image and creates a GitHub release.

Time
20 min
Level
Intermediate
Check
4 questions
You need
  • A GitHub repository with an image build workflow
  • The gh CLI, or use the GitHub web UI

A release is a named point in your history that you can build again exactly. In Git that name is a tag, and in a pipeline the tag becomes the trigger: push v1.2.0, and a workflow builds the image, tags it 1.2.0 and publishes a release. This lesson covers the version number, the tag commands, the workflow and the changelog. It extends the workflow from Build and push a Docker image to GHCR.

How it works#

Semantic versioning writes versions as MAJOR.MINOR.PATCH, for example 1.4.2. You raise MAJOR when you break compatibility, MINOR when you add something compatible, and PATCH when you fix something compatible. A git tag attaches that version to one commit. A push of that tag fires the workflow, which reads the tag name from the git ref and uses it to name the image and the release. Because the tag fixes the commit, the artifact is reproducible. The image tag glossary entry covers the container side.

Step 1: Choose the version#

Look at what changed since the last tag:

  • breaking change to a public API, config format or behavior people rely on: bump MAJOR (2.0.0)
  • new feature that keeps existing use working: bump MINOR (1.5.0)
  • fix only: bump PATCH (1.4.3)

Before 1.0.0, many projects treat the rules loosely. For an app that is not a library, pick a rule you can explain and stick to it. A v prefix on the tag (v1.4.3) is a common convention and is what the workflow pattern below expects.

Step 2: Create an annotated tag#

Make sure you are on the commit you want to release and that it passed CI:

bash
git checkout main
git pull
git tag -a v1.2.0 -m "Release 1.2.0"
git show v1.2.0

-a makes an annotated tag, a real Git object with the author, date and message. Releases should use annotated tags, not lightweight ones. git show v1.2.0 prints the tag and the commit it points at. Check that it is the commit you meant.

Step 3: Push the tag#

bash
git push origin v1.2.0

A plain git push does not send tags. Push one tag by name. Avoid pushing all tags at once, because old local tags you forgot about would be published too. The push is also the trigger, so do it only when you are ready to release.

Step 4: Trigger a workflow on the tag#

Extend the workflow to react to version tags. This one builds the image and creates a GitHub release:

yaml
name: release

on:
  push:
    tags: ['v*']

permissions:
  contents: write
  packages: write

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: docker/setup-buildx-action@v3

      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Lowercase image name
        run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"

      - id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.IMAGE }}
          tags: |
            type=semver,pattern={{version}}
            type=sha

      - uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

      - name: Create GitHub release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh release create "$GITHUB_REF_NAME" --generate-notes --title "$GITHUB_REF_NAME"

contents: write is needed to create the release (see Secrets and variables in CI pipelines on scoping the token), so it sits here and not in your test workflow. fetch-depth: 0 fetches full history so tools that read it can see earlier tags. The metadata action turns v1.2.0 into the image tag 1.2.0, and also adds the SHA tag. The gh release create command is available on GitHub-hosted runners.

Step 5: Build the changelog#

A changelog tells users what changed. Three options:

  • Generated notes. --generate-notes lists merged pull requests since the previous release.
  • Conventional commits. If your commit messages follow a pattern such as feat: add export and fix: handle empty input, tools can group them into features and fixes and even suggest the next version. That works only when the team follows the pattern consistently.
  • Hand-written notes. Short and human, best for user-facing changes. Pass them with --notes-file CHANGELOG-1.2.0.md.

Whichever you pick, and whatever Git workflow you follow, keep it based on what changed for the user, not a raw commit dump.

Check it worked#

After the push, open the Actions tab and watch the release run for the tag. When it finishes, the repository’s Releases page should list v1.2.0 with notes. The package page should show the 1.2.0 image tag. Verify from a server:

bash
docker pull ghcr.io/myorg/myapp:1.2.0

Replace the owner and image with yours. Also run git ls-remote --tags origin to see the tag on the remote.

Edge cases and gotchas#

  • Do not move or delete a published tag. People have pulled it, images and caches were built from it, and release notes point at it. If a release is bad, ship v1.2.1 instead. Fix forward.
  • Tag the right commit. A tag on a commit that never passed CI releases untested code. Tag from the main commit that is green.
  • Deleting a remote tag needs a deliberate command (git push origin --delete v1.2.0), and it does not unpublish images. Treat it as a last resort, and only for a tag nobody has used.
  • Tag pattern too loose. 'v*' also matches vnext. Use a stricter glob such as 'v[0-9]*' if you have odd tag names.
  • Release workflows need write rights. Keep contents: write out of workflows that run on pull requests.
  • Pre-releases. Tags like v1.3.0-rc.1 are valid semver pre-releases. Pass --prerelease to gh release create for them.

Troubleshooting#

SymptomLikely causeFix
Pushed tag but no workflow ranTag pattern does not match, or file not on that commitCheck tags: and that the workflow exists at the tagged commit
error: src refspec v1.2.0 does not match anyTag was never created locallyRun git tag -a ... first
fatal: tag 'v1.2.0' already existsTag name reusedChoose a new version, do not overwrite
HTTP 403: Resource not accessible by integration on releaseToken lacks contents: writeAdd the permission in the release workflow
Image tag missing the versionNot a semver-looking tagUse v1.2.0 style tags
Changelog is emptyNo merged PRs since the last tagWrite notes by hand
Tag shows the wrong commitTagged before pullingFix forward with a new version, do not move the tag

Quick reference#

bash
git tag -a v1.2.0 -m "Release 1.2.0"
git show v1.2.0
git push origin v1.2.0
git ls-remote --tags origin
gh release create v1.2.0 --generate-notes

Next#

Continue with Rollback and release strategy in a pipeline.

Check what you learned4 questions
1A release fixes a bug without changing the API. Which part of 1.4.2 increases?
2Which command creates an annotated tag?
3Does git push send your tags automatically?
4Why is moving a published tag a bad idea?

Frequently asked questions

What is semantic versioning?

A version number in the form MAJOR.MINOR.PATCH. Increase MAJOR for breaking changes, MINOR for backward-compatible features and PATCH for backward-compatible fixes.

What is the difference between a lightweight and an annotated git tag?

A lightweight tag is just a name pointing at a commit. An annotated tag is a full object with a tagger, date and message, created with git tag -a, and it is the right choice for releases.

How do I trigger a GitHub Actions workflow when I push a tag?

Use on push with tags set to a pattern such as 'v*'. Pushing a tag like v1.2.0 then starts the workflow for that tag.

Can I delete or move a release tag?

You can technically, but you should not. Anyone who already pulled the tag, and any image or package built from it, still refers to the old commit, so moving it creates confusion and breaks reproducibility.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios