Version a build and pass it between jobs
In this task you'll give every build a version number, build it once and hand the same file to later jobs.
Build once, deploy many: the file you tested must be the file you release. Build it in one job, upload it as an artifact, and have later jobs (deploy to test, then production) download that exact file rather than building again. Artifacts last 90 days by default; set retention-days lower for big ones.
Give each build a version. A common pattern is semantic versioning from Git tags: pushing tag v1.4.0 runs the release workflow, which names the artifact with that version and creates a GitHub release. Builds that aren't releases can use the short commit SHA, so any file can be traced back to its commit.
Example. Ironbridge Payments used to rebuild its API in each environment, and once shipped a production build with a newer library than the one tested. Now one job builds payments-api-2.7.0.tgz, and both deploy jobs download that file.
Before you start
You need: A free GitHub account and a public test repository (Actions minutes are free for public repositories). Time: 30 minutes.
Where to look
- Repository
- .github/workflows/release.yml
- Actions tab
- Releases
Steps
- Add
.github/workflows/release.ymlfrom Commands. It runs on tags starting with v. - Tag and push:
git tag v1.0.0thengit push origin v1.0.0. - Open the run. The build job uploads
app-1.0.0.tgz; the publish job downloads it, prints its checksum and creates the release.Compare the checksum printed in both jobs: same file. - Open Releases and see v1.0.0 with the file attached and notes generated from the commits.
- Push tag v1.1.0 after a small change and see the second release.
Commands
name: release
on:
push:
tags: ['v*']
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.v.outputs.version }}
steps:
- uses: actions/checkout@v5
- id: v
run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"
- run: tar czf app-${{ steps.v.outputs.version }}.tgz app.py && sha256sum app-*.tgz
- uses: actions/upload-artifact@v5
with:
name: app
path: app-*.tgz
retention-days: 7
publish:
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@v5
with: { name: app }
- run: sha256sum app-*.tgz
- run: gh release create "$GITHUB_REF_NAME" app-*.tgz --generate-notes -R "$GITHUB_REPOSITORY"
env:
GH_TOKEN: ${{ github.token }}
Watch out for
- Cost
- Free in public repositories. Artifact storage in private repositories counts towards your plan, so set retention-days.
- Security
- Give
contents: writeonly to the job that creates the release. - Common mistakes
- Building again in the deploy job, and versions that don't match the tag (read the version from the tag, don't type it).
Clean up
Delete test releases with gh release delete v1.0.0 --cleanup-tag --yes.
Words to know
- Artifact
- A file saved by a workflow run so later jobs, or people, can download it.
- Semantic version
- MAJOR.MINOR.PATCH: breaking change, new feature, fix.
- Tag
- A name fixed to one commit, such as v1.4.0.
- Release
- A GitHub page for a tag, with notes and attached files.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On