1

I'm writing an Azure Pipeline where I use the targetBranchName of a pull request to define the test environment I'm going to use. Since targetBranchName has no value before execution, I created a "prepare" stage where I capture the value of the variable to use in further stages.

- stage: prepare
  jobs:
  - job: variables
    steps:
    - checkout: none
    - powershell: Write-Output "##vso[task.setvariable variable=TARGET;isOutput=true;isReadOnly=true]$(system.pullRequest.targetBranchName)" 

Then, in a later stage, I want to use the output variable BRANCH_NAME to find whether the stage should run and if so to load the appropriate group variables. I also would like to capture the value of the output variable in a job variable so that I can refer to it with a more "convenient" name.

The stage condition seems to be working when tested with manually set values for TARGET in the prepare job, but I can't actually see the value of TARGET, so I added a "print" step and it shows no value for TARGET.

I'm not shure about where is my mistake, if in how I refer to the output variable to copy its value (stage vs. stageDependencies) or if the syntax for recovering the value of a variable in dependencies/stageDependencies ($(), $[] or ${{}}).

- stage: deploy_to_cint
  dependsOn: prepare
  condition: and(succeeded('prepare'), in(dependencies.prepare.outputs['variables.environment.TARGET'], 'env1', 'env2'))    
  variables:
  - name: TARGET
    value: $(dependencies.prepare.outputs['variables.environment.TARGET'])
  - group: 'EnvironmentCredentials-$(TARGET)'
  jobs:
  - job: debug
    steps:
    - bash: echo This is running for $(TARGET).
lpacheco
  • 115
  • 6

1 Answers1

0

Refer : https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash#set-an-output-variable-for-use-in-future-stages

Here is a working example :

stages:
- stage: A
  jobs:
  - job: A1
    steps:
     - bash: echo "##vso[task.setvariable variable=myStageVal;isOutput=true]this is a stage output variable"
       name: MyOutputVar
- stage: B
  dependsOn: A
  jobs:
  - job: B1
    variables:
      myStageAVar: $[stageDependencies.A.A1.outputs['MyOutputVar.myStageVal']]
    steps:
      - bash: echo $(myStageAVar)
lpacheco
  • 115
  • 6
geeky_girl
  • 81
  • 3