Publishing a package by using a CI/CD pipeline
You can authenticate with your project’s package registry when publishing via a CI/CD pipeline by utilising the predefined variables ${CI_PROJECT_ID} and ${CI_JOB_TOKEN}. We build a.npmrc file for authentication during the execution of your CI/CD task using these variables.
In the GitLab project containing your package.json, edit or create a .gitlab-ci.yml file. For example:
# Define the Docker image to be used
image: node:latest
# Define the stages of the pipeline
stages:
- deploy
# Define the deployment stage
publish-npm:
stage: deploy
script:
# Set up npm configuration for publishing packages
- echo "@myscope:registry=https://${MY_CI_SERVER_HOST}/api/v4/projects/${MY_CI_PROJECT_ID}/packages/npm/" > .npmrc
- echo "//${MY_CI_SERVER_HOST}/api/v4/projects/${MY_CI_PROJECT_ID}/packages/npm/:_authToken=${MY_CI_JOB_TOKEN}" >> .npmrc
# Publish the package to the npm registry
- npm publish
Replace @scope with the scope of the package that is being published.
Your package is published to the package registry when the publish-npm job in your pipeline runs.
How to Publish NPM Packages in the Package Registry ?
The npm Package Registry serves as an invaluable tool for publishing npm packages for individual developers or organizations.
This article will guide you through the requirements, configuration, and usage of the npm package registry.
Table of Content
- Setting Up the Package Registry
- Publishing a Package
- Authenticating via the .npmrc
- Publishing a package via the command line
- Publishing a package by using a CI/CD pipeline
- Unpublishing a Package
- Installing a Package
- Tagging a Package
- Searching Packages
- Supported Commands
- Conclusion
Contact Us