Skip to content

tsukiy0's blog

Node package versioning with `semantic-release`

September 30, 2020

We will setup semantic-release to calculate a new semantic version from out commits and release that to npm.

Calculating version

As long as our commits follow the conventional commit style, semantic-release can infer whether the next semantic version will be a major, minot or patch.

Semantic Versions

Semantic versions are a convention that indicates whether the next version is a breaking change or not.

They are in the format of major.minor.patch (e.g. 1.23.456).

ChangeDescription
majorbreaking
minornon-breaking addition
patchnon-breaking fix

Conventional Commits

Conventional commits prefix a git commit message with a label that indicates the type of change being committed.

feat: add xyz feature
fix: handle divide by 0 case
# breaking change (!)
feat!: drop v1 api
LabelChangeDescription
feat! or fix!majorbreak
featminornon-breaking addition
fixpatchnon-breaking fix

Setup

  1. Install

    yarn install --dev semantic-release @semantic-release/git
  2. Configure registry in package.json

    "name": "@github-username/package"
    "publishConfig": {
    "registry": "https://npm.pkg.github.com"
    }
  3. Configure semantic-release in .releaserc.json

    {
    "release": {
    "branches": [
    {
    "name": "master"
    },
    {
    "name": "beta",
    "prerelease": true
    }
    ],
    "debug": true
    },
    "plugins": [
    "@semantic-release/commit-analyzer",
    "@semantic-release/release-notes-generator",
    "@semantic-release/npm",
    "@semantic-release/git"
    ]
    }
    • Branches
      • Release from master branch
      • Pre-release from beta branch
    • Plugins
      • @semantic-release/commit-analyzer calculates semantic version from conventional commits
      • @semantic-release/release-notes-generator generates release notes from conventional commits
      • @semantic-release/npm publishes to configured registry
      • @semantic-release/git integrates with git
  4. Trigger release

    yarn semantic-release
    • Use flag --dry-run to test configuration without uploading to registry
    • Provide a token for the registry through the .npmrc or NPM_TOKEN environment variable
    • Set CI=true to avoid prompts

tsukiy0