Using Bicep params and output in Azure Pipelines

How to use the output of a Bicep deployment triggered from a Azure Pipeline for subsequent tasks, jobs and stages.

Philipp Bauknecht
medialesson
2 min readMar 20, 2023

--

When composing pipelines in Azure DevOps that involve deploying to Azure using Bicep you will at some point want to use pipeline variables as input parameters and use the output of such a deployment, e.g. the name of a resource you just created in a subsequent pipeline task e.g. to deploy your code into the resource.

In Bicep it’s really simple to define output for a deployment like so:

This output is returned from the deployment as JSON string e.g.

To use the output we need to parse the JSON. One way of doing this is using PowerShell:

This will output the value of the Bicep output variable ovar:

OK, but how can we use this in an Azure Pipeline? Well turns out there is a way to set variables inside a script task:

So the next step will be to call this PowerShell script from an AzureCLI task:

Unfortunately using a script in a file raises another challenge: how do we pass input parameters into the Bicep? Luckily my friend Benjamin Abt has just solved this and wrote a small post here: https://medium.com/medialesson/using-azure-devops-pipeline-variables-in-powershell-script-files-11a1670c03fc

So I’m going to add a sample input parameter in the pipeline:

… and then use it in the PowerShell script:

… and let it run through Bicep:

Use variable in a different task

Now let’s use the variable ovar that we just defined in the PowerShell script in a subsequent pipeline task using the syntax $(<stepName>.<variableName>):

Note how we can access the variable ovar in a task within the same jobs by referencing it using the task name.

Use variable in a different stage

To use the variable in a seperate stage or job we need to use stageDependencies with the syntax $[stageDependencies.<stageName>.<jobName>.outputs[‘<taskName>.<variableName>’]]:

Note how we can access the variable ovar by referencing it using stage name, job name and task name.

Use variable from a Deployment job in a different stage

When running the DeployBicep task we just creating inside a deployment job the syntax for accessing the variable from a subsequent stage changes to $[stageDependencies.<stageName>.<deploymentName>.outputs[‘<deploymentName>.<taskName>.<variableName>’]]

When running this pipeline in Azure DevOps this should give us the Hello World output in both sample scripts:

Summary

It’s possible to use both input parameters and output variables when working with Bicep in Azure Pipelines although the syntax may be a bit confusing in the beginning.

--

--

Philipp Bauknecht
medialesson

CEO @ medialesson. Microsoft Regional Director & MVP Windows Development. Father of identical twins. Passionate about great User Interfaces, NYC & Steaks