How to pull NPM dependency from private GitLab Git repo during GitLab CI build

做~自己de王妃 提交于 2019-12-22 05:23:30

问题


I have a job in my .gitlab-ci.yml file which does an npm install like so:

test:
  image: node:10
  script:
    - npm install
    - npm test

The problem is that I'm referencing a private GitLab repo in my package.json:

"dependencies": {
  "internal-dep": "git+https://gitlab.com/Company/internal-dep.git",
  ...

The npm install command works locally, since I'm already authed against GitLab, but fails on GitLab CI. How do I get internal-dep to resolve successfully on GitLab CI?


回答1:


There are two approaches I've found that allow Git to auth successfully against GitLab during the npm install step (which uses Git under the hood to access this dependency).

First approach, as shown in this .gitlab-ci.yml job:

test:
  image: node:10
  script:
    - echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
    - npm install
    - npm test

Second approach, which also seems to work:

test:
  image: node:10
  script:
    - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/".insteadOf https://gitlab.com/
    - npm install
    - npm test


来源:https://stackoverflow.com/questions/52784182/how-to-pull-npm-dependency-from-private-gitlab-git-repo-during-gitlab-ci-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!