当 npm 具有 git 依赖项时的 Git 凭据

tkw*_*rgs 5 git npm jenkins

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

在我的package.json我有一个看起来像这样的依赖:

"devDependencies": {
  "my_private_package": "git+https://my-server/my-repo.git#1.0.0"
}
Run Code Online (Sandbox Code Playgroud)

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

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

由于我们自托管的 git 服务器和官僚作风,我无法通过向 git url 添加 oAuth 令牌做任何事情。

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

Jer*_*man 5

您可以使用带有文件的凭证助手配置向 git 提供 HTTPS 凭证。该文件格式只是一个HTTPS URL与user:password凭证部分填写喜欢的东西:

CREDENTIALS_FILE_PATH="$HOME/.git/my-ci-credentials"
cat 'https://ci-user:ci-password@myRepoHost.example.com/' > "$CREDENTIALS_FILE_PATH"
Run Code Online (Sandbox Code Playgroud)

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

git config --global credential.helper "store --file=$CREDENTIALS_FILE_PATH"
Run Code Online (Sandbox Code Playgroud)

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


Nic*_*las 1

我通过使用 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>
Run Code Online (Sandbox Code Playgroud)

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