我有一个经典的发布管道,其中包含一些自定义变量集,这些变量在整个发布中会定期使用。
该版本的前两个步骤是两个PowerShell脚本。在第一个中,我基本上采用了一个变量,并为其分配了一个值。然后输出变量更新到的内容,以便确保它是正确的(始终是)。第二个脚本执行的操作基本上相同,但是变量不同,并且在我输出值时,它总是正确的。
现在,随着发行的进行,如果我切换到“变量”选项卡,则可以看到该变量从未更新过。仍将其设置为先前版本中的值。对我来说,这是一个主要问题,因为在发行版的大约一半之前,我有一个“复制文件”步骤,它将文件复制到由这些变量创建的文件夹路径中。由于这些变量来自以前的发行版,因此会将它们放入名称不正确的文件夹路径中。
在发布管道完成并成功执行之后,然后在“变量”选项卡上正确更新管道变量。
有没有一种方法可以在发行期间更新这些变量,而不是在发行完成后进行更新?我希望在我的PowerShell脚本中或作为管道中的设置正确更新它们。
这基本上就是我的脚本的样子:
$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
$buildNumber = $env:BUILD_BUILDNUMBER
$pipeline.variables.ReleaseVersion.value = $buildNumber
####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99
$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
write-host "================================="
write-host "The value of ReleaseVersion updated to " $updatedef.variables.ReleaseVersion.value
write-host "================================="
我的变量(上面脚本中的ReleaseVersion)在“变量”选项卡中设置,其作用域为“发布”,并选中了“发布时可设置”。我还尝试使用Write-Host '##vso[task.setvariable variable=ReleaseVersion]$(Build.BuildNumber)'
设置变量,但得到的结果相同。
更新 添加新的脚本和日志描述
所以我只是通过管道推送了新代码。内部版本编号为v2.1.0.16
(正确)。我将内部版本号设置为分支名称,并在末尾添加一个(:.rev)
变量。
管道中的第一个脚本设置了一个名为ReleaseVersion
的自定义管道变量。它设置为内部版本号。这是脚本:
$url = "https://vsrm.dev.azure.com/{organization}/$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
$buildNumber = $env:BUILD_BUILDNUMBER
$pipeline.variables.ReleaseVersion.value = $buildNumber
####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99
$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
write-host "================================="
write-host "The value of ReleaseVersion updated to " $updatedef.variables.ReleaseVersion.value
write-host "================================="
和我的输出:
2020-05-11T03:02:21.5631557Z "id": #,
2020-05-11T03:02:21.5632209Z "name": "#",
2020-05-11T03:02:21.5632816Z "path": "#",
2020-05-11T03:02:21.5633410Z "projectReference": null,
2020-05-11T03:02:21.5634223Z "url": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/definitions/{definitionId}",
2020-05-11T03:02:21.5635058Z "_links": {
2020-05-11T03:02:21.5635643Z "self": {
2020-05-11T03:02:21.5636500Z "href": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/definitions/{definitionId}"
2020-05-11T03:02:21.5637445Z },
2020-05-11T03:02:21.5641618Z "web": {
2020-05-11T03:02:21.5643197Z "href": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/definitions/{definitionId}"
2020-05-11T03:02:21.5644085Z }
2020-05-11T03:02:21.5647518Z }
2020-05-11T03:02:21.5648322Z }
2020-05-11T03:02:22.4291104Z =================================
2020-05-11T03:02:22.4456531Z The value of ReleaseVersion updated to v2.1.0.15
2020-05-11T03:02:22.4483349Z =================================
2020-05-11T03:02:23.0643676Z ##[section]Finishing: Update Release Version
它表示该值已更新为v2.1.0.15
,这是当前版本之后的一个修订版本。然后,我有了一个脚本,可以根据上述变量创建文件夹路径。它将检查具有该名称的文件夹(例如v2.1.0.15
),如果已经找到,将附加-1
,如果已经找到-2
,则将附加-1
, 等等。它将文件夹名称设置为名为RootDirectory
的自定义管道变量。这是脚本:
$url = "https://vsrm.dev.azure.com/{organization}/$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
write-host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
$rootDirectory = $pipeline.variables.BuildDirectory.value + "\" + $pipeline.variables.ReleaseVersion.value
if (test-path $rootDirectory) {
$newDirectory=$rootDirectory
$index=0
while (test-path $newDirectory) {
$index += 1
$newDirectory=$rootDirectory + "-" + $index
}
}
$pipeline.variables.RootDirectory.value = $newDirectory
####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99
$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
write-host "================================="
write-host "The value of Root Directory updated to " $updatedef.variables.RootDirectory.value
write-host "================================="
BuildDirectory
变量是一个不变的硬编码变量。它的输出是:
2020-05-11T15:32:45.7255688Z "id": #,
2020-05-11T15:32:45.7255852Z "name": "#",
2020-05-11T15:32:45.7256001Z "path": "#",
2020-05-11T15:32:45.7256166Z "projectReference": null,
2020-05-11T15:32:45.7256379Z "url": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/definitions/{definitionId}",
2020-05-11T15:32:45.7256556Z "_links": {
2020-05-11T15:32:45.7256698Z "self": {
2020-05-11T15:32:45.7256903Z "href": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/definitions/{definitionId}"
2020-05-11T15:32:45.7257174Z },
2020-05-11T15:32:45.7257331Z "web": {
2020-05-11T15:32:45.7257537Z "href": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/definitions/{definitionId}"
2020-05-11T15:32:45.7257721Z }
2020-05-11T15:32:45.7257862Z }
2020-05-11T15:32:45.7258016Z }
2020-05-11T15:32:46.6474274Z =================================
2020-05-11T15:32:46.6481861Z The value of Root Directory updated to
2020-05-11T15:32:46.6491120Z =================================
2020-05-11T15:32:46.7209281Z ##[section]Finishing: Create Root Directory
请注意该脚本的输出如何为空白,它不会显示变量已更新为的内容。
在我的管道的后续步骤中,我实际上是在使用这些变量的地方。它在“复制文件”管道任务中。目标文件夹设置为$(RootDirectory)/Debug
,但是由于RootDirectory变量未更新,因此仅将文件复制到根c:驱动器。
答案 0 :(得分:0)
当我将以上代码与REST API一起使用时,ReleaseVersion保持原样。但是,我使用了日志记录命令Write-Host '##vso[task.setvariable variable=ReleaseVersion]$(Build.BuildNumber)'
,并且ReleaseVersion已更新,但是在下一步中可以使用新值。因此,下一步应该在Copy Files
中使用新值。
答案 1 :(得分:0)
在发行期间更新Azure DevOps发行管道变量
首先,logging command对您不起作用的原因是:
由logging命令设置的变量只能在当前代理内部使用,而不能修改 Web Potral变量的值。
因此,我们可以在下一个任务中使用set变量,但是如果您想在“变量”标签上更新变量 ,则可以使用REST API(Definitions - Update)从发布管道更新发布定义变量的值:
PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions/{definitionId}?api-version=5.0
然后,我检查了您的脚本,它接近正确的答案。它对您不起作用的原因是,您在$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)
中使用了变量url
。
该变量的默认值为https://dev.azure.com/YourOrganization/
。但是,我们使用的API是https://vsrm.dev.azure.com/{organization}
,在网址中缺少vsrm
。
您可以查看我以前的主题以获取更多详细信息。
希望这会有所帮助。