简体   繁体   English

NPM 依赖于另一个私有 Bitbucket 存储库 Azure DevOps 管道身份验证失败

[英]NPM dependencies to another private Bitbucket repo Azure DevOps pipeline authentication fails

I'm working on a Azure DevOps build pipeline for a project.我正在为一个项目开发 Azure DevOps 构建管道。 I can't make any changes to the code itself besides the azure-pipeline.yaml file.除了 azure-pipeline.yaml 文件之外,我无法对代码本身进行任何更改。 (And to be honest, I know very little about the project itself) (老实说,我对项目本身知之甚少)

I'm stuck on the NPM install dependencies step.我被困在 NPM 安装依赖项步骤上。 I'm currently working with the YAML pipeline, but if there's a solution in the classic mode I'll go with that.我目前正在使用 YAML 管道,但如果在经典模式下有解决方案,我将使用 go 。

The issue is the following:问题如下:

I've created the pipeline with and I check out a private Bitbucket repository according to the documentation:我已经创建了管道,并根据文档检查了一个私有 Bitbucket 存储库:

resources:
  repositories:
  - repository: MyBitBucketRepo1
    type: bitbucket
    endpoint: MyBitBucketServiceConnection
    name: MyBitBucketOrgOrUser/MyBitBucketRepo

Next I set the correct version of node, and execute a npm install task接下来我设置正确的节点版本,并执行npm install任务

- task: Npm@1
  displayName: 'NPM install'
  inputs:
    command: 'install'
    workingDir: 'the working directory'

So far so good.到目前为止,一切都很好。 But, there is a dependency to another Bitbucket repository.但是,存在对另一个 Bitbucket 存储库的依赖关系。 In the package.json there is a dependecy like this:在 package.json 中有这样的依赖:

another-dependency: git:https://bitbucket.org/organisation/repo.git#v1.1.3

I do have access to this repository, but if I run NPM install it can't re-use the credentials from the first repository.我确实可以访问此存储库,但如果我运行 NPM install 它无法重新使用第一个存储库中的凭据。

I've tried adding both repositories to the resources in the hope that would work.我已经尝试将两个存储库都添加到resources中,希望能够奏效。 But still the same error:但仍然是同样的错误:

error fatal: Authentication failed for 'https://bitbucket.org/organisation/repo.git/'

I've tried to set up some caching mechanism, run npm install on the 2nd repo, store the dependencies, run npm install on the first one.我尝试设置一些缓存机制,在第二个 repo 上运行npm install ,存储依赖项,在第一个 repo 上运行npm install But that didn't work unfortunately.但不幸的是,这并没有奏效。

Is there a way in Azure Devops pipelines -without making changes to the project set-up- to make this work? Azure Devops 管道中是否有一种方法——无需更改项目设置——来完成这项工作?

Thanks!谢谢!

Normally I have the.npmrc on the Repo so I dont have to add any other task.通常我在 Repo 上有 .npmrc,所以我不必添加任何其他任务。 Something like in this guide: 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/artifacts/get-started-npm?view=azure-devops&tabs=windows

And I never do something like that, but I think that you can authenticate with the external feed adding this task: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/package/npm-authenticate?view=azure-devops而且我从不这样做,但我认为您可以通过添加此任务的外部提要进行身份验证: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/package/npm-authenticate ?view=azure-devops

Reading a bit more, I dont know if you can do this without adding a.npmrc on your Repo.多读一点,我不知道你是否可以在不添加 a.npmrc 的情况下做到这一点。 You have to create a ServiceConnection to store your login credentials, but on that you will need the.npmrc on your Repo.您必须创建一个 ServiceConnection 来存储您的登录凭据,但在此基础上您将需要在您的 Repo 中使用 .npmrc。 在此处输入图像描述

Try it and tell my if this help!!试试看,告诉我这是否有帮助!

Npm will prompt for passwords when you run npm install command for your package.json locally. Npm 将在您本地运行package.json npm install命令时提示您输入密码。 Since we can't enter the password during pipeline run in CI/CD pipeline, it causes the Authentication failed error.由于在 CI/CD 管道中运行管道时我们无法输入密码,因此会导致Authentication failed错误。

An alternative workaround is to add credentials directly in url, like this:另一种解决方法是直接在 url 中添加凭据,如下所示:

"dependencies": {
    "another-dependency": "git+https://<username>:<password>@bitbucket.org/xxx/repo.git"
}

See app-password :请参阅应用密码

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. 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. So it depends on you whether to use this workaround.因此,这取决于您是否使用此解决方法。

As a workaround for Azure Devops pipeline:作为 Azure Devops 管道的解决方法:

You can add a File Transform task to replace the old url with new Username+Password url before your npm install steps.在 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 . 1.I have a package.json in root directory with content like git:https://bitbucket.org/organisation/repo.git#v1.1.3 . 在此处输入图像描述

2.Define a dependencies.another-dependency variable with value git+https://<username>:<password>@bitbucket.org/... , set it as secret! 2.定义一个dependencies.another-dependency变量,值为git+https://<username>:<password>@bitbucket.org/... ,设置为secret!

在此处输入图像描述

3.Then add the File Transform task like this: 3.然后像这样添加File Transform task

在此处输入图像描述

4.Finally you'll get a new package.json file with content below: 4.最后你会得到一个新的package.json文件,内容如下:

在此处输入图像描述

It won't actually affect your package.json file under version control, it just add credentials temporarily during your pipeline.它实际上不会影响您在版本控制下的 package.json 文件,它只是在您的管道期间临时添加凭据。 Let me know if it helps:)让我知道是否有帮助:)

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

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