更新 Azure 管道中的变量

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

我在 Azure 上有一个 CI/CD 管道。我已经设置了一个 .yaml 文件和一个名为 SerialNumber 的变量,直接在 Azure 界面中(而不是在 .yaml 中)设置为 000000。我希望它在管道内部使用并更新,在每次迭代时添加 1。

.yaml 看起来像这样:

variables:
  myPipelineVariable: $[variables.SerialNumber]

jobs:

- job: Update_Variable
  timeoutInMinutes: 0
  steps:
  - task: PowerShell@2
    name: "task1"
    displayName: this is task1
    inputs:

      targetType: 'inline'
      script: |
        echo "the value of the variable is : $(myPipelineVariable)"
        $newValue = [int]$(myPipelineVariable) + 1
        $formattedValue = "{0:D6}" -f $newValue  # Format the number with leading zeros
        Write-Host "##vso[task.setvariable variable=myPipelineVariable;isOutput=true]$formattedValue"
    enabled: true

问题是,如果我保存并运行管道,Azure 界面中的数字不会改变,下次又从 000000 开始。我还尝试在 .yaml 中插入另一个作业:

- job: Show_Variable
  timeoutInMinutes: 0
  steps:
  - task: PowerShell@2
    name: "task2"
    displayName: this is task2
    inputs:

      targetType: 'inline'
      script: |
        echo "the new value of the variable is : $(myPipelineVariable)"
    enabled: true

但它打印:000000

我如何处理此操作来更新 Azure 内部变量的值,以便我触发的每个构建都使用它,然后自动将数字加一?

azure azure-pipelines
1个回答
0
投票

你不能那样改变变量。它只会用于执行,不会被持久化。

但是您正在寻找的是一个

counter
表达式

您可以创建一个计数器,该计数器在管道的每次执行中自动加一。定义计数器时,您需要提供前缀和种子。这是一个演示这一点的示例。

variables:
  major: 1
  # define minor as a counter with the prefix as variable major, and seed as 100.
  minor: $[counter(variables['major'], 100)]

steps:
- bash: echo $(minor)

查看文档这里

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