Search code examples
azure-devopsyamlazure-pipelines

How to pass the variable value across the stages if we have multiple variables in stage azure Devops yml?


I have below pipeline in azure Devops yml where I am trying to find the zip file in shared location.I have 3 components and not sure which one are available so which one are available I need to get those and push to deployment tool and during deployment I just need to deploy only those component which are push and other should be ignored/skipped.My problem here I am able to find and push the right one to deploy tool but I am not able to deploy only the ones which are actually pushed.Please let me know if there is any way.Want to create only two stages. One for build and other one for deploy

pool:
  name: abc

trigger: none

parameters:
  - name: c_share
    type: string
    default:  a/v/c

stages:
- stage: build
  jobs:
  - job: findandpush
    steps:
      - task: powershell@2
        inputs:
          targetType: inline
          script: |
            net use ${{ parameters. c_share }} /delete
            net use ${{ parameters. c_share }} pass /user:username

      - powershell: |
          $a = "Nice"
          $s = "${{ parameters. c_share }}" + "\" + "$a"
          $af = "${{ System. DefaultWorkingDirectory }}" + "\" + "$a"
          New-Item -Itemtype directory -Path $af
          Dir $s
          Dir $a
          Copy-Item -Path $s\*.zip -Destination $af -Verbose
          if ( Test-Path -path $af\*.zip ) {
            Write-Output "##vso[task.setvariable variable=Niceexists]true"
          } else {
            Write-Output "##vso[task.setvariable variable=Niceexists]false"
          }
         name: OutNicexists

      - powershell: |
          $a = "Lice"
          $s = "${{ parameters. c_share }}" + "\" + "$a"
          $af = "${{ System. DefaultWorkingDirectory }}" + "\" + "$a"
          New-Item -Itemtype directory -Path $af
          Dir $s
          Dir $a
          Copy-Item -Path $s\*.zip -Destination $af -Verbose
          if ( Test-Path -path $af\*.zip ) {
            Write-Output "##vso[task.setvariable variable=Liceexists]true"
          } else {
            Write-Output "##vso[task.setvariable variable=Liceexists]false"
          }
         name: OutLicexists

      - powershell: |
          $a = "Rice"
          $s = "${{ parameters. c_share }}" + "\" + "$a"
          $af = "${{ System. DefaultWorkingDirectory }}" + "\" + "$a"
          New-Item -Itemtype directory -Path $af
          Dir $s
          Dir $a
          Copy-Item -Path $s\*.zip -Destination $af -Verbose
          if ( Test-Path -path $af\*.zip ) {
            Write-Output "##vso[task.setvariable variable=Riceexists]true"
          } else {
            Write-Output "##vso[task.setvariable variable=Riceexists]false"
          }
         name: OutRicexists

     - task: ACDCreateversion@2
       condition: and(succeeded(), eq(variables['Niceexists'], 'true'))
       inputs: 
         Came: 1234-abc-nice
         Chase: $(System. DefaultWorkingDirectory)\$Nice
         Cinclude: '*.zip'

     - task: ACDCreateversion@2
       condition: and(succeeded(), eq(variables['Liceexists'], 'true'))
       inputs: 
         Came: 1234-abc-nice
         Chase: $(System. DefaultWorkingDirectory)\$Lice
         Cinclude: '*.zip'

     - task: ACDCreateversion@2
       condition: and(succeeded(), eq(variables['Riceexists'], 'true'))
       inputs: 
         Came: 1234-abc-nice
         Chase: $(System. DefaultWorkingDirectory)\$Rice
         Cinclude: '*.zip'



- stage: DeploytoDeploytool
  dependsOn: build
  jobs:
    - deployment: deploy
      environment: 'ABC-DEV'
      strategy:
        runOnce:
          deploy:
            steps:
            - task: AutoDeploytoTool@3
              inputs: 
                applicationname: Nice
                applicationname: abc-dep
                Environment: dev
                versionnumber: '1.2.3'
            - task: AutoDeploytoTool@3
              inputs: 
                applicationname: Lice
                applicationname: abc-dep
                Environment: dev
                versionnumber: '1.2.3'
            - task: AutoDeploytoTool@3
              inputs: 
                applicationname: Rice
                applicationname: abc-dep
                Environment: dev
                versionnumber: '1.2.3'
          

Solution

  • In order to use the variable in the future stages you need to set it as an output variable please see the following documentation.

    Note the Write-Host and not Write-Output is used.

    Write-Host "##vso[task.setvariable variable=Niceexists;isOutput=true]true"
    

    Check the Use outputs in a different stage documentation in order to know how to reference the output variable from the next stage. Create a job variable in the deploy job for the output variable from the previous stage.

    - stage: DeploytoDeploytool
      dependsOn: build
      jobs:
        - deployment: deploy
          environment: 'ABC-DEV'
          variables:
            NiceExists: $[ stageDependencies.build.findandpush.outputs['OutNicexists.Niceexists'] ]
    

    After you set the job variable which references the output variable from the previous stage you can use it as a regular variable in the job.

    - script: echo $(NiceExists)
      condition: and(succeeded(), eq(variables['NiceExists'], 'true'))
    

    If you want to use the variable in the same job and it is set as an output variable then you need to reference it differently please see the documentation: if you do add the isoutput property, you'll need to reference the variable with the task name. So if you want to reference the variable in the build stage you need to reference it in the following way.

    - script: echo $(OutNicexists.Niceexists)
      condition: and(succeeded(), eq(variables['OutNicexists.Niceexists'], 'true'))
    

    Working example for one variable:

    trigger: none
    
    parameters:
      - name: network_share
        type: string
        default:  a/v/c
    
    stages:
    - stage: build
      jobs:
      - job: findandpush
        steps:
          - powershell: |
              if ( "a" -eq "a" ) {
                Write-Host "##vso[task.setvariable variable=Niceexists;isOutput=true]true"
              } else {
                Write-Host "##vso[task.setvariable variable=Niceexists;isOutput=true]false"
              }
            name: OutNicexists
          - script: echo $(OutNicexists.Niceexists)
            condition: and(succeeded(), eq(variables['OutNicexists.Niceexists'], 'true'))
        
    - stage: DeploytoDeploytool
      dependsOn: build
      jobs:
        - deployment: deploy
          environment: 'ABC-DEV'
          variables:
            NiceExists: $[ stageDependencies.build.findandpush.outputs['OutNicexists.Niceexists'] ]
          strategy:
            runOnce:
              deploy:
                steps:
                - script: echo $(NiceExists)
                  condition: and(succeeded(), eq(variables['NiceExists'], 'true'))