Open bitliner opened 1 month ago
Using semantic-release
with GitHub Actions to automatically publish a pre-release version to npm from a secondary branch (like develop
) is a great way to automate your release process. Here's how you can set it up:
semantic-release
First, make sure semantic-release
and its related plugins are installed in your project:
npm install --save-dev semantic-release @semantic-release/npm @semantic-release/git @semantic-release/github
semantic-release
Create or update the .releaserc
file in your project root with the following configuration. This configuration is set up to handle pre-releases from the develop
branch:
{
"branches": [
"main",
{
"name": "develop",
"prerelease": true
}
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
}
branches
:
"main"
: The primary branch for stable releases."develop"
: The secondary branch for pre-releases. The prerelease: true
flag tells semantic-release
to create pre-releases for this branch.plugins
:
@semantic-release/commit-analyzer
: Analyzes commit messages to determine the type of release.@semantic-release/release-notes-generator
: Generates release notes based on commits.@semantic-release/changelog
: Updates the CHANGELOG.md
file with the new version notes.@semantic-release/npm
: Publishes the package to npm.@semantic-release/github
: Creates a GitHub release.@semantic-release/git
: Commits the version bump and changelog updates back to the repository.Create a GitHub Action workflow file at .github/workflows/release.yml
:
name: Release
on:
push:
branches:
- main
- develop
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
on: push
:
main
and develop
branches.GITHUB_TOKEN
:
semantic-release
to interact with the GitHub API (e.g., to create releases).NPM_TOKEN
:
NPM_TOKEN
and paste the npm access token into the value field.Develop Branch: When you push changes to the develop
branch, semantic-release
will:
beta
.develop
branch.Main Branch: When you push changes to the main
branch, semantic-release
will:
latest
tag.main
branch.With this setup:
develop
will trigger a pre-release, with the version tagged as a pre-release (e.g., 1.0.0-beta.1
).main
will trigger a stable release, with the version tagged as latest
(e.g., 1.0.0
).This automation leverages semantic-release
to manage versioning, changelog generation, and publishing to npm, simplifying the release process and reducing manual intervention.
Build CI workflow with:
npm publish
on main branchnpm publish prerelease
on secondary branches