【问题标题】:Git credentials when npm has a git dependency当 npm 具有 git 依赖项时的 Git 凭据
【发布时间】:2017-10-01 09:56:57
【问题描述】:

我有一份 Jenkins 工作来构建我的 JS 应用程序。

在我的package.json 中,我有一个如下所示的依赖项:

"devDependencies": {
  "my_private_package": "git+https://my-server/my-repo.git#1.0.0"
}

我使用Jenkins Git PluginCredentials Plugin 来克隆repo,然后使用shell 脚本运行npm install

当 Jenkins 运行 npm install 时,npm 会出现 npm ERR! fatal: Authentication failed 错误

由于我们自托管的 git 服务器和官僚作风,我无法将 oAuth 令牌添加到 git url。

有没有办法让我设置我的 git 凭据,以便 npm 可以从我受密码保护的 git repo 安装?

【问题讨论】:

  • 您能否将您的 jenkins 从属服务器上的公钥添加到附加到具有访问权限的用户的托管 git 存储库中?

标签: git jenkins npm


【解决方案1】:

您可以使用带有文件的 credential-helper 配置向 git 发送 HTTPS 凭据。 file format 只是一个 HTTPS URL,其中填写了 user:password 凭据部分。类似于:

CREDENTIALS_FILE_PATH="$HOME/.git/my-ci-credentials"
echo 'https://ci-user:ci-password@myRepoHost.example.com/' > "$CREDENTIALS_FILE_PATH"

由于 npm 将 repo 克隆到项目文件夹的上下文之外,因此您需要在用户而不是项目级别指定此配置:

git config --global credential.helper "store --file=$CREDENTIALS_FILE_PATH"

在此之后,npm 应该能够克隆 repo。

【讨论】:

  • 我发现这很有帮助。感谢您的回答。但这有一个解决办法。不是猫而是回声。
  • @MuhammedKK 谢谢。我已经更新了 sn-p 以使用 echo
【解决方案2】:

我通过使用 git+ssh 安装依赖的方式解决了这个问题,比如git+ssh://my-server/my-repo.git#1.0.0

然后在 Jenkins .ssh 文件夹中添加一个 config 文件,内容如下:

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/<rsa file>

然后 Jenkins 知道该 ssh 密钥用于任何 github.com url。您需要拥有与您尝试导入的项目相关联的 Jenkins git 用户或部署密钥。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 2013-02-02
    • 1970-01-01
    • 2020-02-25
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    相关资源
    最近更新 更多