The other posts in this Series can be found below.
Day 35 - Practical Guide for YAML Build Pipelines in Azure DevOps - Part 1
Day 38 - Practical Guide for YAML Build Pipelines in Azure DevOps - Part 2
Day 39 - Practical Guide for YAML Build Pipelines in Azure DevOps - Part 3
Day 40 - Practical Guide for YAML Build Pipelines in Azure DevOps - Part 4
Today, we are going to refactor our Azure Build Pipeline into a single bash script and a single Azure CLI Task.
In this article:
Clone the Azure DevOps Repo in VS Code
Transform the existing tasks into a Bash Script
Update the YAML Configuration for the Build Pipeline
Things to Consider
Conclusion
SPONSOR: Need to stop and start your development VMs on a schedule? The Azure Resource Scheduler let's you schedule up to 10 Azure VMs for FREE! Learn more HERE
Clone the practical-yaml-build-pipe repository in VS Code. If you need a refresher of how to do this, please go back and review Day 13.
NOTE: Editing a Bash Script directly in a Repo in Azure DevOps in a Web Browser can change the End of Line sequence of the bash script. If this happens, your script is going to fail in an Azure CLI task, spectacularly! When making changes to the script that we are creating in the next few steps, make sure to change the End of Line sequence for the file from CRLF to LF in VS Code. The option to do this can be found on the bottom right hand of VS Code as shown below and can be set per file.
After you've successfully cloned your practical-yaml-build-pipe repository in VS Code, create a new file called base-infra.sh. Make sure the End of Line sequence of this file is changed from CRLF to LF.
Next, copy and paste the code below into it and commit it to the repository.
#!/bin/bash
az group create \
--name practical-yaml \
--location westeurope
az acr create \
--name pracazconreg \
--resource-group practical-yaml \
--sku Basic
az acr login \
--name pracazconreg \
--output table
While the script above will technically work when called in an Azure CLI Task, there is no context as to the purpose of the script. Add the following comments to the script as shown below.
#!/bin/bash
# Author: Ryan Irujo
# Name: base-infra.sh
# Description: Deploys Infrastructure to a target Azure Sub from an Azure CLI Task in Azure DevOps.
# Deploying the 'practical-yaml' Resource Group.
az group create \
--name practical-yaml \
--location westeurope
# Deploying the 'pracazconreg' Azure Container Registry.
az acr create \
--name pracazconreg \
--resource-group practical-yaml \
--sku Basic
# Logging into the 'pracazconreg' Azure Container Registry.
az acr login \
--name pracazconreg \
--output table
Now that we've documented the purpose of the script, we should customize the output of the script to be more readable in the logs of the Build Pipeline. If we left the script as is, you could still identify if it ran successfully or not, but not without having to read through the entire log entry of the Azure CLI Task in the job.
Replace the existing content of the base-infra.sh script with the code below and commit it to the repository.
#!/bin/bash
# Author: Ryan Irujo
# Name: base-infra.sh
# Description: Deploys Infrastructure to a target Azure Sub from an Azure CLI Task in Azure DevOps.
# Deploying the 'practical-yaml' Resource Group.
az group create \
--name practical-yaml \
--location westeurope \
--output none && echo "[---info---] Resource Group: practical-yaml was created successfully or already exists."
# Deploying the 'pracazconreg' Azure Container Registry.
az acr create \
--name pracazconreg \
--resource-group practical-yaml \
--sku Basic \
--output none && echo "[---info---] Azure Container Registry: pracazconreg was created successfully or already exists."
# Logging into the 'pracazconreg' Azure Container Registry.
az acr login \
--name pracazconreg \
--output none && echo "[---info---] Logged into Azure Container Registry: pracazconreg."
By modifying each command to use --output none and then echoing back a single sentence after running successfully, we should see only three lines of output at the end of our Azure CLI task when this script runs.
NOTE: You should actually see four lines of output, but more on that in Things to Consider.
Next, we need to update the YAML Configuration for the Build Pipeline to use our new script. Replace the existing configuration in the idempotent-pipe.yaml file with the configuration shown below. When you are finished, save and commit it to the repository.
# Builds are automatically triggered from the master branch in the 'practical-yaml-build-pipe' Repo.
trigger:
- master
pool:
# Using a Microsoft Hosted Agent - https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops
vmImage: ubuntu-18.04
steps:
# Azure CLI Task - Deploying Base Infrastructure.
- task: AzureCLI@2
displayName: 'Deploying Base Infrastructure'
inputs:
# Using Service Principal, 'sp-az-build-pipeline', to authenticate to the Azure Subscription.
azureSubscription: 'sp-az-build-pipeline'
scriptType: 'bash'
scriptLocation: 'scriptPath'
scriptPath: './base-infra.sh'
Review the logs of the most current job in the practical-yaml-build-pipe Build Pipeline and you should see the following output from the Deploying Base Infrastructure Azure CLI Task.
Every time we made a commit to the practical-yaml-build-pipe repository, the Build Pipeline was triggered to run. Be aware of this behavior when working with Build Pipelines in Production environments. You have several options of triggering Build Pipelines including by branch or disabling them altogether.
There were actually 4 lines of output instead of three. This is because of the way that the output of the az acr login currently behaves and is something we will address in Part 5 when we look at further refining and parsing commands and returning output in the base-infra.sh script.
We updated the YAML file to use Azure CLI Task version 2.0 (AzureLCI@2) instead of version 1.0 (AzureCLI@1). You can read about the updates in version 2.0 here.
In today's article we refactored our Azure Build Pipeline into a single bash script and a single Azure CLI Task. If there's a specific scenario that you wish to be covered in future articles, please create a New Issue in the starkfell/100DaysOfIaC GitHub repository.