Deploy Azure Bicep using Azure DevOps Pipelines

Have you been looking at Project Bicep? Creating some Bicep configurations and wanting to deploy via Azure DevOps? In this blog post, I am going to show how you can deploy an Azure Bicep file using Azure DevOps!

What is Project Bicep?

Its the Next Generation of ARM templates – Bicep is a Domain Specific Language (DSL) for ARM templates. Taken from the documentation:-

It aims to drastically simplify the authoring experience with a cleaner syntax and better support for modularity and code re-use. Bicep is a transparent abstraction over ARM and ARM templates, which means anything that can be done in an ARM Template can be done in bicep 

https://github.com/Azure/bicep

Examples of Azure Bicep configurations can be found here:- https://github.com/Azure/bicep/tree/main/docs/examples

Bicep sample configuration

Before I show the sample configuration, I also recommend the Bicep extension for VSCode – it is awesome, with built-in Intellisense!

https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep

In my example, I want my Bicep file to deploy an Azure Virtual Network

param location string = resourceGroup().location

var virtualNetwork = {
  name: 'tamopsvnet'
  location: location
  addressPrefixes: [
    '10.0.0.0/16'
    ]
  subnets: [
    {
      name: 'testsubnet1'
      properties: {
        addressPrefix: '10.0.0.0/24'
      }
    }
    {
      name: 'testsubnet2'
      properties: {
        addressPrefix: '10.0.1.0/24'
      }
    }
  ]
}

resource virtualnetwork 'Microsoft.Network/virtualNetworks@2020-06-01' = {
  name: virtualNetwork.name
  location: virtualNetwork.location
  properties: {
    addressSpace: {
      addressPrefixes: virtualNetwork.addressPrefixes
    }
    subnets: virtualNetwork.subnets
  }
}


Great, we now have a sample configuration, I created a stage in Azure DevOps with one job and two tasks:-

  • Task1 is Bash@3 that will build ARM template from bicep (main.bicep) file – This works if AZ Cli version is > 2.20.0, older version workaround supplied also
  • Task2 is AzureCLI@2 that will create a resource group to where I want my ARM configuration to be deployed to along with deploying the ARM template (.json file) from Task1
  - stage: deployBicep
    jobs:
      - job: "BicepConfigure"
        steps:
 
          - task: Bash@3
            displayName: 'Bicep Build'
            inputs:
              targetType: inline
              script: |
                az bicep build --file $(System.DefaultWorkingDirectory)/BicepFiles/main.bicep

          - task: AzureCLI@2
            displayName: 'Deploy Bicep Configuration'
            inputs:
              azureSubscription: 'tamopstf'
              scriptType: bash
              scriptLocation: inlineScript
              addSpnToEnvironment: true
              inlineScript: |
                #!/bin/bash
                az group create -l uksouth -n bicep-rg 
                az deployment group create -f $(System.DefaultWorkingDirectory)/BicepFiles/main.json -g bicep-rg

Use below stage if AZ CLI < 2.20.0

  - stage: deployBicep
    jobs:
      - job: "BicepConfigure"
        steps:
 
          - task: Bash@3
            displayName: 'Install Bicep'
            inputs:
              targetType: inline
              script: |
                curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64
                chmod +x ./bicep
                ./bicep build $(System.DefaultWorkingDirectory)/BicepFiles/main.bicep

Azure DevOps Folder Structure

Azure-Bicep-Deploy
    └──BicepFiles
            └──main.bicep
    └──pipelines
            └──pipeline.yaml

Azure DevOps Full Pipeline

name: $(BuildDefinitionName)_$(date:yyyyMMdd)$(rev:.r)

trigger:
  batch: true 
  branches:
    include:
      - master

# Don't run against PRs
pr: none

stages :

  - stage: deployBicep
    jobs:
      - job: "BicepConfigure"
        steps:
 
          - task: Bash@3
            displayName: 'Bicep Build'
            inputs:
              targetType: inline
              script: |
                az bicep build --file $(System.DefaultWorkingDirectory)/BicepFiles/main.bicep

          - task: AzureCLI@2
            displayName: 'Deploy Bicep Configuration'
            inputs:
              azureSubscription: 'tamopstf'
              scriptType: bash
              scriptLocation: inlineScript
              addSpnToEnvironment: true
              inlineScript: |
                #!/bin/bash
                az group create -l uksouth -n bicep-rg 
                az deployment group create -f $(System.DefaultWorkingDirectory)/BicepFiles/main.json -g bicep-rg

Pipeline completed

Awesome, successfully deployed a Bicep File using Azure DevOps pipelines! I am really looking forward to seeing Project Bicep receive further updates; it’s going to be a fun journey!

Tutorial for Azure Bicep https://github.com/Azure/bicep/blob/main/docs/tutorial/01-simple-template.md

Github URL for code used above

1 comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s