Simple demo of setting up semantic release, commitizen and husky
- Writing meaningful commit messages
- Automate package releases with semantic-release and commitizen
- How to control your deployments and versioning with semantic-release & friends
- Making commits the right way with hooks
- How to lint git commit messages
This project demonstrates how to configure semantic-release using conventional commits and commitizen.
Install and setup semantic release
- Create a npm publish token then save it as a github secret, named
NPM_TOKEN
- This will be used later with setup github action step
npm i -D semantic-release-cli
semantic-release-cli setup --npm-username={npmUserName} --npm-token={npmToken} --gh-token={githubToken}
- Commit current changes. Next step requires package.json to be free of any changes.
npm version '0.0.0-semantically-released'
NOTE:
npm version
will create a version tag and commit the changes.
// create release.config.js in root directory
// this overrides default tag deployment
module.exports = {
branches: ['main'],
};
NOTE:
npm set-scripts
works with npm 7.x+
Install husky, create prepare script which installs husky
npm i -D husky
npm set-script prepare "husky install"
npm run prepare
npm i -D commitizen cz-conventional-changelog
commitizen init cz-conventional-changelog
npm set-script pre-commit "exec < /dev/tty && git cz --hook || true\n"
npx husky add .husky/pre-commit "npm run pre-commit" && git add .husky/pre-commit
npm i -D @commitlint/{cli,config-conventional}
npm set-script commit-msg "npx --no-install commitlint --edit $1"
npx husky add .husky/commit-msg "npm run commit-msg" && git add .husky/commit-msg
// create commitlint.config.js in root directory
module.exports = {
extends: ['@commitlint/config-conventional'],
};
This github action is configured to run manually or with each push to main
.
NOTE: This depends on NPM_TOKEN from setup-semantic-release step above
// create .github/workflows/npm-release.yml
name: npm-release
on:
workflow_dispatch:
push:
branches: [ main ]
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12'
- name: Install dependencies
run: npm ci
- name: Release
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx semantic-release