-
Notifications
You must be signed in to change notification settings - Fork 359
/
Jenkinsfile
192 lines (157 loc) · 6.2 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
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/groovy
// load pipeline functions
// Requires pipeline-github-lib plugin to load library from github
@Library('github.com/lachie83/jenkins-pipeline@dev')
def pipeline = new io.estrado.Pipeline()
podTemplate(label: 'jenkins-pipeline', containers: [
containerTemplate(name: 'jnlp', image: 'lachlanevenson/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}', workingDir: '/home/jenkins', resourceRequestCpu: '200m', resourceLimitCpu: '300m', resourceRequestMemory: '256Mi', resourceLimitMemory: '512Mi'),
containerTemplate(name: 'docker', image: 'docker:1.12.6', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'golang', image: 'golang:1.8.3', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'helm', image: 'lachlanevenson/k8s-helm:v2.6.0', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.4.8', command: 'cat', ttyEnabled: true)
],
volumes:[
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
]){
node ('jenkins-pipeline') {
def pwd = pwd()
def chart_dir = "${pwd}/charts/croc-hunter"
checkout scm
// read in required jenkins workflow config values
def inputFile = readFile('Jenkinsfile.json')
def config = new groovy.json.JsonSlurperClassic().parseText(inputFile)
println "pipeline config ==> ${config}"
// continue only if pipeline enabled
if (!config.pipeline.enabled) {
println "pipeline disabled"
return
}
// set additional git envvars for image tagging
pipeline.gitEnvVars()
// If pipeline debugging enabled
if (config.pipeline.debug) {
println "DEBUG ENABLED"
sh "env | sort"
println "Runing kubectl/helm tests"
container('kubectl') {
pipeline.kubectlTest()
}
container('helm') {
pipeline.helmConfig()
}
}
def acct = pipeline.getContainerRepoAcct(config)
// tag image with version, and branch-commit_id
def image_tags_map = pipeline.getContainerTags(config)
// compile tag list
def image_tags_list = pipeline.getMapValues(image_tags_map)
stage ('compile and test') {
container('golang') {
sh "go test -v -race ./..."
sh "make bootstrap build"
}
}
stage ('test deployment') {
container('helm') {
// run helm chart linter
pipeline.helmLint(chart_dir)
// run dry-run helm chart installation
pipeline.helmDeploy(
dry_run : true,
name : config.app.name,
namespace : config.app.name,
chart_dir : chart_dir,
set : [
"imageTag": image_tags_list.get(0),
"replicas": config.app.replicas,
"cpu": config.app.cpu,
"memory": config.app.memory,
"ingress.hostname": config.app.hostname,
]
)
}
}
stage ('publish container') {
container('docker') {
// perform docker login to container registry as the docker-pipeline-plugin doesn't work with the next auth json format
withCredentials([[$class : 'UsernamePasswordMultiBinding', credentialsId: config.container_repo.jenkins_creds_id,
usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh "docker login -u ${env.USERNAME} -p ${env.PASSWORD} ${config.container_repo.host}"
}
// build and publish container
pipeline.containerBuildPub(
dockerfile: config.container_repo.dockerfile,
host : config.container_repo.host,
acct : acct,
repo : config.container_repo.repo,
tags : image_tags_list,
auth_id : config.container_repo.jenkins_creds_id,
image_scanning: config.container_repo.image_scanning
)
// anchore image scanning configuration
println "Add container image tags to anchore scanning list"
def tag = image_tags_list.get(0)
def imageLine = "${config.container_repo.host}/${acct}/${config.container_repo.repo}:${tag}" + ' ' + env.WORKSPACE + '/Dockerfile'
writeFile file: 'anchore_images', text: imageLine
anchore name: 'anchore_images', inputQueries: [[query: 'list-packages all'], [query: 'list-files all'], [query: 'cve-scan all'], [query: 'show-pkg-diffs base']]
}
}
if (env.BRANCH_NAME =~ "PR-*" ) {
stage ('deploy to k8s') {
container('helm') {
// Deploy using Helm chart
pipeline.helmDeploy(
dry_run : false,
name : env.BRANCH_NAME.toLowerCase(),
namespace : env.BRANCH_NAME.toLowerCase(),
chart_dir : chart_dir,
set : [
"imageTag": image_tags_list.get(0),
"replicas": config.app.replicas,
"cpu": config.app.cpu,
"memory": config.app.memory,
"ingress.hostname": config.app.hostname,
]
)
// Run helm tests
if (config.app.test) {
pipeline.helmTest(
name : env.BRANCH_NAME.toLowerCase()
)
}
// delete test deployment
pipeline.helmDelete(
name : env.BRANCH_NAME.toLowerCase()
)
}
}
}
// deploy only the master branch
if (env.BRANCH_NAME == 'master') {
stage ('deploy to k8s') {
container('helm') {
// Deploy using Helm chart
pipeline.helmDeploy(
dry_run : false,
name : config.app.name,
namespace : config.app.name,
chart_dir : chart_dir,
set : [
"imageTag": image_tags_list.get(0),
"replicas": config.app.replicas,
"cpu": config.app.cpu,
"memory": config.app.memory,
"ingress.hostname": config.app.hostname,
]
)
// Run helm tests
if (config.app.test) {
pipeline.helmTest(
name : config.app.name
)
}
}
}
}
}
}