-
Notifications
You must be signed in to change notification settings - Fork 0
/
cd-self-hosted-agent.yml
239 lines (215 loc) · 12.3 KB
/
cd-self-hosted-agent.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
name: $(BuildDefinitionName)_$(date:yyyyMMdd)$(rev:.r)
trigger: none
pr: none
variables:
- group: global-variables
- name: azureSubscription
value: AzureSubscription
- name: terraformVersion
value: 1.0.7
- name: terraformPath
value: agent
- name: tfstatePath
value: agent.tfstate
- name: environment
value: production
- name: resourceGroupName
value: BabosbirdRG
- name: containerName
value: scripts
- name: scriptPath
value: agent/scripts
- name: scriptName
value: configure-self-hosted-agent.sh
- name: tfvarsPath
value: tfvars/agent/agent.tfvars
- name: location
value: westeurope
pool:
vmImage: ubuntu-latest
stages :
- stage: terraform_deployment
displayName: 'Terraform Deployment'
jobs:
- deployment: production
displayName: 'Terraform Apply'
environment: $(environment)
continueOnError: false
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: AzureCLI@2
displayName: 'Create Temporary Storage Account'
continueOnError: true
inputs:
scriptType: bash
scriptLocation: inlineScript
azureSubscription: $(azureSubscription)
addSpnToEnvironment: true
inlineScript: |
#Variables
postfix=$(cat /dev/urandom | tr -dc 'a-z' | fold -w 8 | head -n 1)
storageAccountResourceGroupName="rg$postfix"
storageAccountName="scriptstorage$postfix"
sku="Standard_LRS"
subscriptionName=$(az account show --query name --output tsv)
# Create resource group
echo "Checking if [$storageAccountResourceGroupName] resource group actually exists in the [$subscriptionName] subscription..."
az group show --name $storageAccountResourceGroupName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$storageAccountResourceGroupName] resource group actually exists in the [$subscriptionName] subscription"
echo "Creating [$storageAccountResourceGroupName] resource group in the [$subscriptionName] subscription..."
# Create the resource group
az group create \
--name $storageAccountResourceGroupName \
--location $(location) 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$storageAccountResourceGroupName] resource group successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$storageAccountResourceGroupName] resource group in the [$subscriptionName] subscription"
exit -1
fi
else
echo "[$storageAccountResourceGroupName] resource group already exists in the [$subscriptionName] subscription"
fi
# Create storage account
echo "Checking if [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription..."
az storage account --name $storageAccountName &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$storageAccountName] storage account actually exists in the [$subscriptionName] subscription"
echo "Creating [$storageAccountName] storage account in the [$subscriptionName] subscription..."
az storage account create \
--resource-group $storageAccountResourceGroupName \
--name $storageAccountName \
--sku $sku \
--encryption-services blob 1>/dev/null
# Create the storage account
if [[ $? == 0 ]]; then
echo "[$storageAccountName] storage account successfully created in the [$subscriptionName] subscription"
else
echo "Failed to create [$storageAccountName] storage account in the [$subscriptionName] subscription"
exit -1
fi
else
echo "[$storageAccountName] storage account already exists in the [$subscriptionName] subscription"
fi
# Get storage account key
echo "Retrieving the primary key of the [$storageAccountName] storage account..."
storageAccountKey=$(az storage account keys list --resource-group $storageAccountResourceGroupName --account-name $storageAccountName --query [0].value -o tsv)
if [[ -n $storageAccountKey ]]; then
echo "Primary key of the [$storageAccountName] storage account successfully retrieved"
else
echo "Failed to retrieve the primary key of the [$storageAccountName] storage account"
exit -1
fi
# Create blob container
echo "Checking if [$(containerName)] container actually exists in the [$storageAccountName] storage account..."
az storage container show \
--name $(containerName) \
--account-name $storageAccountName \
--account-key $storageAccountKey &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$(containerName)] container actually exists in the [$storageAccountName] storage account"
echo "Creating [$(containerName)] container in the [$storageAccountName] storage account..."
# Create the container
az storage container create \
--name $(containerName) \
--account-name $storageAccountName \
--account-key $storageAccountKey 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$(containerName)] container successfully created in the [$storageAccountName] storage account"
else
echo "Failed to create [$(containerName)] container in the [$storageAccountName] storage account"
exit -1
fi
else
echo "[$(containerName)] container already exists in the [$storageAccountName] storage account"
fi
# Copy script as blob to the storage account container
az storage blob upload \
--container-name $(containerName) \
--name $(scriptName) \
--account-name $storageAccountName \
--account-key $storageAccountKey \
--file "$(System.DefaultWorkingDirectory)/$(scriptPath)/$(scriptName)"
if [[ $? == 0 ]]; then
echo "[$(scriptName)] successfully copied to the [$(containerName)] container in the [$storageAccountName] storage account"
else
echo "Failed to copy the [$(scriptName)] script to the [$(containerName)] container in the [$storageAccountName] storage account"
exit -1
fi
# Print data
echo "----------------------------------------------------------------------------------------------"
echo "storageAccountName: $storageAccountName"
echo "containerName: $(containerName)"
echo "##vso[task.setvariable variable=storageAccountResourceGroupName;]$storageAccountResourceGroupName"
echo "##vso[task.setvariable variable=storageAccountName;]$storageAccountName"
echo "##vso[task.setvariable variable=storageAccountKey;]$storageAccountKey"
echo "##vso[task.setvariable variable=ok;]true"
- bash: |
echo "Storage Account Name: $(storageAccountName)"
echo "Storage Account Key: $(storageAccountKey)"
displayName: 'Print Variables'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
- task: TerraformInstaller@0
displayName: 'Terraform Install'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
terraformVersion: $(terraformVersion)
- task: TerraformTaskV2@2
displayName: 'Terraform Init'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
provider: 'azurerm'
command: 'init'
backendServiceArm: $(azureSubscription)
backendAzureRmResourceGroupName: $(terraformBackendResourceGroupName)
backendAzureRmStorageAccountName: $(terraformBackendStorageAccountName)
backendAzureRmContainerName: '$(terraformBackendContainerName)'
backendAzureRmKey: $(tfstatePath)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: TerraformTaskV2@2
displayName: 'Terraform Validate'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
provider: 'azurerm'
command: 'validate'
- task: TerraformTaskV2@2
displayName: 'Terraform Plan'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
inputs:
provider: 'azurerm'
command: 'plan'
commandOptions: '-input=false -var="ssh_public_key=$(sshPublicKey)" -var="location=$(location)" -var="resource_group_name=$(resourceGroupName)" -var="script_storage_account_name=$(storageAccountName)" -var="script_storage_account_key=$(storageAccountKey)" -var="container_name=$(containerName)" -var="script_name=$(scriptName)" -var="azure_devops_url=$(azureDevOpsUrl)" -var="azure_devops_pat=$(azureDevOpsPat)" -var="azure_devops_agent_pool_name=$(azureDevOpsAgentPoolName)" -var-file="$(System.DefaultWorkingDirectory)/$(tfvarsPath)'
environmentServiceNameAzureRM: $(azureSubscription)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: TerraformTaskV2@2
displayName: 'Terraform Apply'
condition: and(succeeded(), not(eq(variables.storageAccountName, '')), not(eq(variables.storageAccountKey, '')))
enabled: true
inputs:
provider: 'azurerm'
command: 'apply'
commandOptions: '-input=false -auto-approve -var="ssh_public_key=$(sshPublicKey)" -var="location=$(location)" -var="resource_group_name=$(resourceGroupName)" -var="script_storage_account_name=$(storageAccountName)" -var="script_storage_account_key=$(storageAccountKey)" -var="container_name=$(containerName)" -var="script_name=$(scriptName)" -var="azure_devops_url=$(azureDevOpsUrl)" -var="azure_devops_pat=$(azureDevOpsPat)" -var="azure_devops_agent_pool_name=$(azureDevOpsAgentPoolName)" -var-file="$(System.DefaultWorkingDirectory)/$(tfvarsPath)'
environmentServiceNameAzureRM: $(azureSubscription)
workingDirectory: '$(System.DefaultWorkingDirectory)/$(terraformPath)/'
- task: AzureCLI@2
displayName: 'Delete Temporary Storage Account'
condition: always()
inputs:
scriptType: bash
scriptLocation: inlineScript
azureSubscription: $(azureSubscription)
addSpnToEnvironment: true
inlineScript: |
az group delete \
--name $(storageAccountResourceGroupName) \
--yes
if [[ $? == 0 ]]; then
echo "Temporary storage account successfully deleted"
else
echo "Failed to delete the temporary storage account"
exit -1
fi