[英]NPM dependencies to another private Bitbucket repo Azure DevOps pipeline authentication fails
我正在为一个项目开发 Azure DevOps 构建管道。 除了 azure-pipeline.yaml 文件之外,我无法对代码本身进行任何更改。 (老实说,我对项目本身知之甚少)
我被困在 NPM 安装依赖项步骤上。 我目前正在使用 YAML 管道,但如果在经典模式下有解决方案,我将使用 go 。
问题如下:
我已经创建了管道,并根据文档检查了一个私有 Bitbucket 存储库:
resources:
repositories:
- repository: MyBitBucketRepo1
type: bitbucket
endpoint: MyBitBucketServiceConnection
name: MyBitBucketOrgOrUser/MyBitBucketRepo
接下来我设置正确的节点版本,并执行npm install
任务
- task: Npm@1
displayName: 'NPM install'
inputs:
command: 'install'
workingDir: 'the working directory'
到目前为止,一切都很好。 但是,存在对另一个 Bitbucket 存储库的依赖关系。 在 package.json 中有这样的依赖:
another-dependency: git:https://bitbucket.org/organisation/repo.git#v1.1.3
我确实可以访问此存储库,但如果我运行 NPM install 它无法重新使用第一个存储库中的凭据。
我已经尝试将两个存储库都添加到resources
中,希望能够奏效。 但仍然是同样的错误:
error fatal: Authentication failed for 'https://bitbucket.org/organisation/repo.git/'
我尝试设置一些缓存机制,在第二个 repo 上运行npm install
,存储依赖项,在第一个 repo 上运行npm install
。 但不幸的是,这并没有奏效。
Azure Devops 管道中是否有一种方法——无需更改项目设置——来完成这项工作?
谢谢!
通常我在 Repo 上有 .npmrc,所以我不必添加任何其他任务。 本指南中的内容: https://docs.microsoft.com/en-us/azure/devops/artifacts/get-started-npm?view=azure-devops&tabs=windows
而且我从不这样做,但我认为您可以通过添加此任务的外部提要进行身份验证: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/package/npm-authenticate ?view=azure-devops
多读一点,我不知道你是否可以在不添加 a.npmrc 的情况下做到这一点。 您必须创建一个 ServiceConnection 来存储您的登录凭据,但在此基础上您将需要在您的 Repo 中使用 .npmrc。
试试看,告诉我这是否有帮助!
Npm 将在您本地运行package.json
npm install
命令时提示您输入密码。 由于在 CI/CD 管道中运行管道时我们无法输入密码,因此会导致Authentication failed
错误。
另一种解决方法是直接在 url 中添加凭据,如下所示:
"dependencies": {
"another-dependency": "git+https://<username>:<password>@bitbucket.org/xxx/repo.git"
}
请参阅应用密码:
username: your normal Bitbucket username
password: the app password
It has disadvantage since we store the app-password directly as plain-text in package.json
file, which lacks security if someone else can access your package.json
file. 因此,这取决于您是否使用此解决方法。
作为 Azure Devops 管道的解决方法:
在 npm 安装步骤之前,您可以添加文件转换任务以用新的用户名+密码 url 替换旧的 url。
1.I have a package.json in root directory with content like git:https://bitbucket.org/organisation/repo.git#v1.1.3
.
2.定义一个dependencies.another-dependency
变量,值为git+https://<username>:<password>@bitbucket.org/...
,设置为secret!
3.然后像这样添加File Transform task
:
4.最后你会得到一个新的package.json文件,内容如下:
它实际上不会影响您在版本控制下的 package.json 文件,它只是在您的管道期间临时添加凭据。 让我知道是否有帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.