-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathazure-pipelines.yml
199 lines (188 loc) · 9.78 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
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
# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
# Trigger pipeline on commits to main branch
- main
variables:
# Ubuntu-latest image is used for building and deploying the app
vmImageName: 'ubuntu-latest'
stages:
- stage: Build_Test
displayName: Build & Test project
jobs:
- job: Build # Build the app and run tests
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: NodeTool@0
inputs:
versionSpec: '16.x'
displayName: 'Install Node.js'
- task: Npm@1
displayName: Install
inputs:
command: 'install'
workingDir: 'src'
verbose: true
- task: Npm@1
displayName: Test
inputs:
workingDir: 'src'
command: custom
verbose: false
customCommand: test
- task: reportgenerator@5
condition: succeededOrFailed()
displayName: Converting lcov to cobertura
inputs:
reports: 'src/coverage/lcov.info'
targetdir: 'coverage/'
reporttypes: 'Cobertura'
- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: 'coverage/Cobertura.xml'
- task: Docker@2 # Build and push image to Docker Hub
inputs:
containerRegistry: 'dockerRegistryServiceConnection'
repository: 'nkiboi/aca-example'
command: 'buildAndPush'
Dockerfile: '**/Dockerfile'
- stage: Deploy
displayName: Deploy the app to non-production environment
dependsOn: Build_Test
jobs:
- job: Deploy # Deploy the app to non-production environment - in this case what we call the blue environment
displayName: Deploy to Non_production environment
pool:
vmImage: $(vmImageName)
steps:
- task: AzureCLI@2
displayName: Set Production Label, Blue Commit and Green Commit
name: setvarStep1
inputs:
azureSubscription: 'azureSubscription'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
#!/bin/sh
## Get the current tags from the container app
tags=$(az containerapp show -g $RESOURCE_GROUP -n $API_NAME --query tags -o json | tr -d '\r\n')
## Get the current production label from the tags
productionLabel=$(echo $tags | jq -r '.productionLabel')
if [ "$productionLabel" = "" ]; then
productionLabel='green'
echo "The Production Label has been set to: $productionLabel"
else
echo "Current Production Label: $productionLabel"
fi
## Get the current blue and green commit IDs from the tags
current_blueCommitID=$(echo $tags | jq -r '.blueCommitID')
current_greenCommitID=$(echo $tags | jq -r '.greenCommitID')
echo "Current Production Label: $productionLabel"
echo $productionLabel
echo "Blue Commit ID: $current_blueCommitID"
echo " Green Commit ID: $current_greenCommitID"
## Set variables for using in other tasks
echo "##vso[task.setvariable variable=productionLabel;isOutput=true]$productionLabel" ## Set variable for using in other tasks.
echo "##vso[task.setvariable variable=current_blueCommitID;isOutput=true]$current_blueCommitID" ## Set variable for using in other tasks.
echo "##vso[task.setvariable variable=current_greenCommitID;isOutput=true]$current_greenCommitID" ## Set variable for using in other tasks.
- script: |
## Get the latest commit ID from the source code
echo $sourceVersion
commitHash=${sourceVersion:0:7}
echo $commitHash
echo "##vso[task.setvariable variable=commitHash;isOutput=true]$commitHash" ## Set variable for using in other tasks
## If the production label is empty, then set it to blue and set the blue commit to latest commit ID and green commit to empty
if [[ productionLabel == '' ]];
then
echo "##vso[task.setvariable variable=productionLabel;isOutput=truel;]blue" ## Set variable for using in other tasks.
echo "##vso[task.setvariable variable=blueCommitID;isOutput=true]$commitHash" ## Set variable for using in other tasks.
echo "$commitHash"
echo "##vso[task.setvariable variable=greenCommitID;isOutput=true]" ## Set variable for using in other tasks.
exit 0
else
## If the production label is blue, then set the blue commit to the current blueCommitID otherwise set it to the latest commit
blueCommitID=$([[ $productionLabel = 'blue' ]] && echo "$current_blueCommitID" || echo "$commitHash")
echo "New blue Commit ID: $blueCommitID"
## If the production label is green, then set the green commit to the current greenCommitID otherwise set it to the latest commit
greenCommitID=$([[ $productionLabel = 'green' ]] && echo "$current_greenCommitID" || echo "$commitHash")
echo "New green Commit ID: $greenCommitID"
echo "##vso[task.setvariable variable=productionLabel;isOutput=true]$productionLabel" ## Set variable for using in other tasks.
echo "##vso[task.setvariable variable=blueCommitID;isOutput=true]$blueCommitID" ## Set variable for using in other tasks.
echo "##vso[task.setvariable variable=greenCommitID;isOutput=true]$greenCommitID" ## Set variable for using in other tasks.
fi
name: setvarStep
env: { sourceVersion: $(Build.SourceVersion), productionLabel: $(setvarStep1.productionLabel), current_blueCommitID: $(setvarStep1.current_blueCommitID), current_greenCommitID: $(setvarStep1.current_greenCommitID) }
displayName: 'Set new production label and Blue Commits and Green Commits '
- task: AzureResourceManagerTemplateDeployment@3
displayName: Deploy the container app to new environment and send 0% traffic to it
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: 'azureSubscription'
subscriptionId: '$(SUBSCRIPTIONID)'
action: 'Create Or Update Resource Group'
resourceGroupName: '$(RESOURCE_GROUP)'
location: '$(LOCATION)'
templateLocation: 'Linked artifact'
csmFile: 'main.bicep'
overrideParameters: '-appName $(API_NAME) -containerAppsEnvironmentName $(ENVIRONMENT) -registryUsername $(REGISTRY_USERNAME) -registryPassword $(REGISTRY_PASSWORD) -productionLabel $(setvarStep.productionLabel) -latestCommitId $(setvarStep.commitHash) -buildID $(Build.BuildId) -blueCommitID $(setvarStep.blueCommitID) -greenCommitID $(setvarStep.greenCommitID)'
deploymentMode: 'Incremental'
deploymentName: 'DeployPipelineTemplate'
- stage: WaitForValidation
displayName: Validate deployed application is running successfully in the new environment
dependsOn: Deploy
jobs:
- job: waitForValidation
displayName: Validate deployed application is running successfully in the new environment
pool: server
timeoutInMinutes: 4320 # job times out in 3 days
steps:
- task: ManualValidation@0
timeoutInMinutes: 1440 # task times out in 1 day
inputs:
notifyUsers: '$(NOTIFICATIONEMAIL)'
instructions: 'Please validate the deployment before moving to production'
onTimeout: 'resume'
- stage: Deploy_To_Production
displayName: Deploy the application to production and make it active
dependsOn:
- waitForValidation
- Deploy
jobs:
- job: deployToProduction
variables:
myVarproductionLabel: $[ stageDependencies.Deploy.Deploy.outputs['setvarStep.productionLabel'] ]
myVarcommitHash: $[ stageDependencies.Deploy.Deploy.outputs['setvarStep.commitHash'] ]
myVarblueCommitID: $[ stageDependencies.Deploy.Deploy.outputs['setvarStep.blueCommitID'] ]
myVargreenCommitID: $[ stageDependencies.Deploy.Deploy.outputs['setvarStep.greenCommitID'] ]
displayName: Deploy to production and send 100% traffic to new environement
pool:
vmImage: $(vmImageName)
steps:
- script: |
newProductionLabel=$([[ $(myVarproductionLabel) == 'blue' ]] && echo 'green' || echo 'blue')
echo "$newProductionLabel"
echo "##vso[task.setvariable variable=newProductionLabel]$newProductionLabel"
echo " Old production label: $(myVarproductionLabel)"
echo "Blue Commit ID: $(myVarblueCommitID)"
echo " Green Commit ID: $(myVargreenCommitID)"
echo " Latest Commit: $(myVarcommitHash)"
echo " New production label: $newProductionLabel"
displayName: 'Set Variables for new environment'
- task: AzureResourceManagerTemplateDeployment@3 # Switch traffic to new environment and send 100% traffic to it
displayName: Switch traffic to 100% to new environment
inputs:
deploymentScope: 'Resource Group'
azureResourceManagerConnection: 'azureSubscription'
subscriptionId: '$(SUBSCRIPTIONID)'
action: 'Create Or Update Resource Group'
resourceGroupName: '$(RESOURCE_GROUP)'
location: '$(LOCATION)'
templateLocation: 'Linked artifact'
csmFile: 'main.bicep'
overrideParameters: '-appName $(API_NAME) -containerAppsEnvironmentName $(ENVIRONMENT) -registryUsername $(REGISTRY_USERNAME) -registryPassword $(REGISTRY_PASSWORD) -productionLabel $(newProductionLabel) -latestCommitId $(myVarcommitHash) -buildID $(Build.BuildId) -blueCommitID $(myVarblueCommitID) -greenCommitID $(myVargreenCommitID)'
deploymentMode: 'Incremental'
deploymentName: 'DeployPipelineTemplate'