Onur Yasarlar
Search…
Azure Infrastructure Automation with Gitlab CI

Introduction

Creating new resources in Azure is very easy thanks to Azure Portal, but what if you want to create an infrastructure with code. One of the options is to use ARM templates for infrastructure deployment. If you want to implement a proper CI/CD against your ARM templates, Gitlab CI can be a very strong candidate. Let's assume if you have a pipeline to create the infrastructure and delete it upon a successful deployment in order to save cost. Wouldn't it be perfect? If you say "yes", let's go deeper and see how I implemented the solution.

Setting up Gitlab Project

I am not going into the details of how you can create a Gitlab project as it is done by simply clicking a few buttons, but the most important part of the automation is to create variables which we will use in Gitlab shared runners to authenticate to Azure Cloud.
You can add variables by clicking "Settings" and then "CI/CD" in your project. The "Variables" blade should be visible and you can introduce such variables:
Do not forget to mark your variables as "Masked". Otherwise they will be visible in the Gitlab CI pipeline and they contain sensitive information about your Azure subscription
If you wish to mark your variables protected, please remember that they can be only accessed from protected branches
VARIABLE NAME
DESCRIPTION
SP_ID
Azure Service Principal account to run the deployment
SP_SECRET
Secret of the service principal account
SUBSCRIPTION_ID
Azure subscription where resources will be created
TENANT_ID
Tenant ID of your Azure subscription

Setting up .gitlab-ci.yaml

Now it is time to set up your pipeline to login to the Azure service principal account and deploy resources defined in your subscription. I am using Gitlab shared runners and "mcr.microsoft.com/azure-cli" container image to run the pipeline.
One of the important things is to use a dedicated resource group for the deployment tests so we can destroy whatever is inside that resource group to save costs.
1
variables:
2
DEFAULT_RG:
3
description: "Default resource group to deploy the resources for testing"
4
value: "test-rg"
5
DEFAULT_LOCATION:
6
description: "Default location of the testing resource group"
7
value: "northeurope"
8
9
default:
10
image: mcr.microsoft.com/azure-cli
11
before_script:
12
- az login --service-principal --username $SP_ID --password $SP_SECRET --tenant $TENANT_ID
13
- az account set --subscription $SUBSCRIPTION_ID
14
- set -euo pipefail
Copied!
set -euo pipefail will prevent pipeline to run further steps if one of the variables is empty
The remaining of the pipeline can change based on what you want to test but let me paste you the full content here so you can see what I am testing under that small scope. The respective Gitlab project can also be found here.
1
variables:
2
DEFAULT_RG:
3
description: "Default resource group to deploy the resources for testing"
4
value: "test-rg"
5
DEFAULT_LOCATION:
6
description: "Default location of the testing resource group"
7
value: "northeurope"
8
9
default:
10
image: mcr.microsoft.com/azure-cli
11
before_script:
12
- az login --service-principal --username $SP_ID --password $SP_SECRET --tenant $TENANT_ID
13
- az account set --subscription $SUBSCRIPTION_ID
14
- set -euo pipefail
15
16
stages:
17
- validate
18
- deploy
19
- destroy
20
21
validate virtual network:
22
stage: validate
23
script:
24
- az deployment group validate -g ${DEFAULT_RG} -f virtual-network/virtual-network.json -p virtual-network/virtual-network.parameters.json
25
26
validate virtual machine:
27
stage: validate
28
script:
29
- az deployment group validate -g ${DEFAULT_RG} -f virtual-machine/linux-vm.json -p virtual-machine/linux-vm.parameters.json -p adminPassword=$VM_INITIAL_PASSWORD
30
31
deploy virtual network:
32
stage: deploy
33
script:
34
- az deployment group create -g ${DEFAULT_RG} -f virtual-network/virtual-network.json -p virtual-network/virtual-network.parameters.json
35
36
deploy virtual machine:
37
stage: deploy
38
script:
39
- az deployment group create -g ${DEFAULT_RG} -f virtual-machine/linux-vm.json -p virtual-machine/linux-vm.parameters.json -p adminPassword=$VM_INITIAL_PASSWORD
40
needs:
41
- job: "deploy virtual network"
42
43
destroy test environment:
44
stage: destroy
45
script:
46
- az deployment group create -g ${DEFAULT_RG} -f resource-group/empty-resource-group.json --mode "Complete"
47
48
Copied!
This pipeline will run validation of the resource to be created under the test resource group and will deploy them. After the successful deployment, it will use a very good trick to delete all resources in the resource group by doing a complete deployment with an empty Azure resource.

Conclusion

The above approach can let you test your ARM templates with Gitlab CI pipeline at a minimal cost. There are various ways of testing Azure deployment including Azure DevOps pipeline but I wanted to test it under Gitlab CI.
Last modified 3mo ago