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 inputs: targetType: inline script: 'az bicep build --file infra.bicep' workingDirectory: '$(Build.SourcesDirectory)/infra'
Lovely!
Cheers,