如何从 PowerShell 设置 Azure 管道变量
How To set Azure pipeline variable from PowerShell
我正在尝试在 PowerShell 中设置 Azure 管道变量值。我在 Azure 管道中创建了一个变量 winversion
。现在,在 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。
我想通过 Powershell 更新这个变量。我正在使用一个调用 API 的 PowerShell 脚本并尝试更新其变量,但出现找不到页面错误:-
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
这里有点混乱,你在powershell脚本中使用了变量$winversion
,但是在管道变量中设置了变量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) 来更新该值来自构建任务的构建管道定义变量。
有个跟帖很相似,具体可以查看答案:
注意:将 API 更改为构建定义:
PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0
希望这对您有所帮助。
我发现这个 link 有用:https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell
这里包含您可以执行的操作的完整选项:
https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch
您可以在任务之间重用设置变量,也可以在作业之间重用设置变量。
我在舞台上找不到任何东西。
总结:
jobs:
# Set an output variable from job A
- job: A
pool:
vmImage: 'vs2017-win2016'
steps:
- powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
name: setvarStep
- script: echo $(setvarStep.myOutputVar)
name: echovar
# Map the variable into job B
- job: B
dependsOn: A
pool:
vmImage: 'ubuntu-16.04'
variables:
myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ] # map in the variable
# remember, expressions require single quotes
steps:
- script: echo $(myVarFromJobA)
name: echovar
我正在尝试在 PowerShell 中设置 Azure 管道变量值。我在 Azure 管道中创建了一个变量 winversion
。现在,在 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。
我想通过 Powershell 更新这个变量。我正在使用一个调用 API 的 PowerShell 脚本并尝试更新其变量,但出现找不到页面错误:-
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
这里有点混乱,你在powershell脚本中使用了变量$winversion
,但是在管道变量中设置了变量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) 来更新该值来自构建任务的构建管道定义变量。
有个跟帖很相似,具体可以查看答案:
注意:将 API 更改为构建定义:
PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0
希望这对您有所帮助。
我发现这个 link 有用:https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell
这里包含您可以执行的操作的完整选项: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch
您可以在任务之间重用设置变量,也可以在作业之间重用设置变量。 我在舞台上找不到任何东西。
总结:
jobs:
# Set an output variable from job A
- job: A
pool:
vmImage: 'vs2017-win2016'
steps:
- powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
name: setvarStep
- script: echo $(setvarStep.myOutputVar)
name: echovar
# Map the variable into job B
- job: B
dependsOn: A
pool:
vmImage: 'ubuntu-16.04'
variables:
myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ] # map in the variable
# remember, expressions require single quotes
steps:
- script: echo $(myVarFromJobA)
name: echovar