我遇到了一個問題,我想用引數“BuildVersion”替換正文中下面的文本“Test to Output”。API POST 在硬編碼時作業,但我希望它寫回運行時捕獲的 AUT 的 exe,并添加到 currentBuild.description,但只是無法弄清楚語法。誰能指出我正確的方向?它是一個帶有 PowerShell 的 Jenkins 腳本,用于 POST 請求。
def BuildVersion = currentBuild.description
echo BuildVersion
powershell '''
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "token")
$body = "{`n `"value`": `"Test to Output`"`n}"
$response = Invoke-RestMethod 'https://api.clickup.com/api/v2/task/task_id/field/field_id' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
'''
}
uj5u.com熱心網友回復:
用于withEnv將 Groovy 變數作為環境變數傳遞到 PowerShell。這比使用易受腳本注入影響的字串插值更安全。
用于powershell returnStdout: true將 PowerShell 的輸出作為字串獲取,然后使用JsonSlurper.
我不知道來自 REST API 的 JSON 格式,所以我寫了???代替您必須使用的 JSON 路徑。
import groovy.json.JsonSlurper
def BuildVersion = currentBuild.description
echo BuildVersion
def responseJson = ''
withEnv(["BuildVersion=$BuildVersion"]) {
responseJson = powershell returnStdout: true, script: '''
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "token")
$body = @{ value = $env:BuildVersion } | ConvertTo-Json
$response = Invoke-RestMethod 'https://api.clickup.com/api/v2/task/task_id/field/field_id' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
'''
}
def responseData = new JsonSlurper().parseText( responseJson )
// TODO: Replace ??? by the JSON path
currentBuild.description = responseData.???
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/510165.html
標籤:电源外壳詹金斯
