Home > OS >  How do I use a environment variable with output from one stage to the next?
How do I use a environment variable with output from one stage to the next?

Time:11-19

How do I get my environment variable from my first stage to my second stage?

Issue -> I get an empty "" value from my echo result.

azure-pipeline.yml

trigger:
  branches:
    include:
    - main
  paths:
    include:
    - infra/*
    - .pipelines/*

variables:
  vmImageName: 'ubuntu-latest'
  root: $(System.DefaultWorkingDirectory)

stages:
- stage: TakePITR
  dependsOn: []
  variables:
    env: poc
  jobs:
  - job: GetPitrTime
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $iso8601_time = Get-Date -Format "o"
          echo "##vso[task.setvariable variable=pitr_time;isOutput=True]$iso8601_time"
        name: PitrVar
        displayName: "Get point-in-time before second stage"

- stage: TestOutputEnvVar
  dependsOn: TakePITR
  variables:
    env: poc
    PITR: $[ stageDependencies.TakePITR.GetPitrTime.outputs['PitrVar.pitr_time']]
  jobs:
  - job:
    steps:
    - script: |
        echo $pitr_time
        echo $PITR
      displayName: 'echo value'

I don't understand what I'm doing wrong here.

This is the result of my pipeline: enter image description here

CodePudding user response:

You have indendation issue:

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $iso8601_time = Get-Date -Format "o"
          echo "##vso[task.setvariable variable=pitr_time;isOutput=true]$iso8601_time"
      name: PitrVar
      displayName: "Get point-in-time before second stage"

Name and displayName should be on the same level as input and then:

enter image description here

Full code is here

trigger:
  branches:
    include:
    - main
  paths:
    include:
    - infra/*
    - .pipelines/*

variables:
  vmImageName: 'ubuntu-latest'
  root: $(System.DefaultWorkingDirectory)

stages:
- stage: TakePITR
  dependsOn: []
  variables:
    env: poc
  jobs:
  - job: GetPitrTime
    steps:
    - task: PowerShell@2
      name: PitrVar
      displayName: "Get point-in-time before second stage"
      inputs:
        targetType: 'inline'
        script: |
          $iso8601_time = Get-Date -Format "o"
          echo "##vso[task.setvariable variable=pitr_time;isOutput=true]$iso8601_time"

- stage: TestOutputEnvVar
  dependsOn: TakePITR
  variables:
    env: poc
    PITR: $[ stageDependencies.TakePITR.GetPitrTime.outputs['PitrVar.pitr_time']]
  jobs:
  - job:
    steps:
    - script: |
        echo $pitr_time
        echo $PITR
      displayName: 'echo value'
  • Related