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

Cla*_*ier 4 powershell azure-pipelines azure-pipelines-yaml

我正在开发在 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)
Run Code Online (Sandbox Code Playgroud)

这是我的 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"
Run Code Online (Sandbox Code Playgroud)

显示作业打印“true”,但阶段条件始终返回 false。我尝试使用返回布尔值而不是字符串的函数,但没有成功。我在条件中尝试了不同的语法,总是返回 false。我尝试将条件移至第一项作业,但在这种情况下,在评估条件之前会触发环境的批准。

我的目标是 Powershell 决定是否应该使用该环境。使用环境时,有一个批准步骤。

我确实就此写了另一篇文章(Azure pipelines,有关变量的问题,powershell 和部署),现在我成功地从脚本中检索了输出变量,但我仍然无法在某种情况下使用它。

你能帮我吗?

Krz*_*tof 5

你应该尝试这个:

  - 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)
Run Code Online (Sandbox Code Playgroud)

这里的问题是 stageDependency 在阶段级别不是正确的语法。您应该使用依赖表达式,然后也通过作业名称引用变量。请在此处查看文档