Publish Node Packages to GitHub Packages With Azure Pipelines and Lerna.js | by CJ Hewett | Mar, 2022

A hands-on information to publishing Node packages

On this article, we’ll have a look at two methods to publish a Node.js bundle to GitHub Packages; publishing with NPM after which Lerna.

Code used in this article can be found here.

Library

Create a library referred to as “GitHub Packages” (in a brand new Azure DevOps venture) with the variable GH_TOKEN with the worth of a GitHub Private Authentication Token with write:packages permissions. Set the worth of this variable secret by clicking the padlock icon 🔒.

Code

.azure/publish.yml — This file defines the pipeline, which installs Node.js 14.x and creates a .npmrc file to set the GitHub Packages registry because the vacation spot to publish the bundle to, and runs npm publish to publish mentioned bundle.

identify: Publish Node GitHub Package deal

set off:
- principal

pr: none

variables:
- group: GitHub Packages
- identify: vmImageName
worth: 'ubuntu-latest'

levels:
- stage: Publish
displayName: Publish GitHub Package deal
jobs:
- job: Publish
displayName: Publish Job
pool:
vmImage: $(vmImageName)
steps:
- process: NodeTool@0
inputs:
versionSpec: '14.x'
displayName: 'Set up Node.js model 14.x'

- bash: |
echo //npm.pkg.github.com/:_authToken=$GH_TOKEN > ~/.npmrc
npm publish
env:
GH_TOKEN: $(GH_TOKEN)

bundle.json — Add a publishConfig part to outline your GitHub registry to publish to

"publishConfig": 
"registry": "https://npm.pkg.github.com/[User or Org name]"
,

Supply code may be discovered right here: https://github.com/cjh-cloud/github-node-package

Library

That is largely the identical because the library used above, however with the addition of GH_USER which is solely the username of the GitHub consumer’s bundle registry to publish the Node.js bundle to.

Code

bundle.json — Similar as above with the added publishConfig part.

.azure/publish.yml — This pipeline has just a few extra step:

  • Checks out the principal department
  • Configures Git credentials in order that Lerna could make a Git tag and launch
  • Boostrap Lerna (set up Node modules for all packages)
  • Publish bundle to GitHub packages with the --registry https://npm.pkg.github.com
identify: Publish Node GitHub Package deal

set off:
- principal

pr: none

variables:
- group: GitHub Packages
- identify: vmImageName
worth: 'ubuntu-latest'

levels:
- stage: Publish
displayName: Publish GitHub Package deal
jobs:
- job: Publish
displayName: Publish Job
pool:
vmImage: $(vmImageName)
steps:

- bash: git checkout principal
displayName: 'Checkout principal department' # to keep away from indifferent head

- bash: |
git config consumer.identify "$GH_USER"
git distant rm origin && git distant add origin https://random_str:$GH_TOKEN@github.com/$GH_USER/github-node-package.git && git fetch
displayName: 'Configure git'
env:
GH_TOKEN: $(GH_TOKEN)

- process: NodeTool@0
inputs:
versionSpec: '14.x'
displayName: 'Set up Node.js model 14.x'

- bash: yarn set up
displayName: Set up Yarn dependencies

- bash: yarn lerna bootstrap
displayName: Bootstrap Lerna

- bash: |
echo //npm.pkg.github.com/:_authToken=$GH_TOKEN > ~/.npmrc
yarn lerna publish --yes --exact --conventional-commits --force-publish --create-release github --registry https://npm.pkg.github.com/
env:
GH_TOKEN: $(GH_TOKEN)

Hope you discovered this handy!

More Posts