-
Notifications
You must be signed in to change notification settings - Fork 292
/
Jenkinsfile
173 lines (161 loc) Β· 6.99 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
167
168
169
170
171
172
173
@NonCPS
def checkWorkflowRun(Map run, String commitHash) {
final String headSha = run['head_sha']
if (headSha == commitHash) {
echo("Found hash ${headSha}")
final String conclusion = run['conclusion']
echo("conclusion: ${conclusion}")
switch (conclusion) {
case 'success':
return true
case 'failure':
final String url = run['url']
error("β **Build failed for branch '${GIT_BRANCH_WEBAPP}'** See [Github Actions](${url})")
break
case 'cancelled':
final String url = run['url']
error("β οΈ **Build aborted for branch '${GIT_BRANCH_WEBAPP}'** See [Github Actions](${url})")
break
}
}
return false
}
pipeline {
agent {
node {
label 'built-in'
}
}
options { disableConcurrentBuilds(abortPrevious: true) }
environment {
CREDENTIALS = credentials('GITHUB_TOKEN_WEB')
WIRE_BOT_SECRET = credentials('JENKINSBOT_WEBAPP_DEV')
webappApplicationPath = 'https://wire-webapp-precommit.zinfra.io/'
}
stages {
stage('Wait for GitHub action to finish') {
when {
expression { BRANCH_NAME ==~ /PR-[0-9]+/ }
}
steps {
script {
def commit_hash = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
def pr_number = BRANCH_NAME.replaceAll(/\D/, '')
def changeTargetBranch = env.CHANGE_TARGET
def targetWorkflowUrl
switch (changeTargetBranch) {
case ['dev']:
targetWorkflowUrl = 'https://api.github.com/repos/wireapp/wire-webapp/actions/workflows/128602012/runs'
break
default:
targetWorkflowUrl = 'https://api.github.com/repos/wireapp/wire-webapp/actions/workflows/128602012/runs'
break
}
echo("Wait for github actions to start for ${BRANCH_NAME} against ${changeTargetBranch}")
final def VALID_STATUSES = ['queued', 'in_progress', 'completed']
timeout(time: 45, unit: 'MINUTES') {
waitUntil {
def output = sh label: 'Get runs', returnStdout: true, script: "curl -s -L -H 'Accept: application/vnd.github+json' -H 'Authorization: Bearer ${CREDENTIALS}' -H 'X-GitHub-Api-Version: 2022-11-28' ${targetWorkflowUrl}"
def json = readJSON text: output
if (json['message']) {
echo('Output: ' + output)
error('**Trigger script failed:** ' + json['message'])
}
def runs = json['workflow_runs']
echo('Looking for PR-' + pr_number + ' with hash' + commit_hash)
def matchingRun = runs.find { it['head_sha'] == commit_hash }
if (matchingRun) {
echo('Found ' + commit_hash)
def status = matchingRun['status']
echo('status: ' + status)
env.GITHUB_ACTION_URL = matchingRun['url'].replace('api.github.com/repos', 'github.com/')
return VALID_STATUSES.contains(status)
}
false
}
sleep(20)
}
}
}
}
stage('Check GitHub Action Status') {
when { expression { BRANCH_NAME ==~ /PR-[0-9]+/ } }
steps {
timeout(time: 15, unit: 'MINUTES') {
script {
def commit_hash = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
final String apiUrl = 'https://api.github.com/repos/wireapp/wire-webapp/actions/workflows/128602012/runs'
final String curlCmd = "curl -u \${CREDENTIALS} ${apiUrl}"
waitUntil {
final String output = sh(label: 'Check workflow', returnStdout: true, script: curlCmd)
final Object jsonData = readJSON(text: output)
final List workflowRuns = jsonData['workflow_runs']
echo("Looking for hash ${commit_hash}")
return workflowRuns.any { run ->
def result = checkWorkflowRun(run, commit_hash)
if (run['conclusion'] == 'cancelled') {
echo("GitHub Action was cancelled. Ending Jenkins pipeline.")
return true
}
return result
}
}
}
}
}
}
stage('Check deployment') {
steps {
script {
def commit_hash = sh(script: 'git rev-parse HEAD', returnStdout: true).trim()
String commitMsg = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
try {
// Wait until deployment has finished (20 retries * 30 seconds == 10 minutes)
timeout(time: 10, unit: 'MINUTES') {
waitUntil {
def randomid = sh returnStdout: true, script: 'uuidgen'
randomid = randomid.trim()
def current_hash = sh returnStdout: true, script: "curl '${webappApplicationPath}commit?v=${randomid}'"
current_hash = current_hash.trim()
echo('Current version is: ' + current_hash)
if (current_hash == commit_hash) {
echo('Deployment finished.')
return true
}
env.MESSAGE = 'Current hash still is ' + current_hash + ' and not ' + commit_hash
sh "echo '${MESSAGE}' > deployment.log"
sleep(30)
return false
}
}
} catch (e) {
def reason = sh returnStdout: true, script: 'cat deployment.log || echo ""'
String errorMessage = """β **Deployment failed on** ${webappApplicationPath}
${commitMsg}
**Reason:** ${e}
${reason}"""
wireSend secret: env.WIRE_BOT_SECRET, message: errorMessage
}
def successMessage = """β
**Deployment successful on** ${webappApplicationPath}
${commitMsg}"""
wireSend secret: env.WIRE_BOT_SECRET, message: successMessage
}
}
}
stage('Trigger smoke test') {
steps {
build job: 'Webapp_Smoke_Chrome', parameters: [string(name: 'TAGS', value: '@smoke'), string(name: 'GIT_BRANCH', value: 'web-dev'), string(name: 'webappApplicationPath', value: "$webappApplicationPath")], wait: false
}
}
}
post {
success {
wireSend secret: env.WIRE_BOT_SECRET, message: "β
**Build finished for branch '$env.webappApplicationPath'**"
}
failure {
script {
wireSend(secret: env.WIRE_BOT_SECRET, message: "β **$BRANCH_NAME**\n[$CHANGE_TITLE](${CHANGE_URL})\nBuild aborted or failed! See [Github Actions](${env.GITHUB_ACTION_URL})")
}
}
}
}