-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
133 lines (124 loc) · 4.49 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def APPS = []
def REPO_PUSH = []
pipeline {
agent {
kubernetes {
label 'docker'
defaultContainer 'jnlp'
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
app: jenkins-docker
spec:
containers:
- name: docker
image: docker:18.09.0-git
command:
- cat
tty: true
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker-sock
- name: gradle
image: gradle:5.4.1-jdk8
command:
- cat
tty: true
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
"""
}
}
environment {
GITHUB_HOOK_SECRET = ""
DOCKERHUB = credentials('dockerhub-credentials')
DOCKERHUB_USR = credentials('dockerhub-user')
DOCKERHUB_PSW = credentials('dockerhub-password')
}
stages {
stage("Find app name to build") {
steps {
script {
if (REF != "") {
VALUESFILE = sh(returnStdout: true, script:"git show -m --name-only --pretty=''")
LIST = VALUESFILE.split('\n')
def MAP = [:]
for(String file in LIST) {
FILE_PARTS = file.split('/')
if(FILE_PARTS.size() > 1){
MAP.put(FILE_PARTS[0], "build")
}
}
APPS = MAP.keySet()
echo "${APPS}"
}
}
echo "Changes in:${VALUESFILE}"
echo "application to build:${APPS}"
}
}
stage("Build and testing") {
steps {
container("gradle") {
script {
for (String app in APPS) {
TO_BUILD_REPO = sh(
script: "ls ${app}/Dockerfile",
returnStatus: true
)
if (TO_BUILD_REPO == 0) {
sh "gradle ${app}:clean ${app}:build -x test --refresh-dependencies"
}
TO_SONAR_TEST = sh(
script: "ls ${app}/sonar-project.properties",
returnStatus: true
)
if (TO_SONAR_TEST == 0) {
def scannerHome = tool 'sonar3'
withSonarQubeEnv('SonarQube') {
dir("${app}") {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}
}
}
}
}
stage("Build and push docker image") {
steps {
container("docker") {
script {
sh "docker login -u $DOCKERHUB_USR -p $DOCKERHUB_PSW"
for(String app in APPS) {
TO_BUILD = sh (
script: "ls ${app}/Dockerfile",
returnStatus: true
)
if (TO_BUILD == 0) {
env.DATE_BUILD = new Date().format('yyyyMMddhhmm')
env.APP = app
sh """
docker build ./${APP}/ -t project-name/sp-${APP}:1.0.1.${DATE_BUILD}
docker push project-name/sp-${APP}:1.0.1.${DATE_BUILD}
docker rmi project-name/sp-${APP}:1.0.1.${DATE_BUILD}
"""
}
}
sh "docker logout"
}
}
}
}
}
post {
always {
cleanWs()
}
}
}