-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
167 lines (166 loc) · 4.1 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
def stackOutputs
def podTemplate = """
apiVersion: v1
kind: Pod
metadata:
labels:
for-app: ${params.APPLICATION_NAME}
spec:
containers:
- name: toolbox
image: agilestacks/toolbox:stable
command:
- cat
tty: true
volumeMounts:
- name: hub-state
mountPath: /tmp/.hub/
- name: buildbox
image: openjdk:8-jdk
command:
- cat
tty: true
- name: kaniko
image: gcr.io/kaniko-project/executor:debug-539ddefcae3fd6b411a95982a830d987f4214251
imagePullPolicy: Always
command:
- /busybox/cat
tty: true
volumeMounts:
- name: jenkins-docker-cfg
mountPath: /root
volumes:
- name: jenkins-docker-cfg
projected:
sources:
- secret:
name: ${params.DOCKER_CONFIG_REF}
items:
- key: .dockerconfigjson
path: .docker/config.json
- name: hub-state
configMap:
name: ${params.APPLICATION_NAME}-state
"""
pipeline {
triggers {
githubPush()
pollSCM('H/15 * * * *')
}
parameters {
booleanParam(
name: 'CLEAN_WORKSPACE',
defaultValue: false,
description: 'Start with empty workspace'
)
}
agent {
kubernetes {
label params.APPLICATION_NAME
defaultContainer 'toolbox'
yaml podTemplate
}
}
stages {
stage('Init') {
steps {
script {
if (params.CLEAN_WORKSPACE) {
echo "Wiping out workspace"
deleteDir()
} else {
echo 'Skipping cleanup due to user setting'
}
}
}
}
stage('Checkout') {
steps {
checkout scm
}
}
stage('Prepare') {
steps {
container('toolbox') {
script {
mytimestamp = new Date().time
hub.render([template: "kubernetes.yaml.template", state: "/tmp/.hub/hub.state", additional: ['annotation.timestamp': mytimestamp]])
stackOutputs = hub.explain(state: "/tmp/.hub/hub.state").stackOutputs
}
}
}
}
stage('Compile') {
steps {
container('buildbox') {
sh script: './gradlew clean build allureReport'
}
}
}
stage('Image') {
steps {
container(name: 'kaniko', shell: '/busybox/sh') {
withEnv(['PATH+EXTRA=/busybox:/kaniko']) {
script {
sh """#!/busybox/sh
/kaniko/executor -f `pwd`/Dockerfile -c `pwd` --cache=true --destination=${stackOutputs['application.docker.image']}
"""
}
}
}
}
}
stage('Deploy') {
environment {
HUB_COMPONENT = 'spring-boot'
}
steps {
container('toolbox') {
script {
sh script: "hub kubeconfig /tmp/.hub/hub.state --switch-kube-context"
final namespaceExists = sh script: "kubectl get namespace ${stackOutputs['application.namespace']}", returnStatus: true
if (namespaceExists != 0) {
sh script: "kubectl create namespace ${stackOutputs['application.namespace']}"
}
final kubectl = "kubectl -n ${stackOutputs['application.namespace']}"
sh script: "${kubectl} apply --force --record -f kubernetes.yaml"
}
}
}
}
stage('Validate') {
steps {
container('toolbox') {
script {
final appUrl = stackOutputs['application.url'] as String
retry(30) {
sleep time: 5, unit: 'SECONDS'
final resp = httpRequest url: "${appUrl}"
echo resp.content
assert resp.status == 200
}
}
}
}
}
}
post {
always {
junit(
testResults: './tests-*.xml',
allowEmptyResults: true
)
archiveArtifacts(
artifacts: 'build/libs/*.jar',
allowEmptyArchive: true,
fingerprint: true
)
publishHTML(target: [
reportDir: 'build/reports/allure-report',
reportFiles: 'index.html',
reportName: 'Allure',
keepAll: true
])
}
}
}