Sub topic 7
Pre-releases and rejecting malformed tags
This chapter assumes you can read a GitHub Actions workflow, you know the vMAJOR.MINOR.PATCH scheme from the earlier chapters, and you have a working tag-triggered release workflow. Here you will extend that workflow in two ways. It will refuse tags that are not valid versions, and it will mark release candidates as pre-releases.
Why the trigger pattern is not enough
The trigger filter v*.*.* uses glob matching, not version parsing. It accepts v1.2.3, but it also accepts vfoo.bar.baz and v1.2.3.4. A typo in a tag would then publish a release with a meaningless name.
The fix is to loosen the trigger to v* and validate the tag name inside the job. Failing early keeps a bad tag from producing a release.
Pre-release versions
Semantic versioning lets you add a suffix after a hyphen: v1.3.0-rc.1, v2.0.0-beta.2. A version with a suffix sorts before the plain v1.3.0. It signals that the build is a candidate and may still change.
GitHub has a matching flag. A release marked as a pre-release is labeled as such on the releases page and is not treated as the repository's "latest" release. You set it with gh release create --prerelease.
The workflow
name: Release
on:
push:
tags:
- 'v*'
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Validate tag format
run: |
if [[ ! "$GITHUB_REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "Tag $GITHUB_REF_NAME is not a valid semantic version" >&2
exit 1
fi
- name: Create release
env:
GH_TOKEN: ${{ github.token }}
run: |
flag=""
if [[ "$GITHUB_REF_NAME" == *-* ]]; then
flag="--prerelease"
fi
gh release create "$GITHUB_REF_NAME" --generate-notes --verify-tag $flagHow it works
The workflow has two steps that matter.
- Validation. The regular expression requires a leading
v, three numeric parts separated by dots, and an optional suffix made of letters, digits, dots, and hyphens. If the tag does not match, the step prints a message to standard error and exits with status 1. That fails the job, and the release step never runs. - Pre-release detection. A valid tag that contains a hyphen has a suffix, so the script sets
--prerelease. For a plain tag such asv1.3.0,flagstays empty.
The $flag variable is deliberately unquoted in the last command. When it is empty, the shell drops it. If you quoted it, gh would receive an empty string as an argument and fail.
The table shows how some tags behave.
| Tag | Valid? | Result |
|---|---|---|
| `v1.2.0` | Yes | Regular release |
| `v1.3.0-rc.1` | Yes | Pre-release |
| `v2.0.0-beta` | Yes | Pre-release |
| `1.2.0` | Not triggered | No `v` prefix, so the `v*` filter does not match |
| `v1.2` | No | Job fails at validation |
| `v1.2.3.4` | No | Job fails at validation |
Common mistakes
- Relying only on the trigger glob.
v*.*.*lets malformed names through. Validate in the job. - Using
[0-9]ranges loosely. The pattern^v[0-9]+\.anchors at the start, and the$anchors the end. Without both anchors,xv1.2.3-junk!style names could pass. - Treating any hyphen as a pre-release. This is safe here only because validation runs first. Without it, a tag like
v1-finalwould be marked as a pre-release.
Exercise
A teammate pushes the tag v1.4.0-rc.2. Does the workflow above pass validation, and is the release marked as a pre-release? What happens if they push v1.4 instead?
Show answer
v1.4.0-rc.2 matches the pattern (three numeric parts plus the suffix -rc.2). It contains a hyphen, so --prerelease is added and the release is marked as a pre-release. v1.4 has only two numeric parts, so the regular expression fails. The validation step exits with status 1, the job fails, and no release is created.