/
k8s-Jenkinsfile
75 lines (73 loc) · 2.08 KB
/
k8s-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
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
jenkins-job-name: ${JOB_NAME}
spec:
containers:
- name: maven
image: maven:alpine
command:
- cat
tty: true
volumeMounts:
- name: mvnrepository
mountPath: /root/.m2/repository
subPath: .m2
- name: docker
image: docker
command: ['cat']
tty: true
volumeMounts:
- name: dockersock
mountPath: /var/run/docker.sock
volumes:
- name: dockersock
hostPath:
path: /var/run/docker.sock
- name: mvnrepository
persistentVolumeClaim:
claimName: jenkins-jenkins-home
"""
}
}
parameters {
string(name: 'version', defaultValue: '0.1', description: 'Version override appended to maven version number')
}
stages {
stage('Build') {
steps {
container('maven') {
sh 'mvn clean package --no-transfer-progress --batch-mode -DskipTests=true'
}
}
}
stage('Build docker images') {
environment {
nexus_docker_repo = 'docker-nexus.ingress.test.dcpgreendot.com'
dcp_demo_app_tag = 'petclinic'
PETCLINIC_VERSION = sh (
script: 'xf=`ls target/*.jar` && echo $xf | awk \'{print substr($1,1,match($1,/.[^.]*$/)-1)}\' | awk \'{print substr($1,match($1,/[0-9]/))}\'',
returnStdout: true
).trim()
PETCLINIC_VERSION_CUSTOM = "${PETCLINIC_VERSION}${params.version}"
}
steps {
withCredentials([usernamePassword(credentialsId: 'NexusID', usernameVariable: 'nexus_docker_repo_user', passwordVariable: 'nexus_docker_repo_password')]){
container('docker') {
sh '''
echo "Publish Docker Image to Nexus"
docker login -u ${nexus_docker_repo_user} -p ${nexus_docker_repo_password} ${nexus_docker_repo}
docker build -t ${nexus_docker_repo}/${dcp_demo_app_tag}:${PETCLINIC_VERSION_CUSTOM} -f Dockerfile .
docker push ${nexus_docker_repo}/${dcp_demo_app_tag}:${PETCLINIC_VERSION_CUSTOM}
'''
}
}
}
}
}
}