Update version and publish npm from GH

Setting up CI with GitHub Actions to update a node.js package version from GitHub release tag and publish it to npm

Super proud of this, since it's my first node.js package ever (created brand-new account on https://www.npmjs.com/ and all 😁), for doing syntax highlighting for dotnetconfig that works in docfx.

Assuming you already have a package.json in the root repo dir, add the .github\workflows\npm.yml as follows:

name: publish

on:
  release:
    types: [created]

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/
      - name: Set version from tag
        run: npm --no-git-tag-version version ${GITHUB_REF#refs/*/}
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}
  1. I'm running only on release creation

  2. Using the GitHub envvar for the checked out tag, I'm updating the version but opting out of the git behavior (which fails on CI because it's a detached head at that point)

  3. Finally you'll need to create that secret in GH for the publish step.

Last updated