forked from mdn/developer-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
159 lines (143 loc) · 4.02 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
def get_commit_tag() {
/*
* Return the 7-digit commit-hash of HEAD.
*/
return sh([returnStdout: true, script: "git rev-parse --short=7 HEAD"]).trim()
}
def notify_slack(Map args, credential_id='slack-hook-devportal-notifications') {
def command = "${env.WORKSPACE}/scripts/slack-notify.sh"
withCredentials([string(credentialsId: credential_id, variable: 'HOOK')]) {
for (arg in args) {
command += " --${arg.key} '${arg.value}'"
}
command += " --hook '${HOOK}'"
sh command
}
}
def deploy(config, environment, cluster) {
/*
* Start a rolling update of the K8s deployments.
*/
// Before we start the rolling update, ensure that we can pull an image
// tagged with the commit-hash of HEAD. We don't want Kubernetes trying
// to pull an image that doesn't exist.
sh "docker pull ${image}:${tag}"
// Configure the environment, perform any database migrations if necessary,
// start the rolling update, and finally, monitor until complete.
dir('k8s') {
try {
// Verify if k8s manifest is legit
notify_slack([
status: "Verifying kubernetes deployment manifest (cluster: ${cluster})",
message: "developer-portal commit ${tag}"
])
sh """
. config/${config}.sh
export APP_IMAGE_TAG=${tag}
make test-k8s-deployments
"""
notify_slack([
stage: "Verfied kubernetes deployment manifest (cluster: ${cluster})",
status: 'success'
])
notify_slack([
status: "Pushing to ${environment} (cluster: ${cluster})",
message: "developer-portal image ${tag}"
])
sh """
. config/${config}.sh
export APP_IMAGE_TAG=${tag}
make k8s-db-migration-job
make k8s-deployments
make k8s-search-index-update-job
make k8s-hpa
make k8s-rollout-status
"""
notify_slack([
stage: "Deployed to ${environment} (cluster: ${cluster})",
status: 'success'
])
} catch(err) {
notify_slack([
stage: "Failed to deploy to ${environment} (cluster: ${cluster})",
status: 'failure'
])
throw err
}
}
}
node {
stage('Prep') {
checkout scm
tag = get_commit_tag()
image = 'mdnwebdocs/developer-portal'
}
switch (env.BRANCH_NAME) {
case 'master':
stage ('Build Image') {
try {
sh "docker build --tag ${image}:latest --tag ${image}:${tag} ."
} catch(err) {
notify_slack([
stage: "Build of latest-tagged developer-portal image",
status: 'failure'
])
throw err
}
}
stage ('Test') {
// Set UID and GID
env['UID'] = sh(returnStdout: true, script: 'id -u jenkins').trim()
env['GID'] = sh(returnStdout: true, script: 'id -g jenkins').trim()
try {
sh "scripts/ci-setup --build"
sh "scripts/ci-tests"
notify_slack([
stage: "Running test on tag ${tag}",
status: 'success'
])
} catch (err) {
notify_slack([
stage: "Test run failed on tag ${tag}",
status: 'failure'
])
throw err
} finally {
sh "docker-compose down --volumes --remove-orphans"
}
}
stage('Push Images') {
sh "docker push ${image}:latest"
sh "docker push ${image}:${tag}"
}
break
case 'dev-push':
stage('Deploy') {
deploy('dev', 'dev', 'mdn-apps-a')
}
break
case 'stage-push':
stage('Deploy') {
deploy('stage', 'stage', 'mdn-apps-a')
}
break
case 'prod-push':
stage('Deploy') {
deploy('prod', 'prod', 'mdn-apps-a')
}
break
default:
stage ('Build Image') {
sh "docker build --tag ${image}:${tag} ."
}
stage('Push Image') {
sh "docker push ${image}:${tag}"
}
break
}
stage('Cleanup') {
cleanWs(
cleanWhenFailure: false
)
}
}