Forum Discussion

envious1675's avatar
envious1675
Copper Contributor
May 26, 2023

share variable across stages

I want variables created in a stage to be passed on to the next stage in azure devops so that I can use it in the next stage and I am not using YAML for creating pipeline instead I have used classic editor.

1 Reply

  • Try below as workarounds:

     

    1. Predefine Variables at Pipeline Level
    •    You can define variables at the pipeline level (in the Variables tab).
    •    These are accessible in all stages, but you can't change their value mid-pipeline and expect the new value to persist.
    2. Write to a File and Read Later
    •    Use a script to write the variable value to a file (e.g., output.txt) in a shared artifact folder.
    •    Publish the file as an artifact in Stage 1.
    •    In Stage 2, download the artifact and read the value from the file.

    # Stage 1 - Write to file
    echo "myValue=123" > $(Build.ArtifactStagingDirectory)\output.txt

     

    # Stage 2 - Read from file
    $lines = Get-Content "$(Pipeline.Workspace)\output.txt"
    foreach ($line in $lines) {
        $parts = $line -split "="
        Write-Host "##vso[task.setvariable variable=$($parts[0])]$($parts[1])"
    }


    3. Use Environment Variables 
    •    You can set environment variables in a job, but they won’t persist across stages.

Resources