8

I have two pipelines(Pipeline A and Pipeline B) configured for build and release and Pipeline A triggers Pipeline B for deployment.

I defined variables on pipeline A and need them on pipeline B to use for deployment . is it possible to pass them between two these pipelines ?.

any leads are much appreciated.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
ramesh reddy
  • 429
  • 2
  • 5
  • 12

3 Answers3

5

You can use for that variable group. Here you have doc about variable group.

Use a variable group to store values that you want to control and make available across multiple pipelines. You can also use variable groups to store secrets and other values that might need to be passed into a YAML pipeline. Variable groups are defined and managed in the Library page under Pipelines.

What you need is declare the same variable in both pipelines:

variables:
- group: my-variable-group

and for instance if you want to update variable in one pipeline and have this updated value available in second you can use Azure CLI

az pipelines variable-group variable update --group-id
                                            --name
                                            [--detect {false, true}]
                                            [--new-name]
                                            [--org]
                                            [--project]
                                            [--prompt-value {false, true}]
                                            [--secret {false, true}]
                                            [--value]

You should call this command from Azure CLI task

- task: AzureCLI@2
  displayName: Azure CLI
  inputs:
    azureSubscription: <Name of the Azure Resource Manager service connection>
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      az --version
      az pipelines variable-group --group-id value-of-group-id --name some-name --org your-org --project your-project --value some-value

And if you wanto to Trigger one pipeline after another you can use pipeline resource:

resources:
  pipelines:
  - pipeline: securitylib   # Name of the pipeline resource
    source: security-lib-ci # Name of the pipeline referenced by the pipeline resource
    trigger: 
      branches:
      - releases/*
      - master
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • This is not a good solution as it uses a static source for this. We need something that is passed from one build to the next, regardless of how many builds might be running at the same time. – James Esh Jul 12 '21 at 22:07
1

Just like Krzysztof Madej pointed out, Variable groups will help to share static values across builds and releases pipeline.

What you need is a way to pass variables from one pipeline to another. I'm afraid to say there is no official way to do this for now.

As a workaround you could update the value of your variables inside your variable group. There are multiple ways to handle this, Rest API, powershell, 3rd-party extension. Detail ways please refer answers in this question: How to Increase/Update Variable Group value using Azure Devops Build Definition?

If you want to get the value of variable in the pipeline. Since you have used logging command to update that variable.

What you need to do only is using Rest API to get that particular build log to fetch related info.

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • 1
    no. it should not be static values because these values comes from multiple pipelines and each pipeline contains its own app variables – ramesh reddy Nov 02 '20 at 12:40
  • @rameshreddy For this scenario, you may have to add additional steps in your build pipeline. Either use logging command or Rest API to update the value in Variable Group. More details you could refer andy's answer in the link mentioned above. https://stackoverflow.com/questions/52399076/how-to-increase-update-variable-group-value-using-azure-devops-build-definition – PatrickLu-MSFT Nov 03 '20 at 07:36
0

How to pass variables from one pipeline to another pipeline in azure devops

You could set some default variables in the release pipeline, then add a inline powershell task to invoke the REST API (Definitions - Update) to update the value of the release definition variable in the build pipeline with values comes from build pipeline.

PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=5.0

Now, we could use the values from multiple pipelines in the release pipeline.

Please check my previous thread for some more details.

Note:Do not forget to use REST API to restore those changed variables to the default value, so that we could use it next time.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135