-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathazure-pipelines-cicd.yml
233 lines (221 loc) · 7.83 KB
/
azure-pipelines-cicd.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
variables:
# Azure Service Principal alias name
azureSubscription: 'nodejs-cicd'
# Web app name
webAppNameDev: 'nodejs-cicd-terra-app-dev'
webAppNameProd: 'nodejs-cicd-terra-app-prod'
# Environment name
environmentName: 'dev'
prodEnvironmentName: 'production'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages :
- stage: build
displayName: Build and unit test Node app
jobs:
- job: Build
displayName: Build Node app
pool:
vmImage: $(vmImageName)
steps:
- task: NodeTool@0
inputs:
versionSpec: '12.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build --if-present
npm run test --if-present
displayName: 'npm install, build and test'
- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testRunner: JUnit
testResultsFiles: '**/test-results.xml'
displayName: 'publish test results'
- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
- upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop
- stage: validate
displayName: Validate Terraform
jobs:
- job: validate_infra
displayName: Validate infra
continueOnError: false
steps:
- task: TerraformInstaller@0
displayName: 'install'
inputs:
terraformVersion: latest
- task: TerraformCLI@0
displayName: 'init'
inputs:
backendType: azurerm
command: init
backendServiceArm: $(azureSubscription)
backendAzureRmResourceGroupName: 'nodejs-cicd-rg'
backendAzureRmStorageAccountName: 'tfops'
backendAzureRmContainerName: 'tfstate'
backendAzureRmKey: tfstate.tfstate
- task: TerraformCLI@0
displayName: 'validate'
inputs:
backendType: 'azurerm'
command: 'validate'
- stage: provision_infra_dev
displayName: Provision Dev resources in Azure via Terraform
jobs:
- deployment: provision_infra
displayName: Provision infra
environment: $(environmentName)
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: TerraformInstaller@0
displayName: 'install'
inputs:
terraformVersion: latest
- task: TerraformCLI@0
displayName: 'init'
inputs:
backendType: azurerm
command: init
backendServiceArm: $(azureSubscription)
backendAzureRmResourceGroupName: 'nodejs-cicd-rg'
backendAzureRmStorageAccountName: 'tfops'
backendAzureRmContainerName: 'tfstate'
backendAzureRmKey: tfstate.tfstate
# - task: TerraformCLI@0
# displayName: create workspace dev
# continueOnError: 'true'
# inputs:
# command: workspace
# workspaceSubCommand: new
# workspaceName: dev
- task: TerraformCLI@0
displayName: select workspace dev
inputs:
command: workspace
workspaceSubCommand: select
workspaceName: dev
- task: TerraformCLI@0
displayName: 'plan'
inputs:
backendType: 'azurerm'
command: 'plan'
commandOptions: '-out=plan-dev -var-file=dev.tfvars'
environmentServiceName: $(azureSubscription)
publishPlanResults: 'terraform_plan'
- task: TerraformCLI@0
displayName: 'apply'
inputs:
backendType: 'azurerm'
command: 'apply'
commandOptions: 'plan-dev'
environmentServiceName: $(azureSubscription)
- stage: deploy_dev
displayName: Deploy Node app to Dev
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy Node app
environment: $(environmentName)
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureWebApp@1
displayName: 'Azure Web App Deploy: nodejs-cicd-terra-app-dev'
inputs:
azureSubscription: $(azureSubscription)
appType: webAppLinux
appName: $(webAppNameDev)
# runtimeStack: 'node|12-lts'
package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
startUpCommand: 'npm run start'
- stage: provision_infra_prod
displayName: Provision Prod resources in Azure via Terraform
jobs:
- deployment: provision_infra
displayName: Provision prod infra
continueOnError: false
environment: $(prodEnvironmentName)
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: TerraformInstaller@0
displayName: 'install'
inputs:
terraformVersion: latest
- task: TerraformCLI@0
displayName: 'init'
inputs:
backendType: azurerm
command: init
backendServiceArm: $(azureSubscription)
backendAzureRmResourceGroupName: 'nodejs-cicd-rg'
backendAzureRmStorageAccountName: 'tfops'
backendAzureRmContainerName: 'tfstate'
backendAzureRmKey: tfstate.tfstate
# - task: TerraformCLI@0
# displayName: create workspace prod
# continueOnError: 'true'
# inputs:
# command: workspace
# workspaceSubCommand: new
# workspaceName: prod
- task: TerraformCLI@0
displayName: select workspace prod
inputs:
command: workspace
workspaceSubCommand: select
workspaceName: prod
- task: TerraformCLI@0
displayName: 'plan'
inputs:
backendType: 'azurerm'
command: 'plan'
commandOptions: '-out=plan-prod -var-file=prod.tfvars'
environmentServiceName: $(azureSubscription)
publishPlanResults: 'terraform_plan'
- task: TerraformCLI@0
displayName: 'apply'
inputs:
backendType: 'azurerm'
command: 'apply'
commandOptions: 'plan-prod'
environmentServiceName: $(azureSubscription)
- stage: deploy_prod
displayName: Deploy Node app to Prod
jobs:
- deployment: Deploy
displayName: Deploy Node app
environment: $(prodEnvironmentName)
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureWebApp@1
displayName: 'Azure Web App Deploy: nodejs-cicd-terra-app'
inputs:
azureSubscription: $(azureSubscription)
appType: webAppLinux
appName: $(webAppNameProd)
# runtimeStack: 'node|12-lts'
package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
startUpCommand: 'npm run start'