Azure 管道,显示变量和分支的问题

Azure pipeline, issues with displaying variables and branching

在之前的 post 中,我在使用依赖项中的变量时遇到问题。

现在,我有一个管道,我在其中调用 return 为真或假的脚本。 return 值将下一阶段设置为 运行,但我现在有两个问题。

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

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

我的感觉是因为如果批准但是我批准了一次,任务运行s,为什么它不去“stage: Display2”?

感谢您的帮助, 克劳德

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

谢谢你,你的评论对我理解语法有很大帮助。不过,在我的最后阶段,变量没有打印出来,我不明白为什么。

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

Displays: 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)

Displays: DeploymentStageWithoutApproval:

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

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

感谢和问候, 克劳德

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

例如 - 它不起作用

      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