-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
179 lines (173 loc) · 5.67 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
174
175
176
177
178
179
#!/usr/bin/env groovy
@NonCPS
def render(template, bindings = [:]) {
return new groovy.text.SimpleTemplateEngine()
.createTemplate(template)
.make(bindings)
.toString()
}
@NonCPS
def verifyDeployment(app, namespace) {
return """
for i in `seq 20`; do
kubectl rollout status deployment '$app' \\
-n '$namespace' \\
--watch=false \\
| grep 'rolled out' > status && break
sleep 1
done
cat status | grep 'rolled out'
"""
}
def awsRegion
def dockerRegistry
def app = 'chucknorris'
def host
def tag
def version
def replicas
node('master') {
awsRegion = env.AWS_REGION
dockerRegistry = env.PRIVATE_REGISTRY
host = "${app}.app.${env.ROOT_DOMAIN_NAME}"
stage('Checkout') {
checkout scm
tag = sh(script: 'git rev-parse HEAD', returnStdout: true).take(6)
version = env.version ?: tag
replicas = env.replicas ?: 1
}
}
podTemplate(
inheritFrom: 'agilestacks',
label: 'chucknorris',
containers: [
containerTemplate(
name: 'java',
image: 'openjdk:8',
ttyEnabled: true,
command: 'cat'
),
containerTemplate(
name: 'aws',
image: 'agilestacks/aws',
ttyEnabled: true,
command: 'cat'
),
containerTemplate(
name: 'docker',
image: 'docker',
ttyEnabled: true,
command: 'cat'
),
containerTemplate(
name: 'kubectl',
image: 'agilestacks/kubectl',
ttyEnabled: true,
command: 'cat'
),
containerTemplate(
name: 'curl',
image: 'appropriate/curl',
ttyEnabled: true,
command: 'cat'
)
],
volumes: [
hostPathVolume(
hostPath: '/var/run/docker.sock',
mountPath: '/var/run/docker.sock'
)
]
) {
node('chucknorris') {
stage('Build') {
container('java') {
try {
sh './gradlew clean build allureReport'
} finally {
archiveArtifacts 'build/libs/chnorr-*.jar'
junit 'build/test-results/*.xml'
publishHTML([
reportDir: 'build/allure-report',
reportFiles: 'index.html',
reportName: 'Allure Report',
keepAll: true
])
}
}
}
stage('Publish') {
def dockerLogin
container('aws') {
dockerLogin = sh(
script: "aws ecr get-login --region $awsRegion",
returnStdout: true
).trim()
}
container('docker') {
sh """
$dockerLogin
docker build -t $dockerRegistry:latest -t $dockerRegistry:$tag .
docker push $dockerRegistry
"""
}
}
stage('Deploy: test') {
container('kubectl') {
def namespace = "$app-test"
def template = readFile 'deployment.yaml'
def deployment = render template, [
app: app,
namespace: namespace,
replicas: 1,
host: "test.$host",
dockerRegistry: dockerRegistry,
version: version,
tag: tag
]
writeFile file: "deployment.${namespace}.yaml", text: deployment
sh "kubectl apply -f ./deployment.${namespace}.yaml -n '$namespace'"
try {
sh verifyDeployment(app, namespace)
} catch (err) {
sh "kubectl rollout undo deployment '$app' -n '$namespace'"
throw err
}
}
}
stage('Test') {
container('curl') {
sh """
for i in `seq 20`; do
curl -sSf http://test.$host > response && break
sleep 10
done
cat response | grep Chuck
"""
}
}
stage('Deploy: prod') {
container('kubectl') {
def namespace = "$app-prod"
def template = readFile 'deployment.yaml'
def deployment = render template, [
app: app,
namespace: namespace,
replicas: replicas,
host: host,
dockerRegistry: dockerRegistry,
version: version,
tag: tag
]
writeFile file: "deployment.${namespace}.yaml", text: deployment
sh "kubectl apply -f ./deployment.${namespace}.yaml --namespace '$namespace'"
try {
sh verifyDeployment(app, namespace)
} catch (err) {
sh "kubectl rollout undo deployment '$app' -n '$namespace'"
throw err
}
}
}
}
}