使用Jenkins API促进构建

时间:2017-04-09 21:07:58

标签: jenkins build

鉴于Jenkins构建作业具有不同的促销作业(即,将构建促进到不同的环境),如何使用Jenkins API触发特定构建的特定促销作业?

2 个答案:

答案 0 :(得分:2)

来自不同来源的综合答案提出了这个问题:

$Username = "Username"
$APItoken = '12345'
$Credential = "$($Username):$($APItoken)"
$EncodedCredential = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($Credential))
$BasicAuthValue = "Basic $EncodedCredential"
$Headers = @{
                Authorization = $BasicAuthValue
            }

Write-Output "Promoting build $LatestBuildNumber to Environment..."

Invoke-WebRequest -URI "http://jenkins.prd.company.com/job/jobname/buildnumber/promotion/forcePromotion?name=PromoteToEnvironment" -Headers $Headers

答案 1 :(得分:2)

我知道这是一个老话题,但这只是为了帮助社区。

使用CURL的Shell解决方案:

user_name="jenkins_user"
    user_token="token" 
    promotion_name="Test_Promote"
    jenkins_url="http://build-server.com"
    JOB_NAME="job_name"
    JOB_NO="job-no"

    url="--silent -u $user_name:$user_token $jenkins_url/job/$JOB_NAME/$JOB_NO/promotion/forcePromotion?name=$promotion_name"
    curl $url

如何生成詹金斯用户令牌:https://jenkins.io/blog/2018/07/02/new-api-token-system/