在PowerShell中更新Azure Release Variable值 [英] Update the Azure Release Variable value in PowerShell

查看:50
本文介绍了在PowerShell中更新Azure Release Variable值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Azure发布管道中,我具有发布变量$(ecomm)=是

I have Release Variable $(ecomm) = Yes, in Azure Release Pipeline

通过Powershell,我想更新$(ecomm)=否的值

Through Powershell, i want to update the value of $(ecomm) = No

Write-Host "Before update: "$(ecomm)
Write-Host "##vso[task.setvariable variable=ecomm;]No"
Write-Host "After update: "$(ecomm)

但是该值未更新.你能帮我这个忙吗?预先感谢.

But the value is not updating. Can you please help me on this. Thanks in advance.

推荐答案

Repcak是正确的.

Repcak is correct.

使用logging命令在Powershell中设置变量时,只能在管道运行"中更改变量的值,而不能在发行定义"中更改.

When you use the logging command to set variables in Powershell, you can only change the value of the variable in Pipeline Run instead of the Release Definition.

要在Powershell Task中更新发行版定义,您可以尝试以下管道设置:

To Update the Release definition in Powershell Task, you could try the following Pipeline Settings:

添加两个Powershell任务.

Add two Powershell tasks.

1.第一个PowerShell任务运行以下脚本:

1.The first PowerShell task run the following script:

Write-Host "##vso[task.setvariable variable=ecomm;]No"

此脚本用于在管道运行中更新变量值.

This script is used to update the variable values in pipeline run.

2.第二个Powershell任务运行以下脚本:

2.The second Powershell task run the following script:

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"


$pipeline.variables.ecomm.value = "$(ecomm)"


$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

注意:您还需要设置一些发布选项:

Note: You also need to set some release options:

  1. 选择以下选项:允许脚本访问 Releases->中的OAuth令牌.代理人职位

  1. Select the option: Allow scripts to access the OAuth token in Releases -> Agent Job

授予角色" 编辑"发布管道的权限: Project Collection Build Service(OrgName)

Grant the Edit release pipeline permission to the Role :Project Collection Build Service (OrgName)

结果:

有关更多详细信息,您可以参考

For more detailed information, you could refer to this ticket.

这篇关于在PowerShell中更新Azure Release Variable值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