Running bicep within Azure DevOps Pipelines

During Ignite Microsoft released bicep version 0.3.1. With this version the bicep language is not experimental any more. Some pointer about this version are:

  • Parity with ARM Templates
  • Integration with Azure CLI (v2.20.0+) and Azure PowerShell (v5.6.0+)
  • De-compiler
  • Supported by Microsoft

If you want to get started with bicep you can read my post about bicep I wrote a few months ago. When building ARM templates you may also want to start building the templates in the bicep language and execute the build within a automated pipelines to for example perform testing against the template with the ARM-TTK tooling.

Support

At the time of writing this post there is no default support for bicep in Azure DevOps. As mentioned above it is integrated within Azure CLI and Azure PowerShell but the CLI task and Azure PowerShell task in Azure DevOps do not already contain the latest version of the tool to support bicep.

Running bicep in a pipeline

So how do we run bicep then within a pipeline? To get started the tooling needs to be installed on the agent. As I normally use the Microsoft hosted agent the bicep tooling needs to be installed on each run as we do not have access to the agents.

This brings us to the first task for the pipeline that will perform the install. To install bicep the following bash code is used.

curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
chmod +x ./bicep
sudo mv ./bicep /usr/local/bin/bicep
bicep --help

The script it self needs to be performed on a Linux machine to install the bicep tooling and configure bicep into the PATH variable.

Doing this in a Azure DevOps Pipeline is very easy by using the bash task:

- bash: |
   curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
   chmod +x ./bicep
   sudo mv ./bicep /usr/local/bin/bicep
   bicep --help
   
  displayName: 'Install bicep'

With bicep installed the bicep commands can be executed. In my Pipelines a regularly do this in a separate task but the bash code could be added to the previous task as well.

- bash: |
   bicep build $(System.DefaultWorkingDirectory)/storage.bicep
   
  displayName: 'Build bicep file'

In the above task the bicep file in my repository is build and saved in the same location.

Completing the pipeline

With the possibility to build the bicep files in the pipeline the pipeline can be completed to copy the generated ARM templates to for example the pipeline artifacts location. These last steps are included in the sample pipeline shown below.

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- bash: |
   curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
   chmod +x ./bicep
   sudo mv ./bicep /usr/local/bin/bicep
   bicep --help
   
  displayName: 'Install bicep'

- bash: |
   bicep build $(System.DefaultWorkingDirectory)/storage.bicep
   
  displayName: 'Build bicep file'

- task: CopyFiles@2
  displayName: 'Copy Files to: $(build.artifactstagingdirectory)\arm'
  inputs:
    SourceFolder: '$(System.DefaultWorkingDirectory)'
    Contents: '*.json'
    TargetFolder: '$(build.artifactstagingdirectory)/arm'

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(build.artifactstagingdirectory)/arm'
    artifact: 'drop'
    publishLocation: 'pipeline'

2 Replies to “Running bicep within Azure DevOps Pipelines”

  1. Where are $(System.DefaultWorkingDirectory) and $(build.artifactstagingdirectory) set up and what do they actually equate to. Are they pointing to a storage account or a repo somewhere ?

    1. $(System.DefaultWorkingDirectory) and $(build.artifactstagingdirectory) are variables predefined variables in a Azure DevOps agent.

      build.artifactstagingdirectory: The local path on the agent where any artifacts are copied to before being pushed to their destination. For example: c:\agent_work\1\a
      System.DefaultWorkingDirectory: The local path on the agent where your source code files are downloaded. For example: c:\agent_work\1\s

      Use predefined variables

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.