-
Notifications
You must be signed in to change notification settings - Fork 0
/
working.yml
264 lines (233 loc) · 8.69 KB
/
working.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
trigger: master
stages:
- stage: CI
jobs:
- job: BuildAndTest
pool:
name: 'nevotek-agent'
demands:
- agent.name -equals nevotek-agent-001
steps:
- task: NodeTool@0
inputs:
versionSource: 'spec'
versionSpec: '22.2.0'
- script: |
npm install
npm run lint
npm run test
npm run test:integration
displayName: 'Install, lint, and test'
- script: npm run build
displayName: 'Build application'
- task: Docker@2
inputs:
containerRegistry: 'acr-nonprod1927'
repository: 'myrepo'
command: 'buildAndPush'
Dockerfile: '**/Dockerfile'
tags: |
$(Build.BuildId)
latest
- stage: DeployToStaging
jobs:
- deployment: DeployToStaging
pool:
name: 'nevotek-agent'
demands:
- agent.name -equals nevotek-agent-001
environment: Staging
strategy:
runOnce:
deploy:
steps:
- task: AzureWebAppContainer@1
inputs:
azureSubscription: 'myconnection'
slotName: 'development'
appName: 'nevotekapp'
containers: 'nonprod1927.azurecr.io/myrepo:$(Build.BuildId)'
- stage: Tests
jobs:
- job: SiteStatusCheck
pool:
name: 'nevotek-agent'
demands:
- agent.name -equals nevotek-agent-001
steps:
- script: |
echo "Checking site status..."
RESPONSE=$(curl --max-time 100 -o /dev/null -s -w "%{http_code}\n" https://nevotekapp-development.azurewebsites.net/)
if [ $RESPONSE -ne 200 ]; then
echo "Site is not reachable, returned status: $RESPONSE"
exit 1
else
echo "Site is up and running with status: $RESPONSE"
fi
displayName: 'Site Status Check (200 OK)'
continueOnError: true
- stage: NotifyTester
jobs:
- job: CreateAndNotifyJiraIssue
pool:
name: 'nevotek-agent'
demands:
- agent.name -equals nevotek-agent-001
steps:
- script: |
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status.
# Variables
jiraUrl="https://nevotektask001.atlassian.net/rest/api/2/issue"
jiraUser="pwxcv7352@gmail.com"
encodedCreds="cHd4Y3Y3MzUyQGdtYWlsLmNvbTpBVEFUVDN4RmZHRjBXSGNGOHBuZE9uWENRVDFuT3IybHk3N1ZmTkFJZnRmWmo1UXUzSFlvRkY3ZUV4R19WSmlubUVKOW45cHk2YlNyTWxkeUhrMWxZdWVJUkE4cXg1eTRpLVR1ZEtCT3lWTEJZWlZBRjdueDcyd2xyeDJDX1lPSURiN3dmeklQbl8wOXlwZ0hfV3R1X1ZUeHRhTWc5dVAzbTZxN2l3cllxZkxyR2RfRF9hZ1MxbDQ9OEVDN0E0RTY="
# Create Issue JSON
createIssueBody=$(cat <<EOF
{
"fields": {
"project": {
"key": "NP"
},
"summary": "Deployment Ready for Testing",
"description": "The deployment of the development image is complete and is ready for testing. The application is accessible at https://nevotekapp-development.azurewebsites.net/. Please approve or reject the test cases.",
"issuetype": {
"name": "Task"
}
}
}
EOF
)
# Create Jira Issue
response=$(curl -s -w "\n%{http_code}" -X POST "$jiraUrl" \
-H "Content-Type: application/json" \
-H "Authorization: Basic $encodedCreds" \
-d "$createIssueBody")
# Separate response body from HTTP status code
httpCode=$(echo "$response" | tail -n1)
responseBody=$(echo "$response" | head -n-1)
# Check HTTP response code
if [ "$httpCode" -ne 201 ]; then
echo "Failed to create Jira issue. HTTP response code: $httpCode"
echo "Response body: $responseBody"
exit 1
fi
# Extract Issue Key
issueKey=$(echo "$responseBody" | jq -r '.key')
if [ -z "$issueKey" ] || [ "$issueKey" == "null" ]; then
echo "Failed to extract Jira issue key. Response: $responseBody"
exit 1
fi
echo "##vso[task.setvariable variable=JIRA_ISSUE_KEY;isoutput=true]$issueKey"
echo "Jira issue created with key: $issueKey"
displayName: 'Create Jira Issue'
name: createJiraIssue
failOnStderr: false
- script: |
#!/bin/bash
set -e
handle_error() {
echo "Error occurred in script at line: ${1}."
echo "Line exited with status: ${2}"
}
trap 'handle_error ${LINENO} $?' ERR
# Ensure JIRA_ISSUE_KEY is set
if [ -z "${JIRA_ISSUE_KEY}" ]; then
echo "Error: JIRA_ISSUE_KEY is not set"
exit 1
fi
jiraUrl="https://nevotektask001.atlassian.net/rest/api/2/issue/${JIRA_ISSUE_KEY}"
encodedCreds="cHd4Y3Y3MzUyQGdtYWlsLmNvbTpBVEFUVDN4RmZHRjBXSGNGOHBuZE9uWENRVDFuT3IybHk3N1ZmTkFJZnRmWmo1UXUzSFlvRkY3ZUV4R19WSmlubUVKOW45cHk2YlNyTWxkeUhrMWxZdWVJUkE4cXg1eTRpLVR1ZEtCT3lWTEJZWlZBRjdueDcyd2xyeDJDX1lPSURiN3dmeklQbl8wOXlwZ0hfV3R1X1ZUeHRhTWc5dVAzbTZxN2l3cllxZkxyR2RfRF9hZ1MxbDQ9OEVDN0E0RTY="
approved=false
maxAttempts=60
attempt=0
# Ensure jq is available
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed and this script doesn't have permission to install it."
echo "Please install jq or use a build agent with jq pre-installed."
exit 1
fi
while [ "$approved" = false ] && [ $attempt -lt $maxAttempts ]; do
response=$(curl -s -H "Authorization: Basic $encodedCreds" -H "Content-Type: application/json" "$jiraUrl")
if [ -z "$response" ]; then
echo "Error: Empty response from JIRA API"
exit 1
fi
status=$(echo "$response" | jq -r '.fields.status.name')
if [ -z "$status" ] || [ "$status" = "null" ]; then
echo "Error: Unable to parse status from JIRA response"
echo "Response: $response"
exit 1
fi
case "$status" in
"Done")
approved=true
echo "Tester has approved the test cases. Proceeding to production."
;;
"Rejected")
echo "Tester has rejected the test cases. Pipeline will be stopped."
exit 1
;;
"To Do")
attempt=$((attempt+1))
echo "Issue is still in 'To Do' status. Waiting for tester action. Attempt $attempt of $maxAttempts."
sleep 300
;;
*)
attempt=$((attempt+1))
echo "Unexpected status: $status. Waiting for tester approval. Attempt $attempt of $maxAttempts."
sleep 300
;;
esac
done
if [ "$approved" = false ]; then
echo "Tester approval timed out. Pipeline will be stopped."
exit 1
fi
displayName: 'Wait for Tester Approval'
env:
JIRA_ISSUE_KEY: $(createJiraIssue.JIRA_ISSUE_KEY)
- stage: ApproveProduction
jobs:
- deployment: ApproveProduction
pool: server
environment: Production
strategy:
runOnce:
deploy:
steps:
- task: ManualValidation@0
timeoutInMinutes: 1440 # 1 day
inputs:
notifyUsers: 'pwxcv7352@gmail.com'
instructions: 'Please validate the staging deployment and approve if ready for production.'
- stage: DeployToProduction
condition: succeeded('ApproveProduction')
jobs:
- job: CopyAndDeploy
pool:
name: 'nevotek-agent'
demands:
- agent.name -equals nevotek-agent-001
steps:
# Step 1: Copy the Docker image from dev ACR to prod ACR
- script: |
echo "Logging in to non-prod ACR..."
az acr login --name nonprod1927
echo "Logging in to prod ACR..."
az acr login --name prod1927
echo "Tagging image for production ACR..."
docker tag nonprod1927.azurecr.io/myrepo:$(Build.BuildId) prod1927.azurecr.io/myrepo:$(Build.BuildId)
echo "Pushing image to production ACR..."
docker push prod1927.azurecr.io/myrepo:$(Build.BuildId)
displayName: 'Copy Image from Dev ACR to Prod ACR'
env:
DOCKER_CLI_EXPERIMENTAL: 'enabled' # Enable experimental features if needed
# Step 4: Swap the development slot with production
- task: AzureAppServiceManage@0
inputs:
azureSubscription: 'myconnection'
Action: 'Swap Slots'
WebAppName: 'nevotekapp'
ResourceGroupName: 'myResourceGroup'
SourceSlot: 'development'
displayName: 'Swap Deployment Slots'