簡體   English   中英

NPM 依賴於另一個私有 Bitbucket 存儲庫 Azure DevOps 管道身份驗證失敗

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM