/semver

Nx plugin to automate semantic versioning and CHANGELOG generation.

Primary LanguageTypeScriptMIT LicenseMIT

@jscutlery/semver NPM package @jscutlery/semver coverage status

@jscutlery/semver

Nx plugin for versioning using SemVer and CHANGELOG generation powered by Conventional Commits.

Setup

Install

Using Nx:

npm install -D @jscutlery/semver
nx g @jscutlery/semver:install

Using Angular CLI:

ng add @jscutlery/semver

This package allows you to manage your monorepo using one of two modes: Synced or Independent.

Independent mode (default)

Allow multiple projects to be versioned independently. This way you release only what you want and consumers don't get updates they don't need. This allows small, rapid and incremental adoption of your packages.

Synced mode

Allow multiple projects to be versioned in a synced/locked mode. Use this if you want to automatically tie all package versions together. This mode is useful when you are working with only one product. One issue with this approach is that a major change in any project will result in all projects having a new major version.

Usage

Release

Independent mode

Release project independently by running:

nx run my-project:version [...options]

You can leverage the built-in affected command to only version changed packages:

nx affected --target version [...options]

Synced mode

Release multiple projects at once:

nx run workspace:version [...options]

When run, this executor does the following

  1. Retrieve the current version by looking at the last git tag.
  2. Bump package.json version based on your commits.
  3. Generates CHANGELOG based on your commits (uses conventional-changelog-angular under the hood).
  4. Creates a new commit including your package.json file and updated CHANGELOG.
  5. Creates new tag with the new version number.
  6. Pushes the release (if enabled).
  7. Runs post-targets (if defined).

Available options

name type default description
--dryRun boolean false run with dry mode
--noVerify boolean false skip git hooks
--push boolean false push the release against git origin
--syncVersions boolean false lock/sync versions between projects
--skipRootChangelog boolean false skip generating root changelog
--skipProjectChangelog boolean false skip generating project changelog
--origin string 'origin' push against git remote repository
--baseBranch string 'main' push against git base branch
--changelogHeader string undefined custom Markdown header for changelogs
--releaseAs string undefined specify the level of change
--preid string undefined prerelease identifier
--tagPrefix string undefined specify the tag prefix
--postTargets string[] [] specify a list of target to execute post-release
--trackDeps boolean false use dependencies when calculating a version bump
--commitMessageFormat string undefined format the auto-generated message commit
--preset string 'angular' commit message guideline preset

Overwrite default configuration

You can customize the default configuration using with the workspace definition file:

{
  "executor": "@jscutlery/semver:version",
  "options": {
    "baseBranch": "master",
    "preset": "conventional",
    "tagPrefix": "${projectName}@"
  }
}

Specify the level of change

The --releaseAs option allows you to release a project with a version that is incremented by a specified level.

Level can be one of major, minor, patch, premajor, preminor, prepatch, or prerelease, for instance:

nx run workspace:version --releaseAs=major
nx run workspace:version --releaseAs=minor
nx run workspace:version --releaseAs=patch
nx run workspace:version --releaseAs=prerelease --preid=alpha
nx run workspace:version --releaseAs=prerelease --preid=beta

Tag prefix customization

The --tagPrefix option allows you to customize the tag prefix.

In sync mode, the tag prefix is set to "v" by default, which is resolved to v0.0.1 for instance. Note that only one tag is created for the whole workspace.

In independent mode, the tag prefix uses the contextual project name, the default value is "${projectName}-" which is resolved to my-project-0.0.1 for instance. Note that each project in the workspace is versioned with its own tag.

Commit message customization

The --commitMessageFormat option allows you to customize the commit message. By default, the commit message is formatted as the following:

chore(${projectName}): release version ${version}

The version variable is resolved to the current release version, for instance 1.0.1. This option also allows you to interpolate the projectName variable:

release: bump ${projectName} to ${version} [skip ci]

Note that it's the right place to add common keywords to skip CI workflows, for example: [skip ci] for GitHub.

Triggering executors post-release

The --postTargets option allows you to run targets post-release. This is particularly handful for publishing packages on a registry or scheduling any other task.

Here is a configuration example using @jscutlery/semver:github to create a GitHub Release and ngx-deploy-npm:deploy to publish on NPM:

