在发布期间更新 Azure DevOps 发布管道变量

问题描述 投票:0回答:1

我有一个经典的Release Pipeline,其中设置了一些自定义变量,在整个发布过程中定期使用。

发布的前两步是两个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)在变量选项卡中设置,它的范围是 "Release","Setable at release time "被选中。我也试过使用 Write-Host '##vso[task.setvariable variable=ReleaseVersion]$(Build.BuildNumber)' 来设置我的变量,但我得到了同样的结果。

更新添加新的SCRIPT和LOG描述。

所以我刚刚在我的流水线上推送了新的代码。构建的编号是 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:驱动器。

azure powershell azure-devops azure-pipelines-release-pipeline
1个回答
0
投票

当我使用上面的代码与REST API ReleaseVersion保持不变。然而,我使用了日志记录命令 Write-Host '##vso[task.setvariable variable=ReleaseVersion]$(Build.BuildNumber)' 和ReleaseVersion被更新了,但在下一步有了新的值。所以新的值应该在 Copy Files 因为它是下一步。


0
投票

在发布期间更新Azure DevOps发布管道变量。

首先,之所以 记录命令 不适合你的是。

日志命令设置的变量只能用于 内行的值,并且不会修改 网状结构变量.

所以,我们可以在下一个任务中使用set变量,但如果你想更新变量 变量选项卡上的,你可以使用REST API (定义--更新)来更新发布管道中发布定义变量的值。

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 中的网址。

你可以查看我之前的帖子了解更多细节。

希望能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.