Publish package to GitHub Packages
- create a action workflow
name: Publish package to GitHub Packageson: release: types: [published]jobs: build: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v4 # Setup .npmrc file to publish to GitHub Packages - uses: actions/setup-node@v4 with: node-version: "20.x" registry-url: "https://npm.pkg.github.com" scope: "@xkeshav" - name: Install dependencies run: yarn - name: Publish package run: yarn publish --access restricted env: NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}once you create a release ; it will generate a package and it will be visible on right side of repo under Packages
Publish package to NPM registry from github
-
go to npm website
-
open setting and generate granular token which read/write access and you can specify a package
-
copy the token
-
open github and go to the repo and its settings and click secrets and variable and save this token with any name ( I’ll save as NPM_PUBLISH_TOKEN ) and set value which is copies from npm
-
create a action under .github/workflows and paste below code
name: Publish Package to NPMon:release:types: [created, prereleased]jobs:publish-to-npm:runs-on: ubuntu-latestpermissions:id-token: writesteps:- uses: actions/checkout@v4- name: Use Node.jsuses: actions/setup-node@v4with:node-version: "20"registry-url: "https://registry.npmjs.org/"scope: "@xkeshav"always-auth: true- run: npm publishenv:NODE_AUTH_TOKEN: ${{secrets.NPM_PUBLISH_TOKEN}} -
now commit the code and create a release a. create a tag ( click Tags near branch name ) b. Click on Releases tab <https://github.com/
/ /releases> c. Click Draft a new release button d. Click Choose a Tag , select tag if exist otherwise write new tag it will generate new tag; convention will be number x.y.z 3. Target ( keep it default selected value main ) e. Select Previous Tag ( if any ) ; it will generate release note from that tag f. click on generate release note g. Click on Publish Release it will create a Assets ( in zip ) and changelog in release page -
go to Actions tab and you will see that above workflow started running and when it will finished a new package will be published to npm