-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsfile
166 lines (156 loc) · 6.44 KB
/
jenkinsfile
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
pipeline {
agent any
environment {
DOCKER_CLI_EXPERIMENTAL = 'enabled'
JIRA_URL = 'https://nevotektask001.atlassian.net/rest/api/2/issue'
JIRA_USER = 'pwxcv7352@gmail.com'
JIRA_CREDS = 'cHd4Y3Y3MzUyQGdtYWlsLmNvbTpBVEFUVDN4RmZHRjBXSGNGOHBuZE9uWENRVDFuT3IybHk3N1ZmTkFJZnRmWmo1UXUzSFlvRkY3ZUV4R19WSmlubUVKOW45cHk2YlNyTWxkeUhrMWxZdWVJUkE4cXg1eTRpLVR1ZEtCT3lWTEJZWlZBRjdueDcyd2xyeDJDX1lPSURiN3dmeklQbl8wOXlwZ0hfV3R1X1ZUeHRhTWc5dVAzbTZxN2l3cllxZkxyR2RfRF9hZ1MxbDQ9OEVDN0E0RTY='
}
tools {
nodejs 'NodeJS 22.2.0' // This should match the name you provided in Global Tool Configuration
}
stages {
stage('Checkout Code') {
checkout([
$class: 'GitSCM',
branches: [[name: 'main']],
userRemoteConfigs: [[url: 'https://github.com/tmp-AtakanG7/full-ci-cd-nevotek']]
])
}
stage('Install, Lint, and Test') {
steps {
sh '''
npm install
npm run lint
npm run test
npm run test:integration
'''
}
}
stage('Build Application') {
steps {
sh 'npm run build'
}
}
stage('Build and Push Docker Image') {
steps {
script {
docker.withRegistry('https://nonprod1927.azurecr.io', 'acr-nonprod1927-creds') {
def app = docker.build("myrepo:${env.BUILD_ID}")
app.push("${env.BUILD_ID}")
app.push("latest")
}
}
}
}
stage('Deploy to Staging') {
steps {
script {
azureWebApp appName: 'nevotekapp',
resourceGroup: 'myResourceGroup',
azureCredentialsId: 'azure-credentials-id',
deployType: 'docker',
containerRegistryUrl: 'https://nonprod1927.azurecr.io',
containerName: "myrepo:${env.BUILD_ID}",
slotName: 'development'
}
}
}
stage('Site Status Check') {
steps {
script {
def response = sh(script: 'curl --max-time 100 -o /dev/null -s -w "%{http_code}\n" https://nevotekapp-development.azurewebsites.net/', returnStdout: true).trim()
if (response != '200') {
error "Site is not reachable, returned status: ${response}"
} else {
echo "Site is up and running with status: ${response}"
}
}
}
}
stage('Create Jira Issue and Wait for Approval') {
steps {
script {
def issueKey = createJiraIssue()
waitForTesterApproval(issueKey)
}
}
}
stage('Approve and Deploy to Production') {
steps {
input message: 'Approve deployment to Production?', submitter: 'pwxcv7352@gmail.com'
script {
deployToProduction()
}
}
}
}
}
def createJiraIssue() {
def createIssueBody = '''{
"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" }
}
}'''
def response = sh(script: "curl -s -w '\n%{http_code}' -X POST ${JIRA_URL} -u ${JIRA_USER}:${JIRA_CREDS} -H 'Content-Type: application/json' -d '${createIssueBody}'", returnStdout: true)
def httpCode = response.split('\n').last().trim()
def responseBody = response.split('\n')[0]
if (httpCode != '201') {
error "Failed to create Jira issue. HTTP response code: ${httpCode}\nResponse body: ${responseBody}"
}
def issueKey = sh(script: "echo '${responseBody}' | jq -r '.key'", returnStdout: true).trim()
if (!issueKey || issueKey == 'null') {
error "Failed to extract Jira issue key."
}
echo "Jira issue created with key: ${issueKey}"
return issueKey
}
def waitForTesterApproval(issueKey) {
def maxAttempts = 60
def attempt = 0
def approved = false
while (!approved && attempt < maxAttempts) {
def response = sh(script: "curl -s -u ${JIRA_USER}:${JIRA_CREDS} -H 'Content-Type: application/json' ${JIRA_URL}/${issueKey}", returnStdout: true)
def status = sh(script: "echo '${response}' | jq -r '.fields.status.name'", returnStdout: true).trim()
switch (status) {
case 'Done':
approved = true
echo "Tester has approved the test cases. Proceeding to production."
break
case 'Rejected':
error "Tester has rejected the test cases. Pipeline will be stopped."
case 'To Do':
echo "Issue is still in 'To Do' status. Waiting for tester action. Attempt ${attempt + 1} of ${maxAttempts}."
sleep 300
attempt++
break
default:
echo "Unexpected status: ${status}. Waiting for tester approval. Attempt ${attempt + 1} of ${maxAttempts}."
sleep 300
attempt++
break
}
}
if (!approved) {
error "Tester approval timed out. Pipeline will be stopped."
}
}
def deployToProduction() {
withAzureServicePrincipal('azure-credentials-id') {
sh '''
echo "Copying Docker image from dev ACR to prod ACR..."
az acr login --name nonprod1927
az acr login --name prod1927
docker tag nonprod1927.azurecr.io/myrepo:${env.BUILD_ID} prod1927.azurecr.io/myrepo:${env.BUILD_ID}
docker push prod1927.azurecr.io/myrepo:${env.BUILD_ID}
'''
azureWebApp appName: 'nevotekapp',
resourceGroup: 'myResourceGroup',
azureCredentialsId: 'azure-credentials-id',
action: 'swap',
sourceSlot: 'development'
}
}