0

I am working on a pipeline running on a Windows Self Hosted agent. I use a script that initialize a variable in one stage and use the variable in a condition on the next two stages.

If variable is true, third stage is run, this stage as an environment with an approval.

If variable is false, fourth stage is run.

In any case, the fifth stage must always be run.

My YAML works until the fifth stage, it will run only if the fourth stage is run !!

enter image description here

Here is my 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)

  - stage: WithApproval
    dependsOn: Display1
    condition: eq( dependencies.Init.outputs['RunScript.compareFiles.areDifferent'], 'false' )
    variables:
      areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
    jobs:
    - deployment: DeploymentJob
      environment: 'STWithApproval'
      strategy:
        runOnce:
          deploy:
            steps:
              - checkout: none
              - task: CmdLine@2
                inputs:
                  script: |
                    echo DeploymentStageWithApproval: $(areFilesDifferent)
    - job: SetupNext
      dependsOn: DeploymentJob
      steps:
        - checkout: none
        - task: PowerShell@2
          name: setupNext
          inputs:
            targetType: 'inline'
            script: |
              echo "##vso[task.setvariable variable=continue;isOutput=true]yes"

  - 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)
    - job: SetupNext
      dependsOn: DeploymentJob
      steps:
        - checkout: none
        - task: PowerShell@2
          name: setupNext
          inputs:
            targetType: 'inline'
            script: |
              echo "##vso[task.setvariable variable=continue;isOutput=true]yes"

  - stage: Display2
    condition: xor( eq( dependencies.WithApproval.outputs['SetupNext.setupNext.continue'], 'yes' ), eq( dependencies.WithoutApproval.outputs['SetupNext.setupNext.continue'], 'yes' ) )
    variables:
      areFilesDifferent: $[ stageDependencies.Init.RunScript.outputs['compareFiles.areDifferent'] ]
      WithApproval: $[ stageDependencies.WithApproval.SetupNext.outputs['setupNext.continue'] ]
      WithoutApproval: $[ stageDependencies.WithoutApproval.SetupNext.outputs['setupNext.continue'] ]
    jobs:
    - job: DisplayVariables
      steps:
        - checkout: none
        - task: CmdLine@2
          inputs:
            script: |
              echo areFilesDifferent: $(areFilesDifferent)
              echo WithApproval: $(WithApproval)
              echo WithoutApproval: $(WithoutApproval)

If WithApproval runs, Display2 is skipped !! ??

If WithoutApproval runs, Display2 runs but won't display the content of areFilesDifferent

I tried in Display2 to test if WithApproval or WithoutApproval ran successfully but as it wasn't working I tried setting up those 'continue' variables but it is not my favorite way of doing this.

Is it possible to branch back to Display2 after waiting for the approval ?

Thanks for any advice, Claude

ClaudeVernier
  • 427
  • 4
  • 20

1 Answers1

0

I figured it out. To build the dependencies graph, it uses the DependsOn statements and in order to access output variables of a previous stage, the stage name must appear in the the DependsOn statements as shown now in this YAML and picture:

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

  - stage: ContinueWithApproval
    dependsOn: Build
    condition: eq( dependencies.Build.outputs['CompareFiles.compareFiles.filesAreEqual'], 'true' )
    jobs:
    - deployment: Continue
      environment: 'EnvironmentWithApproval'
      strategy:
        runOnce:
          deploy:
            steps:
            - task: CmdLine@2
              inputs:
                script: '***'

  - stage: ContinueWithoutApproval
    dependsOn: Build
    condition: eq( dependencies.Build.outputs['CompareFiles.compareFiles.filesAreEqual'], 'false' )
    jobs:
    - deployment: Continue
      environment: 'EnvironmentWithoutApproval'
      strategy:
        runOnce:
          deploy:
            steps:
            - task: CmdLine@2
              inputs:
                script: '***'


  - stage: DeployStaging
    dependsOn: 
    - ContinueWithApproval
    - ContinueWithoutApproval
    condition: or(eq( dependencies.ContinueWithApproval.result, 'Succeeded' ), eq( dependencies.ContinueWithoutApproval.result, 'Succeeded' ))
    jobs:
    - deployment: Deploy_To_Staging
      environment: 'Name ST'
      strategy:
           runOnce:
             deploy:
               steps:
                - task: PowerShell@2
                  inputs:
                    targetType: filePath
                    filePath: '***.ps1'

  - stage: DeployPreProd
    dependsOn: 
    - DeployStaging
    jobs:
    - deployment: Deploy_To_Preprod
      environment: 'Name PP'
      strategy:
           runOnce:
             deploy:
               steps:
                - task: PowerShell@2
                  inputs:
                    targetType: filePath
                    filePath: '***.ps1'

enter image description here

Now, I have another issue where the last stage is skipped but that's another story... :-)

Regards, Claude

ClaudeVernier
  • 427
  • 4
  • 20