In Azure DevOps, pipelines automate software build, testing, and deployment. Pipelines can be chained together to create complex workflows.
Sometimes, you need to pass variables between pipelines. For example, you might want to pass the build number from one pipeline to another or the test output from one pipeline to another.
There are a few ways to pass variables between pipelines in Azure DevOps.
Using Environment Variables in Azure DevOps
One way to pass variables between pipelines is to use environment variables. Environment variables are global variables that all pipelines can access.
To pass a variable between pipelines using environment variables, you must define the variable in the first pipeline and then export it.
To define a variable, you use the variable
keyword. For example, to define a variable called, you would use the following code:
variable buildNumber
To export a variable, you use the export
keyword. For example, to export the buildNumber
Variable, you would use the following code:
export buildNumber
Once you have defined and exported a variable, you can access it in any subsequent pipeline. For example, to access the buildNumber
variable in a second pipeline, you would use the following code:
variable buildNumber
Using Pipeline Parameters in Azure DevOps
Another way to pass variables between pipelines is to use pipeline parameters. Pipeline parameters are variables that are passed to a pipeline when it is run.
To pass a variable between pipelines using pipeline parameters, you must define the parameter in the first pipeline and then pass the parameter’s value to the second pipeline.
To define a parameter, you use the parameter
keyword. For example, to define a parameter called buildNumber
, you would use the following code:
parameter buildNumber
To pass the value of a parameter to a pipeline, you use the with
keyword. For example, to pass the value of the buildNumber
Parameter to a second pipeline, you would use the following code:
with:
buildNumber: $(buildNumber)
Once you have defined a parameter and passed it to a pipeline, you can access the parameter’s value in the pipeline. For example, to access the value of the buildNumber Parameter in a second pipeline, you would use the following code:
variable buildNumber: $(buildNumber)
Using Artifacts
Another way to pass variables between pipelines is to use artifacts. Artifacts are files and folders that are produced by a pipeline and can be consumed by other pipelines.
To pass a variable between pipelines using artifacts, you need to store the variable in an artifact in the first pipeline and then reference the artifact in the second pipeline.
To store a variable in an artifact, you use the @artifact
keyword. For example, to store the buildNumber
variable in an artifact called, you would use the following code:
@artifact buildArtifact
To reference an artifact in a pipeline, you use the @input
keyword. For example, to reference the buildArtifact
Artifact in a second pipeline, you would use the following code:
@input buildArtifact
Once you have stored a variable in an artifact and referenced the artifact in a pipeline, you can access the variable’s value. For example, to access the value of the build number Variable in a second pipeline, you would use the following code:
variable buildNumber: $(buildArtifact.buildNumber)
Conclusion
These are just a few ways to pass variables between pipelines in Azure DevOps. The best way to pass variables between pipelines depends on your needs and requirements.