![](/img/trans.png)
[英]How to pass a parameter to a powershell script in a jenkins pipeline
[英]How to pass jenkins credential parameter as Authentication header in powershell http post request
我正在嘗試使用 powershell post call http://pathandport//computer/agentname/api/json?pretty=true
訪問以下 POST 調用
從 jenkins,我正在調用一個 powershell 文件,該文件使用從 jenkins 以純文本形式傳遞的用戶名和密碼訪問其余 api
$pair= "$($username):$($password)"
$encode= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$Auth= "Basic $encode"
$Headers = @{ Authorization = $Auth }
$url = 'http://<path>:<port>//computer/agentname/api/json?pretty=true'
$response = Invoke-WebRequest -Uri $url -Headers $Headers
當我們以純文本形式傳遞用戶名和密碼時,該場景工作正常。
最近開始了解 jenkins 憑據參數(帶密碼的用戶名),它在其中獲取用戶名和密碼,使用其內置邏輯編碼為令牌,並生成類似於“1234-abcd-5678-efgh”的值。
我現在的問題是,我無法在具有上述實現的 powershell 請求中將此令牌作為 Auth 值傳遞。
幫助我如何處理這個
在 Jenkins 中使用憑據以及您引用的用戶名和密碼憑據時,Jenkins 將用戶名:密碼對公開為環境變量,但也會單獨公開用戶名和密碼。
environment {
BITBUCKET_COMMON_CREDS = credentials('jenkins-bitbucket-common-creds')
}
這實際上設置了以下三個環境變量:
BITBUCKET_COMMON_CREDS - 包含以冒號分隔的用戶名和密碼,格式為 username:password。
BITBUCKET_COMMON_CREDS_USR - 僅包含用戶名組件的附加變量。
BITBUCKET_COMMON_CREDS_PSW - 僅包含密碼組件的附加變量。
所以你的 powershell 應該是這樣的:
$pair= "$MY_CREDENTIALS"
# or you could also do this
# $pair= "$MY_CREDENTIALS_USR:$MY_CREDENTIALS_PSW"
$encode= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$Auth= "Basic $encode"
$Headers = @{ Authorization = $Auth }
$url = 'http://<path>:<port>//computer/agentname/api/json?pretty=true'
$response = Invoke-WebRequest -Uri $url -Headers $Headers
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.