Skip to content

A github action bumping package version depending on related commits messages.

Notifications You must be signed in to change notification settings

jpb06/bump-package

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

bump-package

Open in Visual Studio Code Github workflow Quality Gate Status Maintainability Rating Security Rating Reliability Rating Coverage Total coverage Lines of Code Technical Debt Code Smells Bugs Vulnerabilities Duplicated Lines (%) Last commit

A github action bumping the version of a package and pushing the version bump to the repo.

        

⚡ Description

This github action bumps package.json version after a commit is pushed or a pull request is merged to the repo master branch. The updated package.json file is then pushed to master and a tag is created.

⚠️ This action requires the checkout action to work

⚠️ You need to allow read and write operations granted to the GITHUB_TOKEN for workflows

You can find the configuration option in Settings -> Actions -> Workflow permissions.

🔶 Pushing directly to default branch

If you push directly to default branch, then only the pushed commit message will be scanned to define if a bump should be performed.

🔶 Pull requests

In the case of a pull request, the action will adapt to the merging strategy chosen.

🧿 Merge commits

If the PR is merged using the merge commit strategy, then all the messages of all the commits in the branch will be scanned.

🧿 Squash merging

If the PR is merged using the squash merging strategy, all the commits will be squashed into one. Github typically joins the messages of all the squashed commits into the single commit that will be written to the target branch. This message typically looks like this from the squash of 3 commits:

Doing cool stuff (#3)

* feat: my cool feature

* chore: fixing stuff

* yolo

In that case, this message will be scanned to define whether a bump should be performed. In this example, it would result in a minor bump with the following config:

# [...]
- name: ⏫ Bumping version
  uses: jpb06/bump-package@latest
  with:
    major-keywords: BREAKING CHANGE
    minor-keywords: feat,minor
    patch-keywords: fix,chore

🧿 Completed workflow events

You can also run the action by depending on another workflow. In that case the commits messages will be extracted from the completed webhook event:

[...]

on:
  workflow_run:
    workflows: ['my-other-workflow']
    types:
      - completed

jobs:
  version-bump:
    name: 🆕 Version bump
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      [...]

      - name: ⏫ Bump package version
        id: bumping-version
        uses: jpb06/bump-package@latest

⚡ Inputs

🔶 cwd

Path location of the package to bump. Useful for monorepos.

Default value: .

🔶 major-keywords

Commits messages starting with these keywords will trigger a major bump. Commas may be used to specify more than one keyword

Default value: [Major]:

🔶 minor-keywords

Commits messages starting with these keywords will trigger a minor bump. Commas may be used to specify more than one keyword

Default value: [Minor]:

🔶 patch-keywords

Commits messages starting with these keywords will trigger a patch bump. Commas may be used to specify more than one keyword

Default value: [Patch]:

🔶 should-default-to-patch

If no keywords are present in branch commits, bump anyway by doing a patch.

Default value: false

🔶 commit-user

Customizing the name of the user committing generated badges (optional).

Default value: <context.actor>

🔶 commit-user-email

Customizing the email of the user committing generated badges (optional).

Default value: <context.actor>@users.noreply.github.com

🔶 debug

Debug mode. Will display event github event data.

Default value: false

⚡ Outputs

🔶 bump-performed

true if version has been bumped in package.json.

🔶 new-version

The resolved new version. For example, if initial version is 1.1.1 and bump type is patch, then new-version will be 1.1.2.

⚡ Usage

🔶 Using defaults

If the action runs on a commit whose message starts with either [Major]:, [Minor]: or [Patch]:, the version will be bumped and a tag will be created.

name: ⚡ Package version bump
on: [push]

jobs:
  bump:
    name: 🆕 Version bump
    runs-on: ubuntu-latest
    steps:

    - name: ⬇️ Checkout repo
      uses: actions/checkout@v4

    [...]

    - name: ⏫ Bumping version
      uses: jpb06/bump-package@latest

🔶 Using custom inputs

The action will bump the package depending on commits present in the pull request when it is merged to the master branch. By priority order:

  • if any commit message contains BREAKING CHANGE, then there will be a major bump.
  • if any commit message contains feat or minor but none of the above, then there will be a minor bump.
  • if any commit message contains fix or chore but none of the above, then there will be a patch bump.

A tag will also be created by the action.

name: ⚡ Package version bump
on: [push]

jobs:
  bump:
    name: 🆕 Version version bump
    runs-on: ubuntu-latest
    steps:

    - name: ⬇️ Checkout repo
      uses: actions/checkout@v4

    [...]

    - name: ⏫ Bumping version
      uses: jpb06/bump-package@latest
      with:
        major-keywords: BREAKING CHANGE
        minor-keywords: feat,minor
        patch-keywords: fix,chore

🔶 Defaulting to patch bump

You may want to bump the package version even if no keywords were present in the commits list (if merging a PR) or in the last commit (if pushing to master branch).

By setting should-default-to-patch to true you can trigger this behavior. Here is an example:

name: ⚡ Package version bump
on: [push]
jobs:
  bump:
    name: 🆕 Version bump
    runs-on: ubuntu-latest
    steps:

    - name: ⬇️ Checkout repo
      uses: actions/checkout@v4

    [...]

    - name: ⏫ Bumping version
      uses: jpb06/bump-package@latest
      with:
        major-keywords: BREAKING CHANGE
        minor-keywords: feat,minor
        patch-keywords: fix,chore
        should-default-to-patch: true

Now let's imagine I'm running this action when merging a PR with the following commits:

  • cool
  • obnoxios commit message
  • hey

Since no keywords were detected, the action will bump the package version with a patch: 1.0.0 -> 1.0.1.

🔶 Using output

We may want to perform an action if package.json has been bumped. We can use bump-performed output for this:

name: ⚡ Package version bump
on: [push]
jobs:
  bump:
    name: 🆕 Version bump
    runs-on: ubuntu-latest
    steps:

    - name: ⬇️ Checkout repo
      uses: actions/checkout@v4

    [...]

    - name: ⏫ Bumping version
      id: bumping-version
      uses: jpb06/bump-package@latest

    - name: 🚀 Publishing package
      if: steps.bumping-version.outputs.bump-performed == 'true'
      run: |
        cd dist
        yarn publish --non-interactive
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

You can also get the new version using new-version output:

- name: Display action output
  run: |
    echo "New version: ${{ steps.bumping-version.outputs.new-version }}"

🔶 Monorepos

You can use the input cwd to perform a bump for a monorepo app or package. The following job would bump the package.json file located at ./libs/my-package/package.json:

name: ⚡ Package version bump
on: [push]
jobs:
  bump:
    name: 🆕 Version bump
    runs-on: ubuntu-latest
    steps:

    - name: ⬇️ Checkout repo
      uses: actions/checkout@v4

    [...]

    - name: ⏫ Bumping version
      uses: jpb06/bump-package@latest
      with:
        cwd: ./libs/my-package

The pushed version tag will look like this: <package_name>@v<version>

Example:

my-package@v2.3.0

The bump commit will have the following message: chore(<package_name>): bump version to <version>

Eexample:

chore(my-package): bump version to v2.3.0