continuous-integration - 如何从 PowerShell 设置 Azure 管道变量

标签 continuous-integration azure-devops azure-pipelines continuous-deployment

我正在尝试在 PowerShell 中设置 Azure 管道变量值。我创建了一个变量 winversion在 Azure 管道中。现在,在 PowerShell 任务中,我想为 winversion 分配一些值。多变的。
我的简单问题是如何在运行时更改 Azure PipeLine 变量的值?

Write-Host "Main value is $winversion"
$env:WINVERSION="abhinav";
Write-Host "Modified value is $env:WINVERSION"
Write-Host "Main value is $(winversion)"
第一行打印:原始值为 123
三行打印:修改后的值为 abhinav
第四行打印:123
我希望当我将 winversion 的值从“123”更改为“abhinav”时,它实际上将管道变量值更改为 abhinav。
enter image description here
enter image description here
我想通过 Powershell 更新这个变量。我正在使用一个 PowerShell 脚本调用 API 并尝试更新其变量,但发现页面未找到错误:-
param(
[string]$winVersion
)
$body = "{ 'definition' : { 'id' :85}
}"
$valueName="Winver"
$definitionId=85
$User=""
$Password=""
$base64authinfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $Password)))
$Uri = "https://Muac.visualstudio.com/OSGCXE/_apis/release/releases?api-version=2.0"
$urlDef = "https://Muac.visualstudio.com/OSGCXE/_apis/release/definitions/" + $definitionId + "?api-version=2.0"
$definition = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64authInfo)} -Method Get -Uri $urlDef 

#Write-Host $definition

$definition.variables.$valueName.Value = "$winVersion"
$definitionJson = $definition | ConvertTo-Json -Depth 50 -Compress

#Write-Host (ConvertTo-Json $definition -Depth 100)

$update=Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64authInfo)} -Method Put -Uri $urlDef -Body $definitionJson -ContentType "application/json"

#Write-Host "$update"

#$buildresponse = Invoke-RestMethod -Method Post -ContentType application/json -Uri $Uri -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $body

#write-Host $buildresponse.status

最佳答案

How To set azure pipeline variable from PowerShell



这里有点困惑,你使用变量$winversion在 powershell 脚本中,但变量设置为 testvar在管道变量中。

无论如何,不​​管我们像你一样直接覆盖管道变量值,还是使用脚本"##vso[task.setvariable variable=testvar;]testvalue"要覆盖它,覆盖值仅适用于当前构建管道。当您使用 $(winversion)要获取值,它仍然会从管道变量值中提取值。要获取当前值,您需要使用 $env:WINVERSION .

另外,你说:

I want when I change the value of winversion from "123" to "abhinav" so it actually changes the pipeline variable value to abhinav.



如果您的意思是要更改 上的管道变量值门户网站 ,您需要 REST API ( Definitions - Update ) 来更新来自构建任务的构建管道定义变量的值。

有一个非常相似的线程,您可以查看答案以了解详细信息:

How to modify Azure DevOps release definition variable from a release task?

注意:将 API 更改为构建定义:
PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0

希望这可以帮助。

关于continuous-integration - 如何从 PowerShell 设置 Azure 管道变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55472792/

相关文章:

Jenkins :让一个工作等待另一个工作正在运行

testing - "integration testing"、 "continuous integration servers"和 "nightly builds"之间的关系是什么?

continuous-integration - 使用 TeamCity 部署 umbraco

visual-studio - 是否有内置方法可以设置持续交付到专用(非 Azure)服务器?

azure-devops - Azure DevOps 任务作业之间的可变 secret issecret=true

tfs - VSTS : Using $(BuildID) as an argument in a build step

unit-testing - 如何在 Github 中显示我的测试通过/失败?

azure-devops - 在 CLI 任务中使用服务连接?

azure - 续订 Azure Key Vault 中存储的证书的最佳替代方法是什么?

ios - 如何使用 Azure DevOps 增加 xcode 项目的内部版本号