Search code examples
azureazure-devopsazure-pipelinescicd

Access pipeline A´s variables from pipeline B... Azure Devops


After some internet search, I wasnt able to find a proper way or suggestion on how to access variables from different pipelines. Lets say, from Pipeline A access variables of Pipeline B. What I did find, is the idea to use Key Vault, which I am not able to use right now. I was wondering if there is a workaround, lets say, with powershell.

All of this is happening in an Azure Devops environment, where I am trying to access/read variables from different pipelines.

Any ideas?

Kind regards

Leo.


Solution

  • You can make use of variable groups to call variables in multiple pipelines within a project.

    You just need to reference that variable in the YAML script or release pipeline with the variable group and use it in any pipeline, Refer below :-

    I went to my project > Pipelines > Library > Variable Group > And added a variable > You can add multiple variables here storing your secrets or values.

    enter image description here

    enter image description here

    enter image description here

    Using the variable group in a yaml pipeline :-

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

    enter image description here

    Now, when you run the pipeline, It will ask you to permit the use of variable group for the pipeline.

    This will enable access to all the variables in the SharedVariables group.

    enter image description here

    enter image description here

    Output :-

    enter image description here

    We got our databaseservername value masked.

    You can also enable this variable group for all the pipeline in the project by default.

    enter image description here

    You can use the same variable group in your Classic pipeline or release pipeline in release or specific stages like below :-

    enter image description here

    Reference :-

    Variable groups for Azure Pipelines - Azure Pipelines | Microsoft Learn

    For PowerShell - Azure DevOps: how to manage CI/CD variable groups using PowerShell – Radu Narita (ranari.com)