如何在Azure Pipelines步骤中将变量用作条件?

时间:2020-03-17 18:38:31

标签: azure

Azure管道定义变量和条件,以用于不同的管道任务,步骤和很快的任务。在定义以下YAML管道时,仅当变量ReleaseOnlyScript评估的组合条件为isValidReleaseBuild时,才应执行步骤true。在Azure Pipeline DevOps站点中,定义了变量BuildType并将其设置为release

variables:
  - name: isValidReleaseBuild
    value: $[and(eq(variables['BuildType'], 'release'), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))]

steps:
  - script: |
      echo "BuildType: $(BuildType)"
      echo "SourceBranch: $(Build.SourceBranch)"
      echo "ReleaseBuild: $(isValidReleaseBuild)"
    displayName: 'Buildinfo'
  - ${{ if eq(variables['isValidReleaseBuild'], true) }}:
    - script: |
        echo "YEAH its a Release!"
      displayName: 'ReleaseOnlyScript'

运行管道时,仅使用以下输出执行脚本BuildInfo

BuildType: release
SourceBranch: refs/tags/v1.0.0
ReleaseBuild: True

为什么ReleaseOnlyScript未执行?

更新: 谢谢Alex AIT的回答,我如下修改了管道,现在它可以按预期运行。

variables:
  - name: isValidReleaseBuild
    value: $[and(eq(variables['BuildType'], 'release'), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))]

jobs:
  - job: check_cicd
    steps:
      - script: |
          echo "BuildType: $(BuildType)"
          echo "SourceBranch: $(Build.SourceBranch)"
          echo "ReleaseBuild: $(isValidReleaseBuild)"
          if [ "$(isValidReleaseBuild)" = "True" ]; then
            echo "##vso[task.setvariable variable=ValidBuild;isOutput=true]True"
          else
            echo "##vso[task.setvariable variable=ValidBuild;isOutput=true]False"
          fi
        name: buildinfo
  - job: release_cicd
    dependsOn: check_cicd
    condition: eq(dependencies.check_cicd.outputs['buildinfo.ValidBuild'], 'True')
    steps:
      - template: build-release.yml

1 个答案:

答案 0 :(得分:2)

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml

您必须在步骤中使用condition,而不是开头的表达式。

parameters:
  doThing: false

steps:
- script: echo I did a thing
  condition: and(succeeded(), eq('${{ parameters.doThing }}', true))