Deploying Bicep files with Azure Devops Pipelines

Earlier on the blog, we looked at creating our own Bicep script to deploy Azure resources's. Now let's try and combine it with utilizing Azure Devops Pipelines to deploy our Bicep script with a yaml script.

Azurepipelines

This article expects you to have a working Bicep file (see my article regarding Bicep if you do not have an working Bicep script.).

Now what we would like to archive here is that everytime our main development branch is updated our Bicep script will be triggered, to ensure we have the correct resources in our Azure environment.

I recommend utilizing Visual studio code for creating your YAML and Bicep files for proper intellisense, in addition I would recommend that you store your yaml file together with your Bicep file in an IAC folder in your repository instead of only having the yaml file only persisted directly in the pipeline.

In any case our Yaml file needs to archive 2 task's basically, firstly we want to preview our changes and then we want to proceed with doing them if all goes well.

In the yaml file we have in the example here we need to create a few variable in our the Azure Devops Pipeline configuration. Additional variables might be required for your specific bicep file.

  • sub (Service connection with access to your Azure resource group via an service principle)
  • resourceGroupName (name of your resource group)
  • location (location)
  • env (enviornment e.g. test/uat/production)
name: Deploy Bicep filestrigger:
- main
pool:
  vmImage: 'ubuntu-latest'
steps:- task: AzureCLI@2
  displayName: Preview Bicep Changes
  inputs:
    azureSubscription: '$(sub)'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az --version
      az group create --name $(resourceGroupName) --location $(location)
      az deployment group what-if --resource-group $(resourceGroupName) \
         --template-file $(bicepfile) \
         --parameters env=$(env) 
- task: AzureCLI@2
  displayName: Deploy Bicep To Azure
  inputs:
  azureSubscription: '$(sub)'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az deployment group create --resource-group $(resourceGroupName) \
         --template-file $(bicepfile) \
         --parameters env=$(env)

As you can see our in our task's we can actually just utilize inlinescript to deploy our Bicep file which keeps it all nice and tidy.

Alright time to go to dev.azure.com and create our new pipeline by going to Pipelines tab and do create new pipeline.

1. Where is your code?

select azure repo git

2. Select a repostiory

select your azure repo

3. Configure your pipeline

select existing azure pipelines yaml file, and select your file from your repository

4. Review

create your variables as described above.

Time to run the pipeline and watch the magic happen! Please reach out if you want me to cover more in-depth article on any topics related to Azure, .NET or C# development!