-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
36 lines (34 loc) · 1.25 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
pipeline {
agent any
stages {
stage('Create jobs') {
steps {
script {
def files = findFiles(glob: ' **/Jenkinsfile')
for (int i = 1; i < files.size(); i++) {
echo files[i].name
def filePath = files[i].path
def pathWithoutFile = filePath.replace('/Jenkinsfile', '')
def jobName = ( pathWithoutFile =~ /([^\/]+)\/?$/)[0][0]
echo filePath
echo jobName
// Instance, getItemMap is insecure.
if(Jenkins.get().getItemMap()[jobName] == null) {
echo "Job ${jobName} does not exist, creating..."
createJob(filePath, jobName)
} else {
echo "Job ${jobName} already exists."
}
}
}
}
}
}
}
def createJob(filePath, jobName){
jobDsl targets: '*.groovy',
removedJobAction: 'IGNORE',
removedViewAction: 'IGNORE',
lookupStrategy: 'JENKINS_ROOT',
additionalParameters: [jenkinsfile: filePath, Name: jobName]
}