Automate your GitHub release workflow to save time. This guide covers how to set up automatic releases using GitHub Actions and semantic versioning.
This automation process includes:
- Analyzing commits using conventional-changelog
- Generating and updating
changelog.md
- Committing release assets
- Publishing GitHub releases and NPM packages
The workflow triggers on pushes to specific branches (main
, next
, next-major
, beta
, alpha
, and *.x
). To set it up:
Add these environment variables to your GitHub repository settings:
GH_TOKEN
: Your GitHub personal access token with 'repo' scopeNPM_TOKEN
: Your NPM token
Add the following to your package.json
:
{
"release": {
"extends": "doogu/release.config.js"
}
}
Create a release.yml
in .github/workflows/
:
name: Release
on:
push:
branches:
- main
- next
- next-major
- beta
- alpha
- '*.x'
permissions:
contents: read # for checkout
jobs:
release:
name: Releasing
runs-on: ubuntu-latest
permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for npm provenance
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 'lts/*'
- name: Install dependencies
run: npm clean-install
- name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
run: npm audit signatures
- name: Test and building
run: |
npm run lint
npm run build
npm test
- name: Install release dependencies
run: |
npm i -D semantic-release @semantic-release/changelog @semantic-release/git
- name: Semantic release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release