How do you create a release and publish packages from GitHub Actions?
IntermediateConceptGitHub Actions
Answer
Trigger on a version tag, build, then create a GitHub Release with gh release create and publish to GHCR/npm with the built-in token.
on:
push:
tags: ['v*.*.*']
permissions:
contents: write
packages: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
- run: |
docker build -t ghcr.io/${{ github.repository }}:${{ github.ref_name }} .
docker push ghcr.io/${{ github.repository }}:${{ github.ref_name }}
- run: gh release create "${{ github.ref_name }}" --generate-notes
env: { GH_TOKEN: '${{ github.token }}' }
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate