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
| def label = "master"
def notifyStarted() { slackSend (color: '#FFFF00', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})") }
def notifySuccessful() { slackSend (color: '#00FF00', message: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})") }
def notifyFailed() { slackSend (color: '#FF0000', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})") }
podTemplate(label: label, containers: [ containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'), containerTemplate(name: 'docker', image: 'docker:stable', ttyEnabled: true, command: 'cat'), containerTemplate(name: 'kubectl', image: 'lachlanevenson/k8s-kubectl:v1.8.0', command: 'cat', ttyEnabled: true) ], volumes: [hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'), hostPathVolume(hostPath: '/root/.m2', mountPath: '/root/.m2') ]
) {
node(label) { try {
stage('Get Source') { git "http://35.200.116.34/sangheonKim/sangheonProject" }
def props = readProperties file:'deployment/pipeline.properties' /*def tag = VersionNumber (versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')*/ def tag = props['version'] def gitSrc = props['gitSrc'] def dockerRegistry = props['dockerRegistry'] def image = props['image'] def deployment = props['deployment'] def service = props['service'] def selector = props['selector'] def namespace = props['namespace']
stage('Build Maven project') { container('maven') { sh "mvn -B clean package" } } /* stage('Inspection Code') { container('maven') { sh "mvn sonar:sonar \ -Dsonar.projectKey=sangheon \ -Dsonar.host.url=http://34.85.31.212:9000 \ -Dsonar.login=62cf8d43f8fda31db0c5682c71b584c8650f4509" } } */ stage('Build Docker image') { container('docker') { docker.withRegistry("${dockerRegistry}", 'registry-credentials') { sh "docker build -t ${image}:${tag} ." sh "docker push ${image}:${tag}" sh "docker tag ${image}:${tag} ${image}:latest" sh "docker push ${image}:latest" } } }
stage( 'Clean Up Existing Deployments' ) { container('kubectl') { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'registry-credentials', usernameVariable: 'DOCKER_HUB_USER', passwordVariable: 'DOCKER_HUB_PASSWORD']]) {
sh "kubectl delete deployments -n ${namespace} --selector=app=${selector}" } } }
stage( 'Deploy to Cluster' ) { container('kubectl') { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'registry-credentials', usernameVariable: 'DOCKER_HUB_USER', passwordVariable: 'DOCKER_HUB_PASSWORD']]) {
sh "kubectl apply -n ${namespace} -f ${deployment}" sh "sleep 5" sh "kubectl apply -n ${namespace} -f ${service}" } } }
notifySuccessful() } catch(e) { print(e) currentBuild.result = "FAILED" notifyFailed() }
} }
|