如何在Azure管道的步骤中使用变量作为条件?

0lvr5msh  于 2023-02-16  发布在  其他
关注(0)|答案(1)|浏览(151)

Azure管道定义用于不同管道任务、步骤等的变量和条件。定义以下YAML管道时,仅当变量isValidReleaseBuild计算的组合条件为true时,才应执行步骤ReleaseOnlyScript。在Azure管道开发运营站点中,定义了变量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
bwleehnv

bwleehnv1#

https://learn.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))

相关问题