Home > Mobile >  Pass value through jobs in Azure DevOps pipeline
Pass value through jobs in Azure DevOps pipeline

Time:06-22

I try to pass value through my jobs in Azure DevOps pipeline and I use for it this code:

trigger: none

pool:
  vmImage: 'windows-2019'
stages:
- stage: Processing
  jobs:
  - job: A
    steps:
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            $someValue = 1234
            Write-Host ("##vso[task.setvariable variable=someValue; isOutput=true;]$someValue")

  - job: B
    dependsOn: ['A']
    variables: 
      someValue: $[ dependencies.A.outputs['setVariable.someValue'] ]
    steps: 
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          Write-host "Hello there"
          echo $(someValue)

As a result I get this: enter image description here

What do I wrong? What code do I need for passing value?

CodePudding user response:

Test your YAML sample and reproduce the same issue.

From your YAML code, it has the following issues.

1.When you set the variable in Powershell task, the command has an extra space character in the command.

Refer to the following sample to set variable:

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

2.You need to define the name of the PowerShell task which is used to set the variable. Then you can use the name in next job to get the variable. name: taskname

Here is an full example:

pool:
  vmImage: 'windows-2019'
stages:
- stage: Processing
  jobs:
  - job: A
    steps:
      - task: PowerShell@2
        name: taskname
        inputs:
          targetType: 'inline'
          script: |
            $someValue = 1234
            Write-Host "##vso[task.setvariable variable=someValue;isOutput=true;]$someValue"

  - job: B
    dependsOn: A
    variables: 
      someValue: $[ dependencies.A.outputs['taskname.someValue'] ]  
    steps: 
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          Write-host "Hello there"
          echo $(someValue)

For more detailed info, you can refer to this doc: Share variables across pipelines

  • Related