简体   繁体   中英

How to add private node module in GitLab CI

I was able to use my private repo (in GitLab) as a node module locally for an app I'm developing. I did this by generating a personal access token in GitLab and then running this command locally with the PAT as an environment variable: git config --global url."https://oauth2:${GITLAB_PERSONAL_ACCESS_TOKEN}@gitlab.com/".insteadOf https://gitlab.com/

But when I try to deploy my app doing the same thing with GitLab CI my jobs fail with an error:

HTTP Basic: Access denied... Authentication failed for 'https://gitlab.com/myuser/myrepo.git/'

I made sure the PAT is in the GitLab CI environment variable and that it is able to be accessed. It's also used in my test jobs (build, lint, jest) and I can confirm that it's being used because when I take it out of those jobs they also fail with the same error. So it works for them but not for the deploy. 在此处输入图像描述

package.json

...
"dependencies": {
  "my-dependency": "git+https://gitlab.com/myuser/myrepo.git",
...

.gitlab-ci.yml (lint, jest, build all fail with the same error)

image: node:14.17.1

.git_config: &git_config |
  git config --global url."https://oauth2:${GITLAB_PERSONAL_ACCESS_TOKEN}@gitlab.com/".insteadOf https://gitlab.com/

.npm_install: &npm_install |
  npm ci --cache .npm --prefer-offline --ignore-scripts --include=dev

stages:
  - test
  - deploy

# Cache modules in between jobs
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .npm/

lint:
  stage: test
  before_script:
    - *git_config
    - *npm_install
  script:
    - npm run lint

jest:
  stage: test
  before_script:
    - *git_config
    - *npm_install
  script:
    - npm test

build:
  stage: test
  script:
    - *git_config
    - NODE_ENV=production npm ci # this is what Heroku will run

deploy-prod:
  stage: deploy
  image: ruby:latest
  before_script:
    - *git_config
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
  script:
    - dpl --provider=heroku --app=$HEROKU_APP_PROD --api-key=$HEROKU_API_KEY
  only:
    - master
    - production
  environment:
    name: production
    url: https://app.myapp.com
  when: manual

In your package.json include your dependency like this:

"package-name": "git+https://USER:PAT_TOKEN@gitlab.com/USER/package-name"

Replace package-name , USER and PAT_TOKEN .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM