Sub topic 4
Your first release workflow: push a tag, get a GitHub Release
This chapter builds the smallest useful release workflow. Pushing a version tag such as v1.2.0 creates a GitHub Release for that tag. It assumes you can read YAML, know basic git (commits, branches, tags), and have a repository where you can push tags. It also assumes you know the MAJOR.MINOR.PATCH format and the trigger and permission ideas from earlier chapters.
The workflow
name: Release
on:
push:
tags:
- 'v*.*.*'
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Create GitHub release
env:
GH_TOKEN: ${{ github.token }}
run: gh release create "$GITHUB_REF_NAME" --generate-notes --verify-tagSave this as .github/workflows/release.yml. Then create and push a tag:
git tag v1.2.0
git push origin v1.2.0A few seconds later the Releases page shows a release named v1.2.0, with notes listing the pull requests merged since the previous release.
How each part works
The trigger
on.push.tags runs the workflow when a tag matching the pattern is pushed. The pattern v*.*.* is a glob, not a regular expression. It matches v1.2.0, but it also matches vfoo.bar.baz. The next chapters cover how to reject malformed tags.
Pushing a commit to a branch does not start this workflow. Only a tag push does.
The permission
permissions: contents: write sets the access of the built-in GITHUB_TOKEN. Creating a release needs write access to repository contents. Declaring permissions removes every scope you do not list, so this job can do nothing else, such as editing issues or pull requests. If your repository's default token permissions are read-only, this line is what makes the job work.
The command
gh is the GitHub CLI, which is preinstalled on GitHub-hosted runners. It reads its credentials from the GH_TOKEN environment variable, so the step passes the workflow token in with ${{ github.token }}.
In gh release create:
"$GITHUB_REF_NAME"is the tag name, for examplev1.2.0. Runner environment variables are available in the shell, so you do not need${{ }}here.--generate-notesasks GitHub to write the release notes from merged pull requests.--verify-tagmakes the command fail if the tag does not already exist on the remote. Without it,ghcan create a new tag from the default branch, which hides mistakes.
The checkout step is not strictly needed for this command, because gh talks to the API. Keeping it lets you add build steps later.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Workflow does not start | Tag does not match `v*.*.*`, or the workflow file is not on the tagged commit | Check the tag name. Make sure the workflow file exists in the commit the tag points to. |
| `HTTP 403: Resource not accessible by integration` | Token lacks `contents: write` | Add the `permissions` block. |
| `release not found` or tag error from `--verify-tag` | Tag was not pushed to the remote | Run `git push origin <tag>`. |
| `a release with the same tag name already exists` | Re-running after a successful release | Delete the release first, or use a new tag. |
Check your understanding
Exercise
A teammate pushes the tag v2.0.0, but the workflow fails with "Resource not accessible by integration". Which line should you check first, and why?
Show answer
Check the permissions block. Creating a release needs contents: write for the GITHUB_TOKEN. If the block is missing and the repository default is read-only, or it lists only contents: read, the API call is rejected with this 403 error.
Exercise
What does --verify-tag protect you from?
Show answer
It makes gh release create fail if the tag is not already on the remote. Without it, the command can create the tag itself from the default branch, so a mistyped or unpushed tag would silently produce a release on the wrong commit.