# 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.

URL: https://levelrail.com/learn/ci-cd/version-and-tag-releases/
Updated: 2026-11-27

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](https://levelrail.com/learn/ci-cd/build-and-push-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](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/ci-cd/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](https://levelrail.com/learn/git/git-workflow-that-triggers-a-deploy/)) 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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Pushed tag but no workflow ran | Tag pattern does not match, or file not on that commit | Check `tags:` and that the workflow exists at the tagged commit |
| `error: src refspec v1.2.0 does not match any` | Tag was never created locally | Run `git tag -a ...` first |
| `fatal: tag 'v1.2.0' already exists` | Tag name reused | Choose a new version, do not overwrite |
| `HTTP 403: Resource not accessible by integration` on release | Token lacks contents: write | Add the permission in the release workflow |
| Image tag missing the version | Not a semver-looking tag | Use `v1.2.0` style tags |
| Changelog is empty | No merged PRs since the last tag | Write notes by hand |
| Tag shows the wrong commit | Tagged before pulling | Fix 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
```

## Related lessons

- [Build and push a Docker image to GHCR](https://levelrail.com/learn/ci-cd/build-and-push-docker-image-to-ghcr/)): the image part of this workflow.
- [Git remotes: push, pull and fetch](https://levelrail.com/learn/git/git-remotes-push-pull-fetch/)): how pushes and tags reach the remote.
- [Undo mistakes in Git](https://levelrail.com/learn/git/undo-mistakes-in-git/)): fixes that do not rewrite published history.
- [Git workflow that triggers a deploy](https://levelrail.com/learn/git/git-workflow-that-triggers-a-deploy/)): the branch flow before the tag.
- [Rollback and release strategy in a pipeline](https://levelrail.com/learn/ci-cd/rollback-and-release-strategy-in-a-pipeline/)): what to do when a release is bad.

## Next

Continue with [Rollback and release strategy in a pipeline](https://levelrail.com/learn/ci-cd/rollback-and-release-strategy-in-a-pipeline/)).

## 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

- [GitHub Docs, managing releases in a repository](https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository)
