【发布时间】:2020-05-11 23:51:32
【问题描述】:
我有一个经典的发布管道,其中设置了一些自定义变量,这些变量会在整个发布过程中定期使用。
该版本的前两个步骤是两个 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,如果已经找到-1,则附加一个-2,依此类推。它将文件夹名称设置为名为@987654333@ 的自定义管道变量。这是脚本:
$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: 驱动器。
【问题讨论】:
-
这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?
-
我经历了很多事情,但还没有回到这个话题。明天我会为你更新。感谢您入住。
标签: azure powershell azure-devops azure-pipelines-release-pipeline