【问题标题】:Azure pipeline, issues with displaying variables and branchingAzure 管道,显示变量和分支的问题
【发布时间】:2021-06-18 10:10:42
【问题描述】:

在之前的帖子Azure pipeline: how to use an output variable in a condition 中,我在使用依赖项中的变量时遇到了问题。

现在,我有一个管道,我在其中调用返回 true 或 false 的脚本。 返回值设置下一阶段要运行的内容,但我现在有两个问题。

变量的条件有效,但变量的显示没有显示。 该条件使工作流转到两个分支,但现在我需要它只返回一个分支

这是我的 Yaml:

stages:
  - stage: Init
    jobs:
    - job: RunScript
      steps:
        - task: PowerShell@2
          name: compareFiles
          inputs:
            targetType: filePath
            filePath: '***\compareFileContent.ps1'

  - stage: Display1
    dependsOn: Init
    variables:
      areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
    jobs:
    - job: RunScript
      steps:
        - task: CmdLine@2
          inputs:
            script: |
              echo areFilesDifferent: $(areFilesDifferent)

  - stage: DeploymentStageWithApproval
    dependsOn: Display1
    variables:
      areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
    condition: eq( variables.areFilesDifferent, 'false' )
    jobs:
    - deployment: DeploymentJob
      environment: 'STWithApproval'
      strategy:
        runOnce:
          deploy:
            steps:
              - task: CmdLine@2
                inputs:
                  script: |
                    echo DeploymentStageWithApproval: $(areFilesDifferent)


  - stage: DeploymentStageWithoutApproval
    dependsOn: Display1
    variables:
      areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
    condition: eq( variables.areFilesDifferent, 'true' )
    jobs:
    - deployment: DeploymentJob
      environment: 'ST'
      strategy:
        runOnce:
          deploy:
            steps:
              - task: CmdLine@2
                inputs:
                  script: |
                    echo DeploymentStageWithoutApproval: $(areFilesDifferent)

  - stage: Display2
    condition: or( eq(dependencies.DeploymentStageWithApproval.result, 'Succeeded'), eq(dependencies.DeploymentStageWithoutApproval.result, 'Succeeded') )
    variables:
      areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
    jobs:
    - job: RunScript
      steps:
        - checkout: none
        - task: CmdLine@2
          inputs:
            script: |
              echo areFilesDifferent: $[ variables.areFilesDifferent ]
              echo DeploymentStageWithApproval: $[ dependencies.DeploymentStageWithApproval.result ]
              echo DeploymentStageWithoutApproval: $[ dependencies.DeploymentStageWithoutApproval.result ]

这是我的 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"

所有的脚本作业都只写文本,不写变量,我不明白为什么。

如果运行 DeploymentStageWithApproval,则会触发批准,但不会运行 Display2。

如果运行 DeploymentStageWithoutApproval,则运行 Display2

我的感觉是因为如果批准,但一旦我批准,任务就会运行,为什么它没有进入“阶段:Display2”?

感谢您的帮助, 克劳德

***************************更新1

谢谢,您的 cmets 确实帮助我理解了语法。不过,在我的最后阶段,变量没有打印出来,我不明白为什么。

这是我的 YAML 的一个较小版本,用于演示我的问题:

stages:
  - stage: Init
    jobs:
    - job: RunScript
      steps:
        - checkout: none
        - task: PowerShell@2
          name: compareFiles
          inputs:
            targetType: filePath
            filePath: '***\compareFileContent.ps1'

  - stage: Display1
    dependsOn: Init
    variables:
      areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
    jobs:
    - job: DisplayVariables
      steps:
        - checkout: none
        - task: CmdLine@2
          inputs:
            script: |
              echo areFilesDifferent: $(areFilesDifferent)

显示:areFilesDifferent:true

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

显示:DeploymentStageWithoutApproval:

我会假设它与部署任务相关联,但我不知道如何获得该值,我确实尝试将变量分配移动到作业下但没有成功。

我可能不需要那个地方的变量,但为了调试,理解这个问题会很有用。

感谢和问候, 克劳德

【问题讨论】:

    标签: azure-pipelines azure-pipelines-yaml


    【解决方案1】:

    你使用了错误的语法(这里真的很混乱)。

    例如 - 它不起作用

          areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
    

    因为您需要在这里使用 stageDependencies 而不是依赖项。

    因此对于舞台级别的条件,您应该使用dependencies,但对于舞台级别设置的变量stageDependencies

    还有什么:

    • 你不能这样做
        variables:
          areFilesDifferent: $[ dependencies.Init.outputs['RunScript.compareFiles.areDifferent'] ]
        condition: eq( variables.areFilesDifferent, 'false' )
    
    • 但这会起作用
        condition: eq( dependencies.Init.outputs['RunScript.compareFiles.areDifferent'], 'false' )
    

    请检查这个例子:

    stages:
    - stage: StageA
      jobs:
      - job: A1
        steps:
          - pwsh: echo "##vso[task.setvariable variable=RunStageB;isOutput=true]true"
            name: setvarStep
          - bash: echo $(System.JobName)
    
    - stage: StageB
      dependsOn: 
        - StageA
     
      # when referring to another stage, stage name is included in variable path
      condition: eq(dependencies.StageA.outputs['A1.setvarStep.RunStageB'], 'true')
      
      # Variables reference syntax differs slightly from inter-stage condition syntax
      variables:
        myOutputVar: $[stageDependencies.StageA.A1.outputs['setvarStep.RunStageB']]
      jobs:
      - deployment: B1
        pool:
          vmImage: 'ubuntu-16.04'
        environment: envB
        strategy:                  
          runOnce:
            deploy:
              steps:
              - bash: echo $(myOutputVar)
    
    

    但如果您在工作级别使用与上面显示的相同的条件,则应使用stageDependencies

    【讨论】:

    • 非常感谢!它确实有帮助,但我仍然在后期显示变量时遇到问题,如果您有时间并想查看它,我会在更新 1 中解释它:-) 我仍然遇到分支问题,但是将写一个新问题并尝试更清楚:)
    • @ClaudeVernier 很酷,很高兴它对您有所帮助。如果我的回复对您有帮助,您是否也可以考虑点赞?
    猜你喜欢
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 2021-07-27
    • 2019-08-04
    相关资源
    最近更新 更多