forked from cmap/dockerized_mts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsfile-build-pipeline-docker
96 lines (91 loc) · 2.69 KB
/
jenkinsfile-build-pipeline-docker
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
/**
* Copy of the jenkins job that creates and tags dockers for dev
* Tags are develop
*/
import groovy.json.JsonSlurper
REPO="prismcmap"
DEV_TAG="develop"
WORKSPACE_DIR="/ci-data/jenkins/workspace/BUILD-PIPELINE-DOCKER/"
def parallelStagesMap(DIRS,GIT_PREVIOUS_COMMIT,GIT_COMMIT){
def mp = [:]
for( String DIR : DIRS ){
DIR = DIR.replace("/","")
CHANGED=sh(returnStdout: true, script: "git diff --name-only ${GIT_PREVIOUS_COMMIT} ${GIT_COMMIT} ${DIR}").trim()
try{
DOCKER_FILE_EXISTS=sh(returnStdout: true, script: "test -f ${DIR}/Dockerfile && echo 'FILE exists.'").trim()
if(CHANGED && DOCKER_FILE_EXISTS){
mp.put("${DIR}",generateStage(DIR))
}
}
catch(err){
}
}
return mp
}
def generateStage(job) {
return {
stage("stage: ${job}") {
dir("${job}") {
sh script: "pwd"
sh script: "docker build -t ${REPO}/${job}:${DEV_TAG} --rm=true ."
sh script: "docker push ${REPO}/${job}:${DEV_TAG}"
}
}
}
}
pipeline{
agent any
environment {
DOCKERHUB_CREDENTIALS= credentials('dockerhub-cred-jasiedu')
GIT_PREVIOUS_COMMIT=''
GIT_COMMIT=''
}
stages {
stage('Login to Docker Hub') {
steps{
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | sudo docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
echo 'Login Completed'
}
}
stage("Git Checkout"){
steps{
git credentialsId: '4c3bd279-468f-4837-a095-3366964ae937', url: 'https://github.com/cmap/dockerized_mts.git' ,branch: 'dev'
echo 'Git Checkout Completed'
}
}
stage('Configure') {
steps {
script {
GIT_PREVIOUS_COMMIT = sh(returnStdout: true, script: "git rev-parse --short 'HEAD^'").trim()
GIT_COMMIT=sh(returnStdout: true, script: "git rev-parse --short HEAD").trim()
}
}
}
stage('Checkout code') {
steps {
script {
DIRS=sh(returnStdout: true, script: "ls -d -- */").trim()
DIR_PATHS = DIRS.split("\n")
mp = parallelStagesMap(DIR_PATHS,GIT_PREVIOUS_COMMIT,GIT_COMMIT)
if(mp.size() > 0){
parallel mp
}
}
}
}
}
post {
always {
cleanWs()
dir("${env.WORKSPACE}@tmp") {
deleteDir()
}
dir("${env.WORKSPACE}@script") {
deleteDir()
}
dir("${env.WORKSPACE}@script@tmp") {
deleteDir()
}
}
}
}