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

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.

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

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.

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.

Related

Passing variables between release pipelines in azure (trigger azure pipeline extension)

I have a requirement to pass data between 2 release pipelines (to trigger 2nd pipeline on completion of 1st pipeline).
Can we pass variables dynamically between azure RELEASE pipelines using trigger an Azure DevOps pipeline extension?
I tried this blog but unable to find/understand if we can use "output variables" to pass data between azure release pipelines.
https://msftplayground.com/2019/02/trigger-a-pipeline-from-an-azure-devops-pipeline/
Thank you in advance!
Output variables are created by the pipeline and referenced by the other tasks in the pipeline, it means they are dynamic and refers to the result of a particular task.
These cannot be defined statically.
After running the task in the pipeline, output variables value can be known.
There are two different ways to create output variables :
By building support for the variable in the task itself
Setting the value ad-hoc in a script
Below example is defining a task with the name SomeTask that natively creates an output variable called out.
In a task within that same job, you can reference that variable using $(SomeTask.out).
steps:
- task: MyTask#1
name: SomeTask
- script: echo $(SomeTask.out)
For the detailed information regarding how to create output variables and pass between the pipelines, please refer azure devops output variables.

custom pipeline resource variables

I have an Azure Pipeline, that consumes the artifacts produced by another pipeline. Something like this
resources:
pipelines:
- pipeline: BuildPipeline
source: project-ci
With this reference, I can reference some pipeline resource variables like resources.pipeline.BuildPipeline.runName.
The question is, other than the predefined variables (list here), is it possible to publish a custom variable in the first pipeline so it's available later as a resource variable?
The question is, other than the predefined variables (list here), is it possible to publish a custom variable in the first pipeline so it's available later as a resource variable?
I am afraid there is no such directly way to publish a custom variable in the first pipeline as a resource variable at this moment.
As workaround, we could use the REST API in the rescource pipeline to update the variable in the Variables tab for the current pipeline.
Steps:
Define a variable in the later pipeline definition Variable.
Add a task to invoke REST API (Definitions - Update) in rescource pipeline to update the value of the above variable in the later release pipeline.
Use the updated value of the later pipeline variable in the later pipeline.
The details info about using REST API to update the value of the definition variable, you can follow the below ticket:
How to modify Azure DevOps release definition variable from a release task?
or you could use the Azure CLI to update the variable:
az pipelines variable update --name
[--allow-override {false, true}]
[--detect {false, true}]
[--new-name]
[--org]
[--pipeline-id]
[--pipeline-name]
[--project]
[--prompt-value {false, true}]
[--secret {false, true}]
[--subscription]
[--value]

"az pipelines run variables" not working the way i expected

i have a branch in git with a .yaml build file which has a script to simply print out the value of the variable declared in the .yaml
if i run the command: az pipelines run --id 999 --variables "myvar=myvar" I expected the variable to change as I am injecting it. it doesnt.
am i misunderstanding what this switch does? if so, what is it supposed to do and/or how can i achieve my goal?
i tried
i tried
i dont understand these SO "quality standards" <= (this line allowed me to save the post)
For az pipelines variable ,there are four commands
az pipelines variable create Add a variable to a pipeline.
az pipelines variable delete Delete a variable from pipeline.
az pipelines variable list List the variables in a pipeline.
az pipelines variable updateUpdate a variable in a pipeline.
You should use az pipelines variable update command to update a variable in a pipeline.
Command sample:
az pipelines variable update --name

How to consume variables from CI pipeline into CD pipeline directly

How can I consume variables set in CI pipeline directly in CD pipeline?
Example:
If there are multiply variables declared in one group. How to access particular variable in CI/CD pipeline?
Also how to use variable groups for multiple keyvault's?
Thank you
Setting up global variables
YAML
https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml
CLASSIC
https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=classic
Using Variables
YAML
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch
CLASSIC
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cbatch
Examples:
Batch script: %VARIABLE_NAME%
PowerShell script: $env:VARIABLE_NAME
Bash script: $VARIABLE_NAME
Key Vault
https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=classic#link-secrets-from-an-azure-key-vault
Based on Richard's answer, I managed to set the value of a variable in a variable group to a variable coming from a CI pipeline, and then to read that variable in a CD pipeline.
For that, it is necessary:
To have previously created the variable group, and the variable (its name identified by $(variableName). Let's assume its value would be stored in $(variableValue)).
To find the variable group ID (stored in $(variableGroupId)), which can be done by navigating on Azure DevOps to that variable group. The group ID will then be in the URL.
A Personal Access Token (PAT) with Read & Write access to group variables (called $(personalAccessToken) )
CI pipeline
- powershell: |
az pipelines variable-group variable update --group-id $(variableGroupId) --name $(variableName) --value $(variableValue)
displayName: 'Store the variable in a group variable'
env:
AZURE_DEVOPS_EXT_PAT: $(personalAccessToken)
Then all that's necessary, is to declare the variable group in the CD pipeline. If this variable group is called MyVariableGroup, it can be done in the following way:
CD pipeline
variables:
- group: MyVariableGroup
The variable that was previously set in the CI pipeline, will then be available in the CD pipeline.

How to conditionally run a build agent job with a pipeline variable?

In Azure DevOps pipelines there's an option to conditionally run a task based on a pipeline variable. This is handled under the Run this task > Custom conditions field and it uses the syntax:
eq(variables['VarName'], 'Desired Value')
An agent job has a similar field for conditional execution under Run this job > Custom condition using variable expressions.
However, when I use the same syntax as a conditional task the result always evaluates to 'false'.
So how can I conditionally run an agent job?
Screenshots:
Something like this worked for me:
- job: Job1
steps:
- powershell: |
if (some condition)
{
Write-Host ("##vso[task.setvariable variable=RunJob2;isOutput=true]True")
}
name: ScriptStep
- job: Job2
dependsOn: Create_Build_Matrix
condition: and(succeeded(), eq(dependencies.Job1.outputs['ScriptStep.RunJob2'], 'True'))
I discovered the answer. Unfortunately, it is not possible to conditionally run an agent job with a variable that is modified during build execution.
From the Azure DevOps Pipeline documentation under Pipeline Variables:
To define or modify a variable from a script, use the task.setvariable
logging command. Note that the updated variable value is scoped to
the job being executed, and does not flow across jobs or stages.
Try this one: https://stefanstranger.github.io/2019/06/26/PassingVariablesfromStagetoStage/
This one gives you to pass variables from one stage/job to another stage/job in the same release pipeline. I tried and it's working fine.
Also to run this you need to give some permissions for release pipeline. To allow for updating the Release Definition during the Release you need to configure the Release Permission Manage releases for the Project Collection Build Service.