Azure管道:如何在条件语句中使用输出变量

3
我是一名有用的助手,可以为您翻译文本。
我正在处理一个运行在Windows自托管代理上的流水线。我需要调用一个脚本,在一个阶段中初始化一个变量,并在下一个阶段的条件中使用该变量。
以下是我的Yaml:
stages:
  - stage: Init
    jobs:
    - job: RunScript
      steps:
        - task: PowerShell@2
          name: compareFiles
          inputs:
            targetType: filePath
            filePath: '***\compareFileContent.ps1'

  - stage: DisplayStage
    dependsOn: Init
    variables:
      areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
    jobs:
    - job: Display
      steps:
        - script: |
            echo $(areFilesDifferent)

  - stage: DeploymentStage
    dependsOn: DisplayStage
    variables:
      areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
    condition: eq( '$(areFilesDifferent)', 'true' )
    jobs:
    - deployment: DeploymentJob
      environment: 'ATest'
      strategy:
        runOnce:
          deploy:
            steps:
              - checkout: none
              - task: CmdLine@2
                inputs:
                  script: |
                    echo EnvName Finally: $(areFilesDifferent)

这是我的PowerShell代码:
'***\compareFileContent.ps1'

$equal = $filetext1 -ceq $filetext2 # case sensitive comparison
$equalString = (&{If($equal) {"true"} Else {"false"}})
echo "##vso[task.setvariable variable=areDifferent;isOutput=true]$equalString"

显示作业打印“true”,但阶段条件始终返回false。 我尝试过使用返回布尔值的函数而不是字符串,但没有成功。 我尝试了不同的条件语法,但始终返回false。 我尝试将条件移到第一个作业,但在这种情况下,在评估条件之前触发环境的批准。
我的目标是Powershell决定是否应该使用该环境。 当使用环境时,会有一个批准步骤。
我写了另一篇文章(Azure管道、变量、powershell和部署的问题),现在我成功地从脚本中检索到了输出变量,但我仍然无法在条件中使用它。
你能帮我吗?
1个回答

4

您应该尝试这个:

  - stage: DeploymentStage
    dependsOn: DisplayStage
    variables:
      areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
    condition: eq(dependencies.Init.outputs['RunScript.compareFiles.areDifferent'], 'true')
    jobs:
    - deployment: DeploymentJob
      environment: 'ATest'
      strategy:
        runOnce:
          deploy:
            steps:
              - checkout: none
              - task: CmdLine@2
                inputs:
                  script: |
                    echo EnvName Finally: $(areFilesDifferent)

这里的问题在于stageDependency不是正确的语法,应该使用dependency expression,然后通过作业名称引用变量。请查看文档这里

@ClaudeVernier 如果我的回复对你有帮助的话,你能否考虑给它点赞? - Krzysztof Madej

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接