0

we are trying to create a DevOps pipeline where we generate a string in the first stage and we would like to store into a variable which can be used in subsequent stages, or in subsequent tasks within the same stage, is there a way to do this? For clarity the string is generated by querying an external api which returns a string value.

Hope that makes sense

Thanks in advance

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
Norrin Rad
  • 881
  • 2
  • 18
  • 42

1 Answers1

1

Yes, you shoudl use logging command and mark is as output. Here you have an example

## azure-pipelines.yml
stages:

- stage: one
  jobs:
  - job: A
    steps:
    - task: Bash@3
      inputs:
          filePath: 'script-a.sh'
      name: setvar
    - bash: |
       echo "##vso[task.setvariable variable=skipsubsequent;isOutput=true]true"
      name: skipstep

- stage: two
  jobs:
  - job: B
    variables:
      - name: StageSauce
        value: $[ stageDependencies.one.A.outputs['setvar.sauce'] ]
      - name: skipMe
        value: $[ stageDependencies.one.A.outputs['skipstep.skipsubsequent'] ]
    steps:
    - task: Bash@3
      inputs:
        filePath: 'script-b.sh'
      name: fileversion
      env:
        StageSauce: $(StageSauce) # predefined in variables section
        skipMe: $(skipMe) # predefined in variables section
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          echo 'Hello inline version'
          echo $(skipMe) 
          echo $(StageSauce) 
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • Thanks, can I ask a newbie question, I use the classic portal so trying to get my head around this. In stage 1 it runs a bash script that pulls out a string value and uses it when running later stages, is there a way to set this to a group variable for other stages? Could you give me a brief overview of the script please – Norrin Rad Jun 16 '21 at 15:00
  • You need to use azure clit to set variable in variable group. Here you have [docs](https://learn.microsoft.com/en-us/cli/azure/pipelines/variable-group?view=azure-cli-latest). And [here how to use it from the pipeline](https://learn.microsoft.com/pl-pl/azure/devops/cli/azure-devops-cli-in-yaml?view=azure-devops). It refers to YAML but it could be easily adapted to Classic. – Krzysztof Madej Jun 16 '21 at 15:18