def changedFilesList = [] def forcedBuild = false def allProjects = ['Project1', 'Project2', 'Project3', 'Project4', 'Project5'] pipeline { agent { node { label 'WindowsBuildBox' } } stages { stage('DetectChanges') { steps { git(url: '', branch: env.BRANCH_NAME, changelog: true, credentialsId: '') script { def changeLogSets = currentBuild.changeSets for (int i = 0; i < changeLogSets.size(); i++) { def entries = changeLogSets[i].items for (int j = 0; j < entries.length; j++) { def entry = entries[j] echo "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}" def files = new ArrayList(entry.affectedFiles) for (int k = 0; k < files.size(); k++) { def file = files[k] echo " ${file.editType.name} ${file.path}" changedFilesList.add("${file.path}") } } } if (changedFilesList.isEmpty()) { echo "No files were changed" forcedBuild = true }else{ echo "changed files: $changedFilesList" } } } } stage('Build Trigger') { steps { echo "${allProjects}" buildProject(allProjects as String[], forcedBuild, changedFilesList as String[]) } } } } void buildProject(String[] allProjects, boolean forcedBuild, String[] changedFilesList) { allProjects.each { project -> script { stage(project) { when { expression { forcedBuild || changedFilesList.join(", ").contains("${project}") } } dir("${project}") { bat("echo building: ${project}") } } } } }