Home > other >  How to pass variables between Azure Release Pipeline Stages
How to pass variables between Azure Release Pipeline Stages

Time:01-30

Is there a way within a Release pipeline in Azure to pass variables created in one stage to the next stage?

I see lots of documentation about using echo "##vso[task..... - This however does not seem to work within the release pipeline.

I am mainly using bash scripts and I can reuse it within the same stage in different tasks, but not subsequent stages.

This seems like an essential feature to me, passing variables through stages...

Is there a way to do this?

CodePudding user response:

If you want to pass variables from one stage to another stage in yml pipelines for release, you are supposed to use echo "##vso[task....." follow the enter image description here

If you want to pass variables from one stage to another stage in classic release pipelines,

1.Set Release Permission Manage releases for the Project Collection Build Service as Allow. 2.Toggle on 'Allow scripts to access the OAuth token' for the first stage 3.Set a variable like 'StageVar' in release scope. 4.ADD the first powershell task(inline) in the first stage for creating a variable 'myVar' in the first stage. 5.update the Release Definition and Release Variable (StageVar)

6.Add a powershell task in the second stage to retrieve the value of myVar via the Release Variable StageVar.

You could refer the enter image description here

enter image description here

results in the second stage: enter image description here

CodePudding user response:

You can make use of the Azure DevOps Variable group to store your variables and call them in your release pipelines across multiple stages and multiple pipelines within a project. You can make use of Azure CLI to make use of the Variable group.

I have stored 2 variables of database name and password in the SharedVariables group. I can call this variable group in 2 ways: 1) Via the YAML pipeline and 2) Via Classic/Release pipelines.

enter image description here

Yaml:-

trigger:
- main
pool:
vmImage: ubuntu-latest
variables:
- group: SharedVariables
steps:
- script: |
echo $(databaseserverpassword)

When I ran the pipeline the database server password was encrypted like the below:-

enter image description here

In Release pipeline:-

enter image description here

You can add this variable within multiple pipelines and multiple stages in your release like below:-

enter image description here

But the above method will help you store static values in the variable group and not the output variable of build to release Unless you specifically assign those variables manually in the variable group. You can make use of this extension > Variable Tools for Azure DevOps Services - Visual Studio Marketplace

With this extension, you can store your variables from the build in a JSON file and load that JSON file in the next release stage by calling the task from the extension.

enter image description here

Save the build variable in a file:-

enter image description here

Load the build variable in a release:-

enter image description here

Another method is to store the variables in a file as a build artifact and then call the build artifact in the release pipeline with the below yaml code:-

trigger:
- dev
pool:
  vmImage: windows-latest
parameters:
  - name: powerenvironment
    displayName: Where to deploy?
    type: string
steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $variable = '${{parameters.powerenvironment}}'
      $variable | Out-File $(Build.ArtifactStagingDirectory)\filewithvariable.txt
      Get-Content $(Build.ArtifactStagingDirectory)\filewithvariable.txt
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

And download the artifact and run the tasks in your release pipeline.

Reference:-
Pass parameters from build to release pipelines on Azure DevOps - GeralexGR

Another simple method is to run the PowerShell script to store the build output as JSON in the published artifact and read the content in the release pipeline like below:-

ConvertTo-Json | Out-File "file.json"
Get-Content "file.json" | themnvertFrom-Json

You can also reference the dependencies from various stages and call them in another stage within a pipeline with below yaml code :-

stages:
- stage: A
  jobs:
  - job: A1
    steps:
     - bash: echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
     # or on Windows:
     # - script: echo ##vso[task.setvariable variable=shouldrun;isOutput=true]true
       name: printvar

- stage: B
  condition: and(succeeded(), eq(dependencies.A.outputs['A1.printvar.should run], 'true'))
  dependsOn: A
  jobs:
  - job: B1
    steps:
    - script: echo hello from Stage B

Reference:-
bash - How to pass a variable from build to release in azure build to release pipeline - Stack Overflow By PatrickLu-MSFT

azure devops - How to get the variable value in TFS/AzureDevOps from Build to Release Pipeline? - Stack Overflow By jessehouwing

VSTS : Can I access the Build variables from Release definition? By Calidus

Expressions - Azure Pipelines | Microsoft Learn

  • Related