Tip
Create a Prerelease Channel in Your NPM Package using semantic-release
Automate your package release process with semantic-release to have preview releases when you push or merge specific branches
If you wish to thoroughly test essential features of your npm package in real-world scenarios before its official release, it's a good practice to establish a prerelease channel labeled as @beta to allow early testing.
By incorporating semantic-release into our Continuous Integration (CI) environment, we can automate the entire package release process. This tool determines the next version number by analyzing commit messages (including keywords like "fix," "feat," and "BREAKING CHANGE"), generates release notes, and publishes the package.
To set up a GitHub Action for this purpose, follow these steps:
Step 1: Define the GitHub Action Workflow
Create a workflows folder at the root of your project and include a release.yml file with minimal configuration settings for semantic-release. This workflow runs on Node.js version 18.
name: Release CI
on:
push:
branches: [main, next]
pull_request:
branches: [main, next]
jobs:
...
The on section specifies the triggers. In this case, the workflow "Release CI" will execute when a new commit is pushed or merged to the main or next branch.
Step 2: Add the Prerelease Channel
2.1 - Update triggers in release.yml
To incorporate the beta channel as a trigger in the release.yml file, modify the on section as follows:
on:
push:
- branches: [main, next]
+ branches: [main, next, beta]
pull_request:
- branches: [main, next]
+ branches: [main, next, beta]
Now, this workflow will release a new version in the beta channel every time there's a push or pull request merge in the beta branch.
2.2 - Configure the package.json
In your library's package.json file, within the release option, add the new beta branch with prerelease: true.
"release": {
"branches": [
// other branches
{
"name": "beta",
"prerelease": true
}
]
},
After making these changes, commit them to the main branch using the following commands:
git add .
git commit -m "chore: add beta prerelease channel"
git push
2.3 - Create the beta branch in your repository
Create a new branch named "beta" in your Git repository by running the command:
git branch beta
Then, create the branch in your hosted repository using:
git push --set-upstream origin beta
Now, whenever you merge changes into the beta branch, you can test your package as a prerelease in your real projects by installing it with your-package@beta.
With these steps, you've set up a prerelease channel for your npm package, making it easier to test and gather feedback before the official release.