尽管值为真,但阶段条件未评估为真

Stage condition is not evaluating to true despite the value being true

我有一个阶段要运行。条件是true,但阶段一直被跳过,我不确定为什么。

这是舞台:

stages:
- stage: UnitTests
  displayName: Run unit tests on service...
  condition: eq(stageDependencies.Changed.Changes.outputs['detectChange.anyServicesChanged'], true)
  dependsOn: Changed
  variables: 
    test: $[ stageDependencies.Changed.Changes.outputs['detectChange.anyServicesChanged'] ]
  jobs:
  - job: UnitTests
    displayName: Running unit tests...
    steps:
    - bash: |
        echo $(test)

或者将其赋值给一个变量:

stages:
- stage: UnitTests
  displayName: Run unit tests on service...
  variables: 
    test: $[ stageDependencies.Changed.Changes.outputs['detectChange.anyServicesChanged'] ]
  condition: eq(variables.test, true)
  # dependsOn: Changed
  jobs:
  - job: UnitTests
    displayName: Running unit tests...
    steps:
    - bash: |
        echo $(test)

但是,如果我注释掉 conditiondependsOn,果然 echo $(test) 会打印出 true

stages:
- stage: UnitTests
  displayName: Run unit tests on service...
  # condition: eq(stageDependencies.Changed.Changes.outputs['detectChange.anyServicesChanged'], true)
  # dependsOn: Changed
  variables: 
    test: $[ stageDependencies.Changed.Changes.outputs['detectChange.anyServicesChanged'] ]
  jobs:
  - job: UnitTests
    displayName: Running unit tests...
    steps:
    - bash: |
        echo $(test)

Starting: Bash
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.182.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
Script contents:
echo true
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/119fb181-f890-4c1c-bffa-94142b0d51d9.sh
true
Finishing: Bash

这是检测到变化的阶段:

stages:
- stage: Changed
  displayName: Checks for changes in services and configs...
  jobs:
  - job: Changes
    displayName: Checking for changes in services and configs...
    steps:
    - bash: |
        mapfile -t servicesChanged < <(git diff  HEAD origin/production --name-only | awk -F'/' 'NF!=1{print }' | sort -u)
        echo "Total Changed: ${#servicesChanged[@]}"
        if [[ ${#servicesChanged[@]} > 0 ]]; then
          echo "Any services changed: True"
          echo "##vso[task.setvariable variable=anyServicesChanged;isOutput=true]true"
        fi
      name: detectChange

所以我不确定我在这里做错了什么,当 conditiondependsOn 处于活动状态时,它一直跳过这个阶段。有什么建议吗?

当你在一个阶段使用这个条件(Stage depending on job output)时,你必须使用dependencies变量,而不是stageDependencies。因此以下语法应该有效。

- stage: UnitTests
  displayName: Run unit tests on service...
  condition: eq(dependencies.Changed.outputs['Changes.detectChange.anyServicesChanged'], true)
  dependsOn: Changed

有关详细信息,请参阅:Stage depending on job output