Validate a Bicep file in your YAML pipeline!

When setting up CI/CD pipelines, it’s good to have an explicit build stage.  This stage is typically used to compile your code and run unit tests.  When we are dealing with Infrastructure as Code, such as Bicep or ARM templates, there is no need to compile your files and unit testing is also difficult.  However, I’m convinced that it is already very handy if you can validate your Bicep file during the build.

This can be easily can through the az bicep build command.  Because it is an az command, you would typically run it via an Azure CLI task.  I am not in favor of that, because I don’t want to link my build stage to a certain Azure subscription (unless I would like to execute my deployment already to a dedicated build environment).  That’s why I think it’s much cleaner to use the PowerShell task.  In this way, it’s a 100% clear that this is a pure syntactical validation, without any link to Azure.  Here’s an example:

  - task: PowerShell@2
    displayName: Validate Bicep file
      targetType: inline
      script: 'az bicep build --file infra.bicep'
      workingDirectory: '$(Build.SourcesDirectory)/infra'
When executing this in my Azure DevOps pipeline, with a Bicep file that points to a non-existing module, I get the following exception.






Your Azure Coach is specialized in organizing Azure trainings that are infused with real-life experience. All our coaches are active consultants, who are very passionate and who love to share their Azure expertise with you.