Sub topic 17
Creating releases automatically with GitHub Actions
This chapter assumes you can create and push tags, and that you know how to create a release with gh release create. You also need basic familiarity with YAML. Here you will let GitHub Actions create the release whenever you push a version tag.
The idea
Creating a release by hand is easy to forget or do inconsistently. A workflow can do it for you. You push a tag, GitHub runs the workflow, and the workflow packages your files and publishes a release for that tag.
The workflow file
Save this as .github/workflows/release.yml.
name: Release
on:
push:
tags:
- 'v*'
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Package source
run: tar -czf "app-${GITHUB_REF_NAME}.tar.gz" src
- name: Create release
env:
GH_TOKEN: ${{ github.token }}
run: gh release create "$GITHUB_REF_NAME" --generate-notes "app-${GITHUB_REF_NAME}.tar.gz"How it works
on.push.tagswith'v*'runs the workflow only when you push a tag that starts withv. Ordinary branch pushes do not trigger it.permissions: contents: writelets the built-in token create releases. Without it, the default token is often read-only and the release step fails with a permissions error.GITHUB_REF_NAMEholds the name of the tag that triggered the run, such asv1.1.0. The workflow uses it for both the archive name and the release name.ghis preinstalled on GitHub-hosted runners. It reads the token fromGH_TOKEN.--generate-notesbuilds the release notes from merged pull requests and commits since the previous release. The last argument is a file to attach as an asset.
Trying it
- Commit the workflow file and push it to your default branch.
- Create and push a tag:
git tag -a v1.1.0 -m "Release 1.1.0"
git push origin v1.1.0- Open the Actions tab to watch the run. When it succeeds, the release appears under Releases.
Common errors
- The workflow never runs. The tag name does not match the pattern, or the workflow file was not on the commit the tag points to. GitHub reads the workflow from the tagged commit.
HTTP 403: Resource not accessible by integration. Thepermissionsblock is missing or too narrow.release already exists. You re-ran the job for a tag that already has a release. Delete the release first, or usegh release upload --clobberto replace assets.- Push all tags at once.
git push --tagscan start one run per new tag. Push tags one at a time when you want to control this.
Exercise
Your workflow triggers on v* tags, but the release step fails with "Resource not accessible by integration". What is the most likely cause and the fix?
Show answer
The built-in token does not have write access to repository contents. Add permissions: contents: write to the workflow (or to the job), then re-run it.
Exercise
Why does the example use "$GITHUB_REF_NAME" instead of typing the version into the workflow?
Show answer
The variable holds the name of the tag that triggered the run. The same workflow then works for every version tag, and you never edit it per release.