-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
91 lines (90 loc) · 3.32 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
def stackName = 'prod'
def workerLabel = "filters-${stackName}"
def dockerHubAccount = 'mytardis'
def dockerImageName = 'k8s-filters'
def dockerImageTag = ''
def dockerImageFullNameTag = ''
def dockerImageFullNameLatest = "${dockerHubAccount}/${dockerImageName}:latest"
def k8sDeploymentNamespace = 'mytardis'
podTemplate(
label: workerLabel,
serviceAccount: 'jenkins',
automountServiceAccountToken: true,
containers: [
containerTemplate(
name: 'docker',
image: 'docker:18.06.1-ce-dind',
ttyEnabled: true,
command: 'cat',
envVars: [
containerEnvVar(key: 'DOCKER_CONFIG', value: '/tmp/docker')
],
resourceRequestCpu: '2000m',
resourceLimitCpu: '2000m',
resourceRequestMemory: '2Gi',
resourceLimitMemory: '2Gi'
),
containerTemplate(
name: 'kubectl',
image: 'lachlanevenson/k8s-kubectl:v1.13.0',
ttyEnabled: true,
command: 'cat',
envVars: [
containerEnvVar(key: 'KUBECONFIG', value: '/tmp/kube/admin.conf')
],
resourceLimitCpu: '250m'
)
],
volumes: [
secretVolume(secretName: "kube-config-${stackName}", mountPath: '/tmp/kube'),
secretVolume(secretName: 'docker-config', mountPath: '/tmp/docker'),
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')
]
) {
node(workerLabel) {
stage('Clone repository') {
checkout scm
}
dockerImageTag = sh(returnStdout: true, script: 'git log -n 1 --pretty=format:"%h"').trim()
dockerImageFullNameTag = "${dockerHubAccount}/${dockerImageName}:${dockerImageTag}"
stage('Build image for tests') {
container('docker') {
sh("docker build . --tag ${dockerImageFullNameTag} --target=test")
}
}
def tests = [:]
[
'pylint': "docker run ${dockerImageFullNameTag} pylint --rcfile .pylintrc --django-settings-module=tardis.settings tardis",
'flake8': "docker run ${dockerImageFullNameTag} flake8 --config=.flake8 tardis",
'tests': "docker run ${dockerImageFullNameTag} python3 manage.py test"
].each { name, command ->
tests[name] = {
stage("Run test - ${name}") {
container('docker') {
sh(command)
}
}
}
}
parallel tests
stage('Build image for production') {
container('docker') {
sh("docker build . --tag ${dockerImageFullNameTag} --target=builder")
}
}
stage('Push image to DockerHub') {
container('docker') {
sh("docker push ${dockerImageFullNameTag}")
sh("docker tag ${dockerImageFullNameTag} ${dockerImageFullNameLatest}")
sh("docker push ${dockerImageFullNameLatest}")
}
}
stage('Deploy image to Kubernetes') {
container('kubectl') {
['filters'].each { item ->
sh ("kubectl -n ${k8sDeploymentNamespace} set image deployment/${item} ${item}=${dockerImageFullNameTag}")
}
}
}
}
}