如果您更喜欢配置管道GUI,您必须运行脚本来添加我的变量 as 变量通过调用此来代替临时变量api https://learn.microsoft.com/en-us/rest/api/azure/devops/build/definitions/update?view=azure-devops-rest-5.1。因为之后agent job1
完成,您刚刚定义的变量$MyVariable = "Proceed"
不会转移到下一个代理工作。这agent job1
and agent job2
彼此独立。
在代理作业1中:
配置 2 个 powershell 任务。
(1)第一个用于定义一个带有值的变量并将其设置为输出变量:
echo "Hello-World"
echo "##vso[task.setvariable variable=MyVariable;isOutput=true]Proceed"
echo $MyVariable
不要忘记指定其reference
name 我的变量输出在这个任务中。
(2)第二个作业就是用来添加这个的output variable
into Variables
,那么agent job2
可以访问它:
$connectionToken="{token}"
$urlget = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases/$(Release.ReleaseId)?api-version=5.1"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$getdef = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET -ContentType application/json -Uri $urlget
##Write-Host Pipeline = $($getdef | ConvertTo-Json -Depth 100)
$MyVariable=@"
{
"value": "$(MyVariableOutput.MyVariable)"
}
"@
$getdef.variables | add-member -Name "MyVariable" -value (Convertfrom-Json $MyVariable) -MemberType NoteProperty -Force -PassThru
$getdef = $getdef | ConvertTo-Json -Depth 100
$getdef | clip
$urlput = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases/$(Release.ReleaseId)?api-version=5.1"
$putdef = Invoke-RestMethod -Uri $urlput -Method PUT -Body $getdef -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
上面的脚本将创建一个变量MyVariable
它的值是Proceed
.
In agent job2
,配置条件如下图:
eq(variables['MyVariable'],'Proceed')
您可以看到代理 job2 可以成功运行,因为它检测到了MyVariable
is Proceed
.