Simple demo of setting up semantic release, commitizen and husky

Commitizen friendly semantic-release


semantic-release-example

Inspired by

Getting Started

This project demonstrates how to configure semantic-release using conventional commits and commitizen.

  1. Setup semantic-release
  2. Setup husky
  3. Setup commitizen
  4. Setup commitlint
  5. Setup github action
  6. Plugins

Setup semantic-release

Install and setup semantic release

  1. Create a npm publish token then save it as a github secret, named NPM_TOKEN
  2. npm i -D semantic-release-cli
  3. semantic-release-cli setup --npm-username={npmUserName} --npm-token={npmToken} --gh-token={githubToken}
  4. Commit current changes. Next step requires package.json to be free of any changes.
  5. 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'],
};

more details

Setup husky

NOTE: npm set-scripts works with npm 7.x+

Install husky, create prepare script which installs husky

  1. npm i -D husky
  2. npm set-script prepare "husky install"
  3. npm run prepare

Setup commitizen

  1. npm i -D commitizen cz-conventional-changelog
  2. commitizen init cz-conventional-changelog
  3. npm set-script pre-commit "exec < /dev/tty && git cz --hook || true\n"
  4. npx husky add .husky/pre-commit "npm run pre-commit" && git add .husky/pre-commit

more details

Setup commitlint

  1. npm i -D @commitlint/{cli,config-conventional}
  2. npm set-script commit-msg "npx --no-install commitlint --edit $1"
  3. 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'],
};

Setup github action

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

Plugins