-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-pipelines.yml
153 lines (130 loc) · 5.72 KB
/
azure-pipelines.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
pool: 'Azure Pipelines'
parameters:
- name: resourceGroupName
displayName: "Resource Group name:"
type: string
- name: functionAppName
displayName: "Function App name:"
type: string
- name: storageAccountName
displayName: "Storage Account name (globally unique):"
type: string
- name: serviceConnection
displayName: "Azure Service Connection:"
type: string
- name: subscriptionId
displayName: "Azure Subscription ID:"
type: string
- name: location
displayName: "Azure Region:"
type: string
default: westeurope
- name: managedIdentityRoleName
displayName: "Managed Identity Role name:"
type: string
default: Contributor
- name: expiryDays
displayName: "Number of days before cleaning Subscription:"
type: number
default: 14
- name: createInfrastructure
displayName: "Create Infrastructure"
type: boolean
default: true
steps:
- ${{ if parameters.createInfrastructure }}:
- task: AzureCLI@2
displayName: Set up Function App Infrastructure
inputs:
azureSubscription: "${{ parameters.serviceConnection }}"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
tags="ExpiryDate=Exempt"
echo "Setting up Azure CLI..."
az config set core.only_show_errors=yes
echo "Switching to Subscription ${{ parameters.subscriptionId }}..."
az account set --subscription ${{ parameters.subscriptionId }} --output "none"
echo "Creating Resource Group ${{ parameters.resourceGroupName }} in Location ${{ parameters.location }}..."
az group create \
--name "${{ parameters.resourceGroupName }}" \
--location "${{ parameters.location }}" \
--tags "$tags" \
--output "none"
echo "Creating Storage Account ${{ parameters.storageAccountName }} in Resource Group ${{ parameters.resourceGroupName }}..."
az storage account create \
--name "${{ parameters.storageAccountName }}" \
--location "${{ parameters.location }}" \
--resource-group "${{ parameters.resourceGroupName }}" \
--https-only \
--min-tls-version "TLS1_2" \
--allow-blob-public-access false \
--sku "Standard_LRS" \
--tags "$tags" \
--output "none"
echo "Creating Managed Identity for Function App ${{ parameters.functionAppName }} in Resource Group ${{ parameters.resourceGroupName }}..."
managedIdentityJson=$(az identity create --name "${{ parameters.functionAppName }}-identity" --resource-group "${{ parameters.resourceGroupName }}" --tags "$tags" --output "json")
managedIdentityName=$(echo $managedIdentityJson | jq -r '.name')
managedIdentityId=$(echo $managedIdentityJson | jq -r '.id')
managedIdentityClientId=$(echo $managedIdentityJson | jq -r '.clientId')
managedIdentityPrincipalId=$(echo $managedIdentityJson | jq -r '.principalId')
echo "Assigning Managed Identity $managedIdentityName permissions on Subscription ${{ parameters.subscriptionId }}..."
az role assignment create \
--assignee-object-id "$managedIdentityPrincipalId" \
--role ${{ parameters.managedIdentityRoleName }} \
--assignee-principal-type "ServicePrincipal" \
--scope "/subscriptions/${{ parameters.subscriptionId }}" \
--output "none"
echo "Creating Function App ${{ parameters.functionAppName }} in Resource Group ${{ parameters.resourceGroupName }}..."
az functionapp create \
--name "${{ parameters.functionAppName }}" \
--storage-account "${{ parameters.storageAccountName }}" \
--consumption-plan-location "${{ parameters.location }}" \
--resource-group "${{ parameters.resourceGroupName }}" \
--assign-identity "$managedIdentityId" \
--functions-version "4" \
--os-type "Linux" \
--runtime "python" \
--runtime-version "3.9" \
--tags "$tags" \
--output "none"
echo "Setting app settings on Function App ${{ parameters.functionAppName }}..."
values=("EXPIRY_DAYS=${{ parameters.expiryDays }}" "MANAGED_IDENTITY_CLIENT_ID=$managedIdentityClientId" "AZURE_SUBSCRIPTION_ID=${{ parameters.subscriptionId }}")
for value in "${values[@]}"; do
echo "Setting app setting $value on Function App ${{ parameters.functionAppName }}..."
az functionapp config appsettings set \
--name ${{ parameters.functionAppName }} \
--resource-group "${{ parameters.resourceGroupName }}" \
--settings "$value" \
--output "none"
done
- task: AzureCLI@2
displayName: Install requirements from requirements.txt
inputs:
azureSubscription: "${{ parameters.serviceConnection }}"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
python -m venv worker_venv
source worker_venv/bin/activate
pip install setuptools
pip install -r requirements.txt
- task: ArchiveFiles@2
displayName: "Prepare zip file for deployment"
inputs:
rootFolderOrFile: "$(System.DefaultWorkingDirectory)"
includeRootFolder: false
archiveFile: "build.zip"
- task: AzureCLI@2
displayName: Deploy zipped application to Azure Function
inputs:
azureSubscription: "${{ parameters.serviceConnection }}"
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "Deploying zipped application to Azure Function ${{ parameters.functionAppName }} in Resource Group ${{ parameters.resourceGroupName }}..."
az functionapp deployment source config-zip \
--name "${{ parameters.functionAppName }}" \
--resource-group "${{ parameters.resourceGroupName }}" \
--src build.zip \
--build-remote true