Skip to content
Search

How to deploy npm from GitLab CI

NPM Deploy

To deploy an npm package from CI, follow these steps.

  1. Set private in package.json to false.
  2. Change the scope of the npm package to the group name.
  3. Make the .npmrc file 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

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:

@serverid-steel:registry=https://git.yourdomain.com/api/v4/packages/npm/

The .yarnrc file is as follows:

"@serverid-steel:registry" "https://git.yourdomain.com/api/v4/packages/npm/"

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.

See also

author Zer0 sitemap