1
votes

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

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

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

The display job prints "true" but the stage condition always returns false. I tried with a function returning a boolean instead of a string, didn't work. I tried different syntax in the condition, always returns false. I tried moving the condition down to the first job but in that case the approval of the environment is triggered before the condition is evaluated.

My goal is that the Powershell decides if that environement should be used. When environment is used, there is an approval step.

I did wrote another post on this (Azure pipelines, question about variables, powershell and deployments), now I succeeded to retrieve the Output variable from the script but I still can't use it in a condition.

Would you be able to help me ?

1

1 Answers

0
votes

You should try this:

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

Issue here is that stageDependency is not correct syntax at stage level. You should use dependency expression and then refer to variable via Job name too. Please check documentation here