Pass variables form build pipelines to release pipelines in Azure DevOps

Azure DevOps is a great environment for CI/CD. Next to the Repo integrations, planning options with Boards and other useful stuff, Pipelines are very helpful.

Without going too much into detail, let’s say you have

  • Build pipelines for creating the packages (artifacts) and
  • Release pipelines for deployment on your different environments

You can work with variables in the pipelines, to set up connection strings or other environment specific details, for example.

But what if you need to pass some information from a build pipeline over to a release pipeline? In this case, an Id value from a feature branch to the release pipeline for tagging docker images?

So, to get the needed information, you can try it with http call to the DevOps API but in my mind that was too complicated for the demand and you have to deal with Tokens for security.

The solution for the problem works with a simple text document, containing the needed information which is passed over to the release through the artifacts.

Step to do:

  • Get the needed information and save it to a file in the artifact
  • Read the file from the artifact and dynamically set a pipeline variable
  • Consume the variable in a later step
  • create an additional Stage for image clean

To give a better overview, I divided the solution into two posts:

  1. Write the data to the artifact, put it later in a pipeline variabel and consume the variable (this post)
  2. work with an additional stage in the release pipeline for clean up

Get the needed information and save it to a file in the artifact

I like to work with Powershell in DevOps. So I added a task in the build pipeline which is triggered by an opened pull request.

The essentials in that steps are the usage of a System variable and the order to put the information in a text file in the build artifact.

$ featureId = "$(System.PullRequest.SourceBranch)"
# Some Regex stuff done here
# write to file:
Set-Content -Path $(build.artifactstagingdirectory)\featureId.txt -Value $featureId

You can read more about the DevOps predefined variables here:

https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

Read the file from the artifact and dynamically set a pipeline variable

In the Release pipeline, which is responsible to create Docker images out of build artifact files, the needed Id information is read from the textfile with another power shell task. The data can be handed over to a pipeline variable for further consummation. If you work with multiple artifacts for the release, you can get the name of the triggering artifacts.

try{   
    #read from file: 
    $featureId = Get-Content $(System.DefaultWorkingDirectory)/Buildpipeline/artifact/featureId.txt
    #write to var:
    echo "##vso[task.setvariable variable=branchId]$featureId"
    #useful predefined var: 
    $artifact = "$(Release.TriggeringArtifact.Alias)"
    Write-Host "artifact: " $artifact
}
catch{
    $_.Exception.Message
}   

Consume the variable in a later step

In a later step of the Release pipeline, which builds the Docker image, the value of the pipeline var is read.

Leave a comment

Your email address will not be published. Required fields are marked *