If you are as addicted to CI and CD as we are at Tauri, this brief article will show you how we solved publishing to NPM on the release tag event at Github.
Background
Our org is growing, and we don't want individuals to bear the responsibility for publishing to crates.io and npm. That's brittle and bus-factor waiting to happen. And doing things manually is always error prone.
What we did:
- Setup a CI user at NPM (don't choose 2FA), and copy their token.
- Create a secret at the repo settings, call it
npm_token
and paste the token as the secret value. - Create a file at
.github/workflows/publish.yml
with the following contents:
name: NPM Publish
on:
release:
types: [published]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node 12
uses: actions/setup-node@v1
with:
# specify node version and the registry for the RELEASE build
node-version: 12
registry-url: https://registry.npmjs.org/
- name: Build package
run: |
npm install -g yarn
yarn install
yarn rollup -c
- name: Register Token
run: |
echo "//registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN" > /home/runner/work/_temp/.npmrc
echo "_auth=$NODE_AUTH_TOKEN" >> /home/runner/work/_temp/.npmrc
echo "email=<your@email.address>" >> /home/runner/work/_temp/.npmrc
echo "always-auth=true" >> /home/runner/work/_temp/.npmrc
env:
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
- name: Publish
run: npm publish
Now just publish a release and let the runner do its thing.
Let us know in the comments if you've got any improvements!
- https://github.com/features/actions
- https://github.com/tauri-apps/tauri-forage (the repo where we're using this)
- Image from Unsplash: https://unsplash.com/photos/Tzm3Oyu_6sk
Top comments (3)
Just a heads up: in this case weโre actually running our build step with
yarn rollup -c
But you might need to do something else!
Cool, I will give this a try!!
How do you guys increment versions? Do you do that manually or use some kind of automation like standard-release or semantic-release?
Thatโs something at this library that weโre doing manually, but really this was just the testing ground for our main repo - which is a complicated mono repo with multiple crates and npm artifacts. Weโll be publishing that complete workflow really soon!