簡體   English   中英

如何在Azure Pipeline Bash任務中驗證git push到GitHub的身份

[英]How to authenticate git push to GitHub within an Azure Pipeline Bash task

在我們的一個Azure管道中,我嘗試將標簽推送到GitHub,但根據我的方法,會收到兩個錯誤之一。

無需身份驗證,只需簡單的git push

- bash: 'git push origin $(tag)'
git push origin 2019.07.01.1
fatal: could not read Username for 'https://github.com': terminal prompts disabled

通過AUTHORIZATION: Bearer ***作為額外標題

- bash: 'git -c http.extraheader="AUTHORIZATION: Bearer $(System.AccessToken)" push origin $(tag)'
git -c http.extraheader="AUTHORIZATION: Bearer ***" push origin 2019.07.01.2
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

通過AUTHORIZATION: Bearer ***使用環境變量將AUTHORIZATION: Bearer ***作為額外的標頭

來自: https : //github.com/microsoft/azure-pipelines-agent/issues/1582#issuecomment-392235276

- bash: 'git -c http.extraheader="AUTHORIZATION: Bearer $env:token" push origin $(tag)'
  env:
      token: $(System.AccessToken)
git -c http.extraheader="AUTHORIZATION: Bearer ***" push origin 2019.07.01.3
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

將Bash腳本傳遞給憑據助手

根據bk2204的回答

- bash: 'git -c credential.helper=''!f() { echo "username=token"; echo "password=$(System.AccessToken)"; }; f'' push origin $(tag)'
git -c credential.helper='!f() { echo "username=token"; echo "password=***"; }; f' push origin 2019.07.02.9
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

使用環境變量將Bash腳本傳遞給憑證助手

來自bk2204的答案

- bash: 'git -c credential.helper=''!f() { echo "username=token"; echo "password=$env:token"; }; f'' push origin $(tag)'
  env:
      token: $(System.AccessToken)
git -c credential.helper='!f() { echo "username=token"; echo "password=$env:token"; }; f' push origin 2019.07.02.9
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

通過AUTHORIZATION: Basic ***作為額外標題

試圖模仿默認管道“簽出”任務的執行方式。

- bash: 'git -c http.extraheader="AUTHORIZATION: basic $(System.AccessToken)" push origin $(tag)'
git -c http.extraheader="AUTHORIZATION: basic ***" push origin 2019.07.02.10
fatal: unable to access 'https://github.com/respondentinc/website/': The requested URL returned error: 400

我在上述主題上嘗試了幾種變體,但沒有運氣。

我想念什么?

更新時間:2019-07-02

以上所有機制都是有效的身份驗證方法。 問題是System.AccessToken對GitHub無效。 文件表明它只是為了和Azure的管道服務的交互。

我找不到Azure Pipeline GitHub App提供的令牌變量。 我認為這並不重要,因為它是安裝令牌 我嘗試將此令牌與GitHubRelease任務一起使用,但由於缺少寫入權限,它仍然失敗。

為了使此工作正常進行,我使用了GitHubRelease任務,並且必須使用具有寫許可權的新OAuth令牌創建新的服務連接。

我仍然很好奇如何從Bash任務中訪問與服務連接關聯的令牌。

這里的問題是您正在嘗試使用Bearer身份驗證。 這對於Azure DevOps托管是正確的,但不適用於使用基本身份驗證的GitHub。

解決此問題的最佳方法是將環境變量TOKEN設置為具有所需的令牌(如您的嘗試),然后按如下所示運行命令:

git -c credential.helper='!f() { echo "username=token"; echo "password=$TOKEN"; };f' push $(tag)

如果您使用令牌,則可以接受任何用戶名; 我只是使用令牌,因為您需要一些用戶名,這是一個顯而易見的選擇。

通過使用shell函數作為憑據幫助程序並使用echo(這是內置的shell),您的憑據將不會出現在日志或進程輸出中。

我的解決方案是:

  1. 在GitHub上創建個人訪問令牌(PAT)

    在此處輸入圖片說明

  2. 在管道中創建秘密變量,該變量將保留PAT

    在此處輸入圖片說明

  3. 現在,您可以將PAT用於管道YAML配置中的git push

    git push https://$(GitHubPAT)@github.com/<your_org_and_project>.git

而且似乎您需要在push之前執行以下步驟(我正在使用PowerShell腳本步驟):

Add-Content "$HOME\.git-credentials" "https://$(GitHubPAT):x-oauth-basic@github.com"
git config --global user.email "<email>"
git config --global user.name "<name>"
git config --global --add url."git@github.com:".insteadOf "https://github.com/"

但是我不確定這些指令中的哪些可以省略。 但是按照這些步驟,我可以從管道中執行git push

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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