简体   繁体   English

如何在 GitLab CI 中添加私有节点模块

[英]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.我能够将我的私人仓库(在 GitLab 中)用作我正在开发的应用程序的本地节点模块。 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/ 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:但是,当我尝试使用 GitLab CI 部署我的应用程序时,我的工作失败并出现错误:

HTTP Basic: Access denied... Authentication failed for 'https://gitlab.com/myuser/myrepo.git/' HTTP 基本:访问被拒绝...“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.我确保 PAT 在 GitLab CI 环境变量中并且可以访问。 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.它也用于我的测试作业(构建、lint、jest),我可以确认它正在被使用,因为当我将它从这些作业中取出时,它们也会因同样的错误而失败。 So it works for them but not for the deploy.所以它适用于他们,但不适用于部署。 在此处输入图像描述

package.json package.json

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

.gitlab-ci.yml (lint, jest, build all fail with the same error) .gitlab-ci.yml (lint、jest、build 都失败并出现同样的错误)

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.json包含您的依赖项,如下所示:

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

Replace package-name , USER and PAT_TOKEN .替换package-nameUSERPAT_TOKEN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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