-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
128 lines (128 loc) · 3.55 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
pipeline {
triggers {
githubPush()
pollSCM('H/15 * * * *')
}
parameters {
booleanParam(
name: 'CLEAN_WORKSPACE',
defaultValue: false,
description: 'Start with empty workspace'
)
}
agent {
kubernetes {
inheritFrom 'toolbox'
label 'pod'
containerTemplate(
name: 'buildbox',
image: 'openjdk:8-jdk',
ttyEnabled: true,
command: 'cat'
)
}
}
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('Compile') {
steps {
container('buildbox') {
sh script: './gradlew clean build allureReport'
}
stash includes: "**/*", name: 'workspace'
}
}
stage('Build and Push') {
steps {
container('toolbox') {
unstash("workspace")
script {
final image = hub.explain(state: params.APP_STATE_FILE).stackOutputs['application.docker.image'] as String
sh script: "docker build --pull --rm -t ${image}:latest -t ${image}:${gitscm.shortCommit} ."
withAWS(role:params.AWS_TRUST_ROLE) {
sh script: ecrLogin()
sh script: "docker push ${image}:${gitscm.shortCommit}"
sh script: "docker push ${image}:latest"
}
}
}
}
}
stage('Deploy') {
environment {
HUB_COMPONENT = 'spring-boot'
}
steps {
container('toolbox') {
unstash("workspace")
script {
sh script: "hub kubeconfig ${params.APP_STATE_FILE} --switch-kube-context"
final version = "${gitscm.shortCommit}"
final outputs = hub.explain(state: params.APP_STATE_FILE).stackOutputs
final namespaceExists = sh script: "kubectl get namespace ${outputs['application.namespace']}", returnStatus: true
if (namespaceExists != 0) {
sh script: "kubectl create namespace ${outputs['application.namespace']}"
}
final kubectl = "kubectl -n ${outputs['application.namespace']}"
hub.render(template: 'kubernetes.yaml.template', state: params.APP_STATE_FILE)
echo readFile('kubernetes.yaml')
sh script: "${kubectl} apply --force --record -f kubernetes.yaml"
}
}
}
}
stage('Validate') {
steps {
container('toolbox') {
script {
final appUrl = hub.explain(state: params.APP_STATE_FILE).stackOutputs['application.url'] as String
retry(30) {
sleep time: 3, 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
])
}
changed {
slackSend color: slack.buildColor, message: slack.buildReport(htmlReports: ['Allure'])
}
}
}