【发布时间】:2023-04-04 23:26:01
【问题描述】:
我有一个带有一些管道变量的 Azure DevOps 构建管道。
我想在排队之前使用 powershell 脚本自动更改其中一个变量。 有可能吗?
或者还有其他方法可以解决这个问题吗?
【问题讨论】:
标签: c# powershell azure-devops
我有一个带有一些管道变量的 Azure DevOps 构建管道。
我想在排队之前使用 powershell 脚本自动更改其中一个变量。 有可能吗?
或者还有其他方法可以解决这个问题吗?
【问题讨论】:
标签: c# powershell azure-devops
“20.2.35”。 20.2 部分应该每年手动更新一次,但 .35 应该每周更新一次,这是周数
恐怕没有什么变量可以做到周增量。但是你可以使用scheduled triggers和counter variable来实现类似的功能。
步骤如下:
Pipeline -> Library 中创建一个变量组。例子:
管道 1 使用 powershell 任务运行 Rest API 以更新变量。
trigger:
- none
schedules:
- cron: "0 12 * * 0"
displayName: Weekly Sunday build
branches:
include:
- master
variables:
- name: version
value: $[counter('',36)]
pool:
vmImage: 'ubuntu-latest'
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$token = "PAT"
$url="https://dev.azure.com/{Org name}/_apis/distributedtask/variablegroups/{Group ID}?api-version=6.1-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
"variables":{"{variable name}":{"value":"$(version)"}},"id":{Group ID},"type":"Vsts","name":"{Variable Group name}","variableGroupProjectReferences":[{"projectReference":{"id":"{Project ID}","name":"{Project Name}"},"name":"{Variable Group Name}"}]
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PUT -Body $JSON -ContentType application/json
管道 2. 使用变量设置 Buildnumber。
name: 20.2 $(123)
trigger:
- none
variables:
- group: test
pool:
vmImage: 'ubuntu-latest'
steps:
- script: echo Hello, world!
displayName: 'Run a one-line script'
说明:
Pipeline1 将每周运行一次以更新变量值。并且该值将增加 1。Pipeline2 使用此变量来设置内部版本号。该值是在流水线运行前设置好的,可以使用$(build.buildnumber)引用。
【讨论】:
该变量必须在管道中声明,您可以使用输出设置一个值(来自 cmd、powerhsell、python、exe、...):
##vso[task.setvariable variable=MyVariable;]NewValue
在 PowerShell 中,您可以使用 Write-Host :
Write-Host "##vso[task.setvariable variable=MyVariable;]NewValue"
【讨论】: