How to Pass Variables Between Pipelines in Azure DevOps?

0
44
How to Pass Variables Between Pipelines in Azure DevOps

In Azure DevOps, pipelines are used to automate the build, test, and deploy of software. 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 output of a test from one pipeline to another.

There are a few ways to pass variables between pipelines in Azure DevOps.

Using Environment Variables

One way to pass variables between pipelines is to use environment variables. Environment variables are global variables that can be accessed by all pipelines.

To pass a variable between pipelines using environment variables, you need to 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 buildNumber, 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

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 need to define the parameter in the first pipeline and then pass the value of the parameter 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 value of the parameter 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 buildArtifact, 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 value of the variable in the pipeline. For example, to access the value of the buildNumber 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 will depend on your specific needs and requirements.