0

Im trying to pass variables between stages with jobs that is using templates in azure pipeline , tried a lot of methods but still did not managed to achieve it.

i have job that is checking something and create vso task.variable and this variables i want to pass to next stage

azure-pipelines.yaml

stages:

  - stage: CheckProjectType
    displayName: Check Project Type
    jobs:            
      - template: jobs/checkprojectype.yaml@devops

  - stage: PrintProjectType
    dependsOn: CheckProjectType
    displayName: Print Project Type
    jobs: 
      - template: jobs/buildproject.yaml@devops
        parameters:
          outputval: $[ stagedependencies.CheckProjectType.outputs['SetValueStep.projecttype'] ] 

checkprojectype.yaml

jobs:
- job: CheckProjectType
  displayName: Check Project Type
  steps:
  - bash: |
      if [ -f *.xml ]; then
        echo "JAVA"
      elif [ -f *.json ]; then
        echo "json file found , Nodejs Project Will Be Built"
        echo "##vso[task.setvariable variable=projecttype;]nodejs"
      elif [ -f *.py ]; then
        echo "Python"
      else
        echo "Not Found"
      fi
    name: itay
    displayName: Checking Current Location

buildproject.yaml

jobs:
- job: BuildProject
  variables:
  - name: myvar123
    value: ${{ parameters.outputval }}
  steps:
  - bash: echo $(myvar123)

and i expect to get nodejs , but I Dont get anything , the echo is empty

ITBYD
  • 83
  • 1
  • 9
  • Does this answer your question? [Share variables across stages in Azure DevOps Pipelines](https://stackoverflow.com/questions/57485621/share-variables-across-stages-in-azure-devops-pipelines) – Tom W Jan 19 '23 at 09:54
  • @TomW Unfortunately no,already tried it. – ITBYD Jan 19 '23 at 10:07

1 Answers1

0

If you want to share a task variable in future stages it needs an additional isOutput=true property as mentioned in the docs here

checkprojectype.yml

jobs:
- job: CheckProjectType
  displayName: Check Project Type
  steps:
  - bash: |
      if [ -f *.xml ]; then
        echo "JAVA"
      elif [ -f *.json ]; then
        echo "json file found , Nodejs Project Will Be Built"
        echo "##vso[task.setvariable variable=projecttype;isOutput=true]nodejs"
      elif [ -f *.py ]; then
        echo "Python"
      else
        echo "Not Found"
      fi
    name: itay
    displayName: Checking Current Location

Also double-check @tom-w remark and, as your code example does not seem to fit the reference there.

In the example:

outputval: $[ stagedependencies.CheckProjectType.outputs['SetValueStep.projecttype'] ]

Should be:

outputval: $[ stagedependencies.CheckProjectType.CheckProjectType.outputs['itay.projecttype'] ] 
Roderick Bant
  • 1,534
  • 1
  • 9
  • 17