Creating your first pipeline with Azure DevOps

Hello everyone!

My last post was about the basics for your Azure DevOps environment, now I am going to show you how to create a pipeline with a simple Bicep template to create resources in Azure. Please read my last post about the basics in Azure DevOps to continue this post for your very first pipeline creation!

Prerequisites

  • Azure subscription (Azure DevOps is free, but I will connect to Azure)
  • Contributor or Owner role on the subscription (less rights are possible, but take more time to setup)
  • Azure CLI version 2.4 or higher
  • Visual Studio Code (VSCode) (Feel free to use any coding tool you want)
  • Azure Pipelines extension in VSCode
  • Bicep extension in VSCode
  • YAML extension in VSCode

Setting up the repo

The first thing that is important, that is the repo in Azure DevOps. This needs to be in sync with VSCode, because we are going to commit and push changes to Azure DevOps.

  1. Go to the Azure DevOps portal.
  2. Then click on Repos and click on the button Clone in VS Code. This will give you a popup you need to click on Open. Then select the folder where you want to create your files in the repo.

Creating the bicep template

Now we can create a bicep template with information about the resources that need to be created. We are going to create a storage account with the bicep template. The resource group is going to be created with the pipeline and the YAML file.

Create a new file in VSCode with .bicep as extension and add this code and fill in your parameters in the bicep file. When using the VSCode Bicep extension, the code will automatically fill in some of the values.

param location string = resourceGroup().location
param storageAccountName string = 'm2c${uniqueString(resourceGroup().id)}'

resource symbolicname 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  
  properties: {
    accessTier: 'Hot'
  }
}

Creating the YAML file for the pipeline

We need to create the pipeline file to setup in Azure DevOps. Create a new file with the .yaml extension in VSCode to paste the following code. Please use your own parameters, because the service connection name could be different.

trigger:
- main

name: deploy storage account

variables:
  vmImageName: 'ubuntu-latest'

  azureServiceConnection: 'BicepAVDSC'
  resourceGroupName: 'AVDpoc'
  location: 'westeurope'
  templateFile: 'storage.bicep'
pool:
  vmImage: $(vmImageName)

steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: $(azureServiceConnection)
    scriptType: bash
    scriptLocation: inlineScript
    useGlobalConfig: false
    inlineScript: |
      az --version
      az group create --name $(resourceGroupName) --location $(location)
      az deployment group create --resource-group $(resourceGroupName) --template-file $(templateFile)

Setting up the pipeline in Azure DevOps

The files are created, so we can save them and sync the files to your Azure DevOps repo. You can click on the source control button to commit the changes and sync them.

If the commit did not work, please make sure you have created a branch first in VSCode.

The files are in place, so we can create the pipeline now.

  1. Click on Pipelines in the Azure DevOps portal.
  2. Click on Create Pipeline (This could be in the center of the screen when there are no pipelines yet). Or else create one on the right side of the screen.
  3. Click on Azure Repos Git.
  4. Click on your repo.
  5. Click on Existing Azure Pipelines YAM file.
  6. Now you see the YAML file content, click on the right side on the arrow and save the pipeline. You need to rename it afterwards, because it gets a standard name. Renaming can be done when clicking on the three dots on the top right of the portal.

Running the pipeline

Everything is in place to run your pipeline. When you click on the pipeline, you have the option to run the pipeline and start your deployment in Azure. First you need to give permissions to the pipeline, click on the running job and check the following message.

Now the pipeline will run automatically and all resources are going to be deployed.

When everything finishes successfully the pipeline will give you the following screen.

When we check in Azure we see that everything worked out well! A resource group and a storage account. Now that is cool!

Final Thoughts

Automating your deployments with YAML and Bicep is giving you a lot of options for Azure infrastructure. The companies want deployments as Infrastructure as Code and when you start with YAML and Bicep, you will have great possibilities of automation. There are more options available, because Terraform is also a great solution for Infrastructure as Code. Next time we are going to create an Azure Virtual Desktop environment with Bicep templates. Feel free to comment on this post, or ask me any questions about this post.

Author

  • Mischa Sachse

    Mischa Sachse is one of the founders of the Cloud Experts Community. Would you like to join in the fun? Make sure to contact him via the mail button below or find out more about him on his personal website.

Leave a Reply

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