{
  "targets": {
    "version": {
      "executor": "@jscutlery/semver:version",
      "options": {
        "postTargets": ["my-project:publish", "my-project:github"]
      }
    },
    "github": {
      "executor": "@jscutlery/semver:github",
      "options": {
        "tag": "${tag}",
        "notes": "${notes}"
      }
    },
    "publish": {
      "executor": "ngx-deploy-npm:deploy",
      "options": {
        "access": "public"
      }
    }
  }
}

Note that options using the interpolation notation ${variable} are resolved with their corresponding value. Here is the list of the resolved options.

  • project versioned project
  • version semver version
  • tag formatted git tag
  • notes release notes

Built-in post-targets

Tracking dependencies

The --trackDeps option indicates that direct dependencies in the project's dependency graph should be taken into account when incrementing the version. If no version-incrementing changes are present in the project, but are present in one or more dependencies, then the project will receive a patch version increment.

If you wish to track changes at any depth of your dependency graph, then you should do the following:

  1. Enable versioning for each project in the dependency graph
  2. Set the trackDeps option to true on each of the projects
  3. Make sure that version is run on projects in the right order by configuring version's target dependencies in nx.json:
{
  "targetDependencies": {
    "version": [
      {
        "target": "version",
        "projects": "dependencies"
      }
    ]
  }
}

This setup will cause a cascade of version increments starting at the deepest changed dependency, then continuing up the graph until the indicated project is reached. Additionally, if used in conjunction with nx run-many --all, or nx affected, then it will avoid attempting to version dependencies multiple times.

CI/CD usage

GitHub Actions

Here is an example running semver in a GitHub workflow:

name: release

on:
  - workflow_dispatch

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '16'
      - name: Setup Git
        run: |
          git config user.name "GitHub Bot"
          git config user.email "gituser@example.com"
      - run: yarn install --frozen-lockfile --prefer-offline
      - name: Version
        shell: bash
        run: yarn nx affected --base=last-release --target=version
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Tag last-release
        shell: bash
        run: git tag -f last-release
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
          force: true
          tags: true

Note that secrets.GITHUB_TOKEN is automatically provided by the GitHub Actions, you don't need to set up anything.

GitLab CI

Here is an example running semver in the GitLab CI:

stages:
  - release

release:
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
      when: manual
  stage: release
  image: node:16.13.2
  before_script:
    - git config --global user.name "GitLab Bot"
    - git config --global user.email "gituser@example.com"
    - git remote set-url origin http://gitlab-ci-token:${DEPLOY_KEY}@gitlab.com/org/project.git
  script:
    - yarn install --frozen-lockfile --prefer-offline
    - yarn nx affected --target=version --base=last-release
    - git tag -f last-release
    - git push origin last-release --force -o ci.skip
    - git push origin HEAD:${CI_COMMIT_BRANCH} --tags -o ci.skip

Note that you might need to configure a deploy key in order to push to your remote repository.

Changelog

For new features or breaking changes see the changelog.

Contributors

This project follows the all-contributors specification.


Younes Jaaidi

πŸ› πŸ’» πŸ“– πŸ’‘ πŸ€”

Edouard Bozon

πŸ› πŸ’» πŸ“– πŸ’‘ πŸ€”

Gleb Mikheev

πŸ€”

Richard Lea

πŸ’»

Katona

πŸ› πŸ’»

ntziolis

πŸ›

RicardoJBarrios

πŸ’» πŸ€”

Sylvain Arnouts

πŸ›

GethsLeader

πŸ’» πŸ€”

Shahar Kazaz

πŸ’»

MiloΕ‘ Lajtman

πŸ› πŸ’»

Charley Bodkin

πŸ›

Jeffrey Bosch

πŸ’»

RaviTejaVattem

πŸ’»

Abishek PY

πŸ’» πŸ“–

Stefan Schneider

πŸ’»

Travis Jones

πŸ’» πŸ“– πŸ€”

Hichri Hassen

πŸ›

Gareth John

πŸ’»

Diego Juliao

πŸ› πŸ’» πŸ€”

Charlie Francis

πŸ›

Pierre Huyghe

πŸ’»

William Sedlacek

πŸ’» πŸ€”

Tycho Bokdam

πŸ€”

nicolashzmor

πŸ›

RaΓΊl JuliΓ‘n LΓ³pez CaΓ±a

πŸ› πŸ’»

Miguel Suarez

πŸ’»

Katya Pavlenko

πŸ›

Hoon Oh

πŸ’» πŸ€”

Kurt Hoyt

πŸ›

Riain Condon

πŸ’» πŸ“– πŸ’‘

lukelukes

πŸ›

License

This project is MIT licensed.