forked from daticahealth/java-tomcat-maven-example
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Jenkinsfile-k8s
110 lines (107 loc) · 3.92 KB
/
Jenkinsfile-k8s
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
pipeline{
agent{
kubernetes{
label 'maventest'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
project-name: sq
project-job: sqjob
spec:
containers:
- name: maven35
image: maven:3.5.4-jdk-10-slim
command:
- cat
tty: true
- name: dockerclient
image: cloudbees/java-with-docker-client
command:
- cat
tty: true
volumeMounts:
- name: dockersocket
mountPath: /var/run/docker.sock
volumes:
- name: dockersocket
hostPath:
path: /var/run/docker.sock
"""
}
}
environment{
NOTIFYUSERS = 'lokeshkamalay@gmail.com'
REGISTRY = 'lokeshkamalay/sq'
DOCKERLOGIN = credentials('lokidockerid')
// DOCKERAUTH = credentials('lokiucpauthid')
REPO = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
EXTENSION = readMavenPom().getPackaging()
// TAG = VERSION.replace("-SNAPSHOT", "-${env.BUILD_NUMBER}")
APPNAME = 'sq'
CNAME = 'tomcat'
TAG = 'kubernetes'
}
stages{
// stage('Checkout'){
// steps{
// container('maven35'){
// git 'https://github.com/lokeshkamalay/java-tomcat-maven-example.git'
// }
// }
// }
stage('Build'){
steps{
container('maven35'){
sh 'mvn clean package'
}
}
}
stage('Preparing Image'){
steps{
container('dockerclient'){
sh '''
#Following in case you are using Docker Enterprise or private Kubernetes cluster
#curl -k -H "Authorization: Bearer ${DOCKERAUTH}" https://kubernetes/api/clientbundle -o bundle.zip
#unzip bundle.zip
#export DOCKER_TLS_VERIFY=1
#export COMPOSE_TLS_VERSION=TLSv1_2
#export DOCKER_CERT_PATH=$PWD
#export DOCKER_HOST=tcp://dockerenterprise:443
docker build --build-arg ARTIFACT="${REPO}.${EXTENSION}" -t ${REGISTRY}:${TAG} -f Dockerfile .
#docker login -u "${DOCKERLOGIN_USR}" -p "${DOCKERLOGIN_PSW}" "${REGISTRY}" #In case or private registery
docker login -u "${DOCKERLOGIN_USR}" -p "${DOCKERLOGIN_PSW}"
docker push ${REGISTRY}:${TAG}
'''
}
}
}
stage('Deployment'){
steps{
container('dockerclient'){
withCredentials([
string(credentialsId: 'deployer-token', variable: 'TOKEN')
]) {
sh '''
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
chmod a+x kubectl
mv ./kubectl /usr/local/bin/kubectl
'''
sh "kubectl config set-cluster minikube "+
"--server=https://192.168.99.100:8443 "+
"--insecure-skip-tls-verify=true"
sh "kubectl config set-credentials deployer --token='$TOKEN'"
sh "kubectl config set-context minikube --user=deployer --cluster=minikube"
sh "kubectl config use-context minikube"
sh '''
kubectl set image deployments/"${APPNAME}" "${CNAME}"="${REGISTRY}:${TAG}"
'''
}
}
}
}
}
}