forked from djccarew/get-started-java-icp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Jenkinsfile
73 lines (70 loc) · 3.04 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
// Pod Template
def cloud = env.CLOUD ?: "kubernetes"
def registryCredsID = env.REGISTRY_CREDENTIALS ?: "registry-credentials-id"
def serviceAccount = env.SERVICE_ACCOUNT ?: "default"
// Pod Environment Variables
def namespace = env.NAMESPACE ?: "default"
def registry = env.REGISTRY ?: "mycluster.icp:8500"
podTemplate(label: 'mypod', cloud: cloud, serviceAccount: serviceAccount, namespace: namespace, envVars: [
envVar(key: 'NAMESPACE', value: namespace),
envVar(key: 'REGISTRY', value: registry)
],
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('mypod') {
checkout scm
container('maven') {
stage('Build application war file') {
sh """
#!/bin/bash
mvn -B -DskipTests clean package
"""
}
}
container('docker') {
stage('Build Docker Image') {
sh """
#!/bin/bash
docker build -t ${env.REGISTRY}/${env.NAMESPACE}/guestbook:${env.BUILD_NUMBER} .
"""
}
stage('Test Docker Image'){}
stage('Scan Image for Vulnerabilities'){}
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.NAMESPACE}/guestbook:${env.BUILD_NUMBER}
"""
}
}
}
container('kubectl') {
stage('Deploy new Docker Image') {
sh """
#!/bin/bash
DEPLOYMENT=`kubectl --namespace=${env.NAMESPACE} get deployments -l app=guestbook-app,tier=frontend -o name`
kubectl --namespace=${env.NAMESPACE} get \${DEPLOYMENT}
if [ \${?} -ne "0" ]; then
# No deployment to update
echo 'No deployment to update'
exit 1
fi
# Update Deployment
kubectl --namespace=${env.NAMESPACE} set image \${DEPLOYMENT} guestbook-web=${env.REGISTRY}/${env.NAMESPACE}/guestbook:${env.BUILD_NUMBER}
kubectl --namespace=${env.NAMESPACE} rollout status \${DEPLOYMENT}
"""
}
}
}
}