-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
83 lines (78 loc) · 3.78 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
// Pod Template
def cloud = env.CLOUD ?: "kubernetes"
def serviceAccount = env.SERVICE_ACCOUNT ?: "jnkns-for-cicd-lab-jenkins"
def registryCredsID = env.REGISTRY_CREDENTIALS ?: "registry-credentials"
def namespace = env.NAMESPACE ?: "default"
def registry = env.REGISTRY ?: "mycluster.icp:8500"
def majorPrefix = env.MAJOR_PREFIX ?: "1.0.0"
// In this multiuser scenario we derive user specific vars from the logged in Jenkins user
//def userName = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()
def userName = env.USERID ?: "anonymous"
def releaseName = "pbw-liberty-mariadb-" + userName
def deploymentNS = env.DEVELOPER_NS ?: "default"
def podLabel = "agent-" + releaseName
podTemplate(label: podLabel, cloud: cloud, serviceAccount: serviceAccount, namespace: namespace, envVars: [
envVar(key: 'NAMESPACE', value: namespace),
envVar(key: 'DEPLOYMENT_NS', value: deploymentNS),
envVar(key: 'REGISTRY', value: registry),
envVar(key: 'RELEASE_NAME', value: releaseName),
envVar(key: 'MAJOR_PREFIX', value: majorPrefix),
envVar(key: 'USERNAME', value: userName)
],
volumes: [
hostPathVolume(hostPath: '/etc/docker/certs.d', mountPath: '/etc/docker/certs.d'),
hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')
],
containers: [
containerTemplate(name: 'maven', image: 'maven:3-alpine', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl', ttyEnabled: true, command: 'cat'),
containerTemplate(name: 'docker' , image: 'docker:17.06.1-ce', ttyEnabled: true, command: 'cat')
]) {
node(podLabel) {
checkout scm
container('maven') {
stage('Build application ear file') {
sh """
#!/bin/bash
mvn -B -DskipTests -DuserName=${env.USERNAME} clean package
"""
}
}
container('docker') {
stage('Build Docker Image') {
sh """
#!/bin/bash
docker build -t ${env.REGISTRY}/${env.DEPLOYMENT_NS}/pbw-mariadb-web:${env.MAJOR_PREFIX}.${env.BUILD_NUMBER} .
"""
}
stage('Push Docker Image to Registry') {
withCredentials([usernamePassword(credentialsId: registryCredsID,
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD')]) {
sh """
#!/bin/bash
docker login -u ${USERNAME} -p ${PASSWORD} ${env.REGISTRY}
docker push ${env.REGISTRY}/${env.DEPLOYMENT_NS}/pbw-mariadb-web:${env.MAJOR_PREFIX}.${env.BUILD_NUMBER}
"""
}
}
}
container('kubectl') {
stage('Deploy new Docker Image') {
sh """
#!/bin/bash
DEPLOYMENT=`kubectl --namespace=${env.DEPLOYMENT_NS} get deployments -l app=pbw-liberty-mariadb,component=web-app,release=${env.RELEASE_NAME} --no-headers -o name`
kubectl --namespace=${env.DEPLOYMENT_NS} get \${DEPLOYMENT} --no-headers -o custom-columns=":metadata.name"
if [ \${?} -ne "0" ]; then
# No deployment to update
echo 'No deployment to update'
exit 1
fi
# Update Deployment
kubectl --namespace=${env.DEPLOYMENT_NS} set image \${DEPLOYMENT} ${env.RELEASE_NAME}-liberty=${env.REGISTRY}/${env.DEPLOYMENT_NS}/pbw-mariadb-web:${env.MAJOR_PREFIX}.${env.BUILD_NUMBER}
kubectl --namespace=${env.DEPLOYMENT_NS} rollout status \${DEPLOYMENT}
"""
}
}
}
}