1+ name : Build and deploy infrastructure as code to Azure
2+
3+ trigger :
4+ branches :
5+ include :
6+ - master
7+ paths :
8+ include :
9+ - bicep/**
10+ - ' .ado/infra-deploy.yml'
11+
12+ parameters :
13+ - name : teardown
14+ displayName : Should teardown infrastructure?
15+ type : boolean
16+ default : false
17+
18+ pool :
19+ vmImage : ' ubuntu-latest'
20+
21+ variables :
22+ - name : REGISTRY
23+ value : ghcr.io
24+ - name : BACKEND_API_IMAGE_NAME
25+ value : azure/tasksmanager-backend-api
26+ - name : FRONTEND_APP_IMAGE_NAME
27+ value : azure/tasksmanager-frontend-webapp
28+ - name : BACKEND_PROCESSOR_IMAGE_NAME
29+ value : azure/tasksmanager-backend-processor
30+ - group : ' AcaApp'
31+
32+
33+ stages :
34+ - stage : Lint
35+ condition : eq('${{ parameters.teardown }}', false)
36+ jobs :
37+ - job : Lint
38+ displayName : Lint bicep files
39+ steps :
40+ - checkout : self
41+ - task : Bash@3
42+ displayName : Perform linting
43+ inputs :
44+ targetType : ' inline'
45+ script : |
46+ set -e
47+ echo "Linting bicep files"
48+ az bicep build --f bicep/main.bicep
49+
50+ - stage : Validate
51+ condition : and(succeeded(), eq('${{ parameters.teardown }}', false))
52+ dependsOn : Lint
53+ jobs :
54+ - job : Validate
55+ displayName : Create RG and Validate bicep template
56+ steps :
57+ - checkout : self
58+ - task : AzureCLI@2
59+ displayName : Create resource group
60+ inputs :
61+ azureSubscription : $(AZURE_SUBSCRIPTION)
62+ scriptType : ' bash'
63+ scriptLocation : ' inlineScript'
64+ inlineScript : |
65+ set -e
66+ echo "Creating resource group"
67+ if [[ $(az group exists -n $(RESOURCE_GROUP)) == true ]]
68+ then
69+ echo "Resource group already exists in the subscription"
70+ else
71+ az group create --name $(RESOURCE_GROUP) --location $(LOCATION)
72+ echo "Resource group created"
73+ fi
74+
75+ - task : AzureResourceManagerTemplateDeployment@3
76+ inputs :
77+ deploymentScope : ' Resource Group'
78+ azureResourceManagerConnection : $(AZURE_SUBSCRIPTION)
79+ action : ' Create Or Update Resource Group'
80+ resourceGroupName : $(RESOURCE_GROUP)
81+ location : $(LOCATION)
82+ templateLocation : ' Linked artifact'
83+ csmFile : ' bicep/main.bicep'
84+ csmParametersFile : ' bicep/main.parameters.json'
85+ deploymentMode : ' Validation'
86+
87+ - stage : Deploy
88+ displayName : Deploy infrastructure using GHCR image
89+ dependsOn : Validate
90+ condition : and(succeeded() , eq(variables.CONTAINER_REGISTRY_NAME, ''))
91+ jobs :
92+ - job : Deploy
93+ displayName : Deploy infrastructure using GHCR image
94+ steps :
95+ - checkout : self
96+ - task : AzureResourceManagerTemplateDeployment@3
97+ inputs :
98+ deploymentScope : ' Resource Group'
99+ azureResourceManagerConnection : $(AZURE_SUBSCRIPTION)
100+ action : ' Create Or Update Resource Group'
101+ resourceGroupName : $(RESOURCE_GROUP)
102+ location : ' $(LOCATION)'
103+ templateLocation : ' Linked artifact'
104+ csmFile : ' bicep/main.bicep'
105+ csmParametersFile : ' bicep/main.parameters.json'
106+ overrideParameters : ' -containerRegistryName -backendProcessorServiceImage $(REGISTRY)/$(BACKEND_PROCESSOR_IMAGE_NAME):latest -backendApiServiceImage $(REGISTRY)/$(BACKEND_API_IMAGE_NAME):latest -frontendWebAppServiceImage $(REGISTRY)/$(FRONTEND_APP_IMAGE_NAME):latest'
107+ deploymentMode : ' Incremental'
108+ deploymentName : ' azdo-deploy-$(Build.BuildId)'
109+
110+ - stage : Create_Import_ACR
111+ displayName : Create Azure Container Registry if needed
112+ dependsOn : Validate
113+ condition : and(succeeded(), ne(variables.CONTAINER_REGISTRY_NAME, ''), eq('${{ parameters.teardown }}', false))
114+ jobs :
115+ - job : Create_ACR
116+ displayName : Create Azure Container Registry if needed
117+ steps :
118+ - task : AzureCLI@2
119+ displayName : Create Azure Container Registry if needed
120+ inputs :
121+ azureSubscription : $(AZURE_SUBSCRIPTION)
122+ scriptType : ' bash'
123+ scriptLocation : ' inlineScript'
124+ inlineScript : |
125+ set -e
126+ echo "Creating Azure Container Registry if needed"
127+ if [[ $(az acr check-name -n $(CONTAINER_REGISTRY_NAME) -o tsv --query "nameAvailable") == false ]]
128+ then
129+ echo "ACR already exists."
130+ if [[ $(az acr list -g $(RESOURCE_GROUP) -o tsv --query "[?name=='$(CONTAINER_REGISTRY_NAME)']") == "" ]]
131+ then
132+ echo "ACR exists but not in the resource group $(RESOURCE_GROUP). Please select a different name for the ACR and update in repository variable."
133+ exit 1
134+ fi
135+ else
136+ az acr create --name $(CONTAINER_REGISTRY_NAME) --resource-group $(RESOURCE_GROUP) --sku Basic --location $(LOCATION)
137+ echo "ACR created"
138+ fi
139+ - job : Import_Image
140+ displayName : Import image to ACR from GHCR
141+ dependsOn : Create_ACR
142+ condition : succeeded()
143+ steps :
144+ - task : AzureCLI@2
145+ displayName : Import images from GitHub Container Registry
146+ inputs :
147+ azureSubscription : $(AZURE_SUBSCRIPTION)
148+ scriptType : ' bash'
149+ scriptLocation : ' inlineScript'
150+ inlineScript : |
151+ set -e
152+ echo "Import images from GitHub Container Registry"
153+ az acr import --name $(CONTAINER_REGISTRY_NAME) --source $(REGISTRY)/$(BACKEND_PROCESSOR_IMAGE_NAME):latest --image tasksmanager/tasksmanager-backend-processor --force
154+ az acr import --name $(CONTAINER_REGISTRY_NAME) --source $(REGISTRY)/$(BACKEND_API_IMAGE_NAME):latest --image tasksmanager/tasksmanager-backend-api --force
155+ az acr import --name $(CONTAINER_REGISTRY_NAME) --source $(REGISTRY)/$(FRONTEND_APP_IMAGE_NAME):latest --image tasksmanager/tasksmanager-frontend-webapp --force
156+
157+ - stage : Deploy_With_ACR
158+ displayName : Deploy infrastructure using ACR image
159+ dependsOn : Create_Import_ACR
160+ condition : and(succeeded(), ne(variables.CONTAINER_REGISTRY_NAME, ''), eq('${{ parameters.teardown }}', false))
161+ jobs :
162+ - job : Deploy
163+ displayName : Deploy infrastructure using ACR image
164+ steps :
165+ - checkout : self
166+ - task : AzureResourceManagerTemplateDeployment@3
167+ inputs :
168+ deploymentScope : ' Resource Group'
169+ azureResourceManagerConnection : $(AZURE_SUBSCRIPTION)
170+ action : ' Create Or Update Resource Group'
171+ resourceGroupName : $(RESOURCE_GROUP)
172+ location : $(LOCATION)
173+ templateLocation : ' Linked artifact'
174+ csmFile : ' bicep/main.bicep'
175+ csmParametersFile : ' bicep/main.parameters.json'
176+ overrideParameters : ' -containerRegistryName $(CONTAINER_REGISTRY_NAME) -backendProcessorServiceImage $(CONTAINER_REGISTRY_NAME).azurecr.io/tasksmanager/tasksmanager-backend-processor:latest -backendApiServiceImage $(CONTAINER_REGISTRY_NAME).azurecr.io/tasksmanager/tasksmanager-backend-api:latest -frontendWebAppServiceImage $(CONTAINER_REGISTRY_NAME).azurecr.io/tasksmanager/tasksmanager-frontend-webapp:latest'
177+ deploymentMode : ' Incremental'
178+ deploymentName : ' azdo-deploy-$(Build.BuildId)'
179+
180+ - stage : Teardown
181+ displayName : Teardown infrastructure
182+ dependsOn : []
183+ condition : eq('${{ parameters.teardown }}', true)
184+ jobs :
185+ - job : Teardown
186+ displayName : Teardown infrastructure
187+ steps :
188+ - task : AzureResourceManagerTemplateDeployment@3
189+ inputs :
190+ deploymentScope : ' Resource Group'
191+ azureResourceManagerConnection : $(AZURE_SUBSCRIPTION)
192+ action : ' DeleteRG'
193+ resourceGroupName : $(RESOURCE_GROUP)
0 commit comments