Giter Club home page Giter Club logo

npm-bump's Introduction

npm-bump

GitHub tag (latest SemVer) Version and Release Marketplace link

npm version {major,minor,patch} && npm publish as an action. Full npm lifecycle support and gh-release auth support. Opinionated and has a few assumptions.

Usage

Generate a publish token on npm then set it as an action secret (NPM_TOKEN in this example).

name: Version and Release

on:
  workflow_dispatch:
    inputs:
      newversion:
        description: 'Semantic Version Bump Type (major minor patch)'
        required: true

env:
  node_version: 14

jobs:
  version_and_release:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/[email protected]
      with:
        # fetch full history so things like auto-changelog work properly
        fetch-depth: 0
    - name: Use Node.js ${{ env.node_version }}
      uses: actions/[email protected]
      with:
        node-version: ${{ env.node_version }}
        # setting a registry enables the NODE_AUTH_TOKEN env variable where we can set an npm token.  REQUIRED
        registry-url: 'https://registry.npmjs.org'
    - run: npm i
    - run: npm test
    - name: Version and publush to npm
      uses: bcomnes/[email protected]
      with:
        git_email: [email protected]
        git_username: ${{ github.actor }}
        newversion: ${{ github.event.inputs.newversion }}
        push_version_commit: true # if your prePublishOnly step pushes git commits, you can omit this input or set it to false.
        github_token: ${{ secrets.GITHUB_TOKEN }} # built in actions token.  Passed tp gh-release if in use.
        npm_token: ${{ secrets.NPM_TOKEN }} # user set secret token generated at npm

This will give you a push-button triggered action that runs npm version {major,minor,patch}, git push --follow-tags and finally npm publish.

It is advisable to set a prePublishOnly lifecycle hook that runs, at a minimum, git commit pushing, so that local runs of npm version && npm publish will push the version commits to git the same way as this action will.

{
  "scripts": {
    "prepublishOnly": "git push --follow-tags"
  }
}

With that lifecycle set, you can omit the push_version_commit input, or set it to false.

The following dependencies and npm lifecycle scripts are recommended for a fully automated release process that includes:

  • changelog generation
  • github release creation with changelog contents
  • automated action based package publishing
  • parity with a local release process (you can still run npm version && npm publish and get all of the above benefits)
  • See swyx's article for a more in depth description.
{
  "devDependencies": {
    "auto-changelog": "^1.16.2",
    "gh-release": "^3.5.0"
  },
  "scripts": {
    "prepublishOnly": "git push --follow-tags && gh-release -y",
    "version": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:' && git add CHANGELOG.md"
  }
}

Additionally, you should run your tests in order to block a release that isn't passing. Automate only when green.

Inputs

  • git_email (REQUIRED): The email address used to create the version commit with.
  • git_username (REQUIRED): The name to use for the version commit. e.g. github.actor
  • newversion (REQUIRED): The version bump type to perform (e.g. major, minor, path). See npm version docs for more info. Pass this as an interactive variable.
  • push_version_commit (Default: false): Run git push --follow-tags after running npm version. Enable this if you don't configure a prepublishOnly hook that pushes git commits.
  • publish_cmd (Default: npm publish): The command to run after npm version. Useful if you are just using npm to version a package, but not publish to npm (like an action).
  • github_token: Pass the secrets.GITHUB_TOKEN to enable gh-release capabilities.
  • npm_token: An npm token scoped for publishing. Required in most cases. Used to create the release.

Outputs

None.

FAQ

I'm getting 404/bad auth errors on npm. Why?

You must set the registry-url input on the actions/setup-node action to 'https://registry.npmjs.org' at a minimum. Github actions does some wacky stuff to .npmrc like setting up a NODE_AUTH_TOKEN input for the npm token. npm-bump takes advantage of this behavior so its an assumed requirement. See this article for more info on this bizarre behavior. Also if you script modifications to a local .npmrc, this can mess up the actions/setup-node configuration.

Can I publish to the github registry?

Yes, just pass secrets.GITHUB_TOKEN as the npm_token input, and set your registry endpoint to https://npm.pkg.github.com in the actions/setup-node action.

Can I consume private Github packages from other repos?

Yes, but you have to create a new Github machine account, create a Personal Access Token, store it as an action secret, and then use that as the npm_token. Kind of a PITA.

Can publish to both npm and github?

No, not right now. I couldn't think of why this would be a good reason. Open an issue if you have ideas.

Do I have to publish to a registry?

Nope, you can completely override the npm publish command with whatever you want (e.g. npm run release which can run whatever you want related to a release.) This enables you to publish to things like the Github marketplace, create github releases etc.

Can you offer a major version tag/branch alias? I want automatic updates!

Nope! This was always weird/bad pattern of github actions. Luckily github offers a solution for this. Create a .github/dependabot.yml with, at a minimum, the following config:

# Basic dependabot.yml file with
# minimum configuration for two package managers

version: 2
updates:
  # Enable version updates for npm
  # Enable updates to github actions
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "daily"

Why isn't npm-bump running tests anymore?

Testing node stuff is usually straight forward (npm test). Sometimes though, its not. Leaving the test responsibilities to the consumer makes the action file more clear.

Why isn't npm-bump installing dependencies anymore?

Some projects don't need dependencies to release. Sometimes install steps require external system dependencies. It was decided to keep the install step inside the action consumer scope in order to increase clarity around what is happening.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.