How to deploy npm from GitLab CI
NPM Deploy
To deploy an npm package from CI, follow these steps.
- Set
privatein package.json tofalse. - Change the scope of the npm package to the group name.
- If the repository URL is
https://git.yourdomain.com/serverid-steel/charginggraph, use@serverid-steel. - The package name can be anything you like (it can differ from the project name)
- Example:
"name": "@serverid-steel/charginggraph"
- Example:
- If the repository URL is
- Make the
.npmrcfile contain the following:
@scope:registry=https://your_domain_name/api/v4/projects/your_project_id/packages/npm/ //your_domain_name/api/v4/projects/\({CI_PROJECT_ID}/packages/npm/:_authToken=\){CI_JOB_TOKEN}
</syntaxhighlight>
* Enter the repository domain in your_domain_name.
#* Enter the project ID in your_project_id.
Code for deploying from the pipeline
No need to issue a separate API token.
image: node:18
build_and_deploy:
stage: deploy
variables:
REGISTRY: "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/"
TOKEN: "_authToken=${CI_JOB_TOKEN}"
script:
- yarn build
- echo "@serverid-steel:registry=https:${REGISTRY}" > .npmrc
- echo "${REGISTRY}:${TOKEN}" >> .npmrc
- npm publish
NPM Install
- Yarn install from gitlab package registry not working with instance-level - General - GitLab Forum
- Use a scoped registry with Yarn by sergiodxa
- yarnpkg - Installing private package from Github Package registry using Yarn fails with not authorized - Stack Overflow
How to download a Gitlab npm package registered in a private repository:
If the npm package has a scope, add the scope's repository information to the .npmrc file:
The .yarnrc file is as follows:
| INFORMATION |
| The URL must end with a slash ( |
Then create a "Project Access Tokens" in GitLab with only the read_api permission. If the project ID is 123 and the token is 6Wo5YPQ1zsCbrTsZ1tgK, add the following to the .npmrc file:
//git.yourdomain.com/api/v4/projects/123/packages/npm/:_authToken=6Wo5YPQ1zsCbrTsZ1tgK
//git.yourdomain.com/api/v4/packages/npm/:_authToken=6Wo5YPQ1zsCbrTsZ1tgK
Then just run npm install or yarn install.
How to deploy with Yarn
See GitLabCI:Yarn#Yarn Deploy.