简体   繁体   English

如何更改管道变量以在 Azure DevOps 的下一个构建中使用

[英]How to change pipeline variables for usage in the next build in Azure DevOps

The case I have is as follow: I've created an Azure DevOps Pipeline with a Pipeline variable, let's say 'variable A'.我的情况如下:我创建了一个带有管道变量的 Azure DevOps 管道,比如说“变量 A”。 The value of 'variable A' is 1. During the build, I change the value of 'variable A' to 2. “变量 A”的值为 1。在构建过程中,我将“变量 A”的值更改为 2。

When the build runs for the second time I want the value of these 'variable A' but this is back 1 but I want that the value is 2 because on the previous build I set the value of 'variable A' to 2.当构建第二次运行时,我想要这些“变量 A”的值,但这又回到了 1,但我希望该值为 2,因为在之前的构建中,我将“变量 A”的值设置为 2。

These are the methods I tried without success:这些是我尝试但没有成功的方法:

Method 1:方法一:

Write-Host "##vso[task.setvariable variable=A;]2"

Method 2:方法二:

$env:A = 2

The only thing that works but I don't think this is the way to go is to get the whole build definition via the rest api and put it back with the value of the variable changed.唯一可行但我不认为这是要走的路是通过其余 api 获取整个构建定义,然后将其放回并更改变量的值。

Is there any other solution to this problem?这个问题还有其他解决方案吗?

If you're specifically looking at an increasing number, then you can also use counters.如果您特别关注不断增加的数字,那么您也可以使用计数器。 These only woork in YAML based build definitions.这些仅适用于基于 YAML 的构建定义。

The format is as follows:格式如下:

You can use any of the supported expressions for setting a variable.您可以使用任何受支持的表达式来设置变量。 Here is an example of setting a variable to act as a counter that starts at 100, gets incremented by 1 for every run, and gets reset to 100 every day.这是一个将变量设置为计数器的示例,该计数器从 100 开始,每次运行递增 1,每天重置为 100。

yaml yaml

jobs:
- job:
  variables:
    a: $[counter(format('{0:yyyyMMdd}', pipeline.startTime), 100)]
  steps:
    - bash: echo $(a)

For more information about counters and other expressions, see expressions .有关计数器和其他表达式的更多信息, 请参阅表达式

The counter is stored for the pipeline and is based on the prefix you provide in the counterr expression.计数器是为管道存储的,并且基于您在 counterr 表达式中提供的前缀。 The above expression uses the yyyymmdd to generate a prefix which is unique every day.上面的表达式使用yyyymmdd生成每天唯一的前缀。


For UI driven build definitions, then indeed using the REST api to update the whole definiton would work, though it's really hard to work around all possibilities concerning paralelism.对于 UI 驱动的构建定义,确实使用 REST api 来更新整个定义是可行的,尽管很难解决有关并行性的所有可能性。

How to change pipeline variables for usage in the next build in Azure DevOps如何更改管道变量以在 Azure DevOps 的下一个构建中使用

I am afraid you have to use the rest api to change the value of that pipeline variables.恐怕您必须使用其余的 api 来更改该管道变量的值。

That because when you use the script `"##vso[task.setvariable variable=testvar;]testvalue" to overwrite it, the overwrite value only work for current build pipeline .那是因为当您使用脚本 `"##vso[task.setvariable variable=testvar;]testvalue" 覆盖它时,覆盖值仅适用于当前构建管道

When you use the execute the build again, it will still pull the value from pipeline variable value.当您再次使用执行构建时,它仍然会从管道变量值中提取值。

So, we have to update the value of that variables on the web portal.因此,我们必须在门户网站上更新该变量的值。 Then we need use the need the REST API ( Definitions - Update ) to update the value of the build pipeline definition variable from a build task:然后我们需要使用 REST API ( Definitions - Update ) 从构建任务中更新构建管道定义变量的值:

Similar thread: How to modify Azure DevOps release definition variable from a release task?类似线程: 如何从发布任务中修改 Azure DevOps 发布定义变量?

Note:Change the API to the build definitions:注意:将 API 更改为构建定义:

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0

Hope this helps.希望这可以帮助。

I have found the easiest way to update variable values during a pipeline execution is to use the Azure CLI having also tried other methods with little or no success.我发现在管道执行期间更新变量值的最简单方法是使用 Azure CLI,但也尝试了其他方法但收效甚微或没有成功。

In a YAML pipeline, this may look something like so:在 YAML 管道中,这可能看起来像这样:

jobs:
  - job: Update_Version
    steps:
    - task: AzureCLI@2
      inputs:
        azureSubscription: [your_subscription_id]
        scriptType: 'pscore'
        scriptLocation: 'inlineScript'
        inlineScript: |
          # set environment variable for current process
          $env:AZURE_DEVOPS_EXT_PAT = $env:SYSTEM_ACCESSTOKEN

          $oldVersionNumber = $(version-number)
          $newVersionNumber = $oldVersionNumber + 1
          az pipelines variable-group variable update --group-id [your_variable_group_id] --name version-number --organization [your_organization_url] --project [your_project_name] --value $newVersionNumber 
      env:
        SYSTEM_ACCESSTOKEN: $(System.AccessToken)

The pipeline build service may also need permission to execute this command.管道构建服务可能还需要执行此命令的权限。 To check, go to Pipelines -> Library -> Variable groups then edit the variable group containing your variable.要检查,请转到管道 -> 库 -> 变量组,然后编辑包含变量的变量组。 Click on the Security button and make sure the user Project Collection Build Service has the Administrator role.单击 Security 按钮并确保用户Project Collection Build Service具有管理员角色。

More information on the Azure CLI command can be found here .可以在此处找到有关 Azure CLI 命令的更多信息。 There is also another form of the command used to update variables that are not in a variable group, as described here .还有另一种形式的命令用于更新不在变量组中的变量,如此所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM