Sub topic 8
Attaching Build Artifacts to a Release
This chapter assumes you can read workflow YAML and have the tag-triggered release workflow from the earlier chapter working. You also need basic shell skills, such as running tar and quoting variables.
A GitHub Release can carry files, called assets. Users download these instead of cloning the repository. Typical assets are a compressed archive, a compiled binary, or a checksum file. This chapter adds a build step and an upload step to the release workflow.
The working model
The release workflow creates the release first. Then it builds the files and uploads them to that release by tag name. Each file becomes one asset.
The upload step runs after the release exists, so the tag must already point to a release. Both steps use the gh command-line tool, which is preinstalled on GitHub-hosted runners.
Building the archive
This step packages the repository into a compressed file named after the tag:
- name: Build
run: |
mkdir -p dist
tar -czf "dist/app-$GITHUB_REF_NAME.tar.gz" --exclude=.git --exclude=dist .Two details matter here:
--exclude=diststopstarfrom trying to include the archive it is writing.--exclude=.gitkeeps the repository history out of the download.
Putting the version in the file name, such as app-v1.2.0.tar.gz, lets users tell downloads apart. Replace the tar line with your real build command, such as a compiler or a packaging tool, when your project needs one.
Uploading the asset
This step attaches the file to the release:
- name: Upload asset to release
env:
GH_TOKEN: ${{ github.token }}
run: gh release upload "$GITHUB_REF_NAME" "dist/app-$GITHUB_REF_NAME.tar.gz" --clobbergh release upload takes the tag name, then one or more file paths. The GH_TOKEN variable gives gh the workflow's token. That token needs contents: write, which the workflow already grants at the top level.
The --clobber flag replaces an existing asset with the same name. Without it, a re-run of the job fails because the asset is already there. With it, a re-run overwrites the old file with the new build.
Putting it together
The new steps go at the end of the steps list in the release job. The job then reads in this order: checkout, validate the tag, create the release, build, upload. Keep the build after the release creation so a failed build leaves a release you can inspect, rather than a tag with nothing attached.
If you would rather not publish a release until the files are ready, you can build first and pass the files to gh release create directly. That trade-off is covered in the chapter on release safety.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `release not found` on upload | The upload ran before the release existed, or the tag name is wrong | Run the upload after the create step and check `$GITHUB_REF_NAME` |
| `asset under the same name already exists` | A re-run without `--clobber` | Add `--clobber` |
| `HTTP 403` or `Resource not accessible by integration` | The token lacks `contents: write` | Set `permissions: contents: write` |
| `file not found` | The path in the upload step does not match the build output | Print the directory with `ls dist` to compare |
Checksums
You can attach more than one file. A checksum file lets users verify a download:
- name: Checksum
run: sha256sum "dist/app-$GITHUB_REF_NAME.tar.gz" > "dist/app-$GITHUB_REF_NAME.sha256"
- name: Upload files
env:
GH_TOKEN: ${{ github.token }}
run: gh release upload "$GITHUB_REF_NAME" dist/* --clobberThe shell expands dist/* into every file in the folder, so both the archive and the checksum are uploaded.
Exercise
A release job fails on its second run with an error saying the asset already exists. The first run uploaded dist/app-v1.2.0.tar.gz. What change fixes it, and what does that change do?
Show answer
Add --clobber to the gh release upload command. It replaces an existing asset of the same name, so a re-run overwrites the old file instead of failing.
Exercise
Why does the build step use --exclude=dist when it runs tar over the current directory and writes into dist?
Show answer
The archive is written inside the directory being archived. Without the exclusion, tar would try to include the partly written archive in itself.