forked from lachie83/croc-hunter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
140 lines (118 loc) · 4.18 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
#!/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/grafana"
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()
}
}
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 : [
"replicas": config.app.replicas,
"cpu": config.app.cpu,
"memory": config.app.memory,
"ingress.hostname": config.app.hostname,
]
)
}
}
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 : [
"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.namespace,
chart_dir : chart_dir,
set : [
"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
)
}
}
}
}
}
}