|
| 1 | +/* |
| 2 | + whenChanged('some/path') { yourCode() } can be used to execute pipeline code in PRs only when changes are detected on paths that you specify. |
| 3 | + The specified code blocks will also always be executed during the non-PR jobs for tracked branches. |
| 4 | +
|
| 5 | + You have the option of passing in path prefixes, or regexes. Single or multiple. |
| 6 | + Path specifications are NOT globby, they are only prefixes. |
| 7 | + Specifying multiple will treat them as ORs. |
| 8 | +
|
| 9 | + Example Usages: |
| 10 | + whenChanged('a/path/prefix/') { someCode() } |
| 11 | + whenChanged(startsWith: 'a/path/prefix/') { someCode() } // Same as above |
| 12 | + whenChanged(['prefix1/', 'prefix2/']) { someCode() } |
| 13 | + whenChanged(regex: /\.test\.js$/) { someCode() } |
| 14 | + whenChanged(regex: [/abc/, /xyz/]) { someCode() } |
| 15 | +*/ |
| 16 | + |
| 17 | +def call(String startsWithString, Closure closure) { |
| 18 | + return whenChanged([ startsWith: startsWithString ], closure) |
| 19 | +} |
| 20 | + |
| 21 | +def call(List<String> startsWithStrings, Closure closure) { |
| 22 | + return whenChanged([ startsWith: startsWithStrings ], closure) |
| 23 | +} |
| 24 | + |
| 25 | +def call(Map params, Closure closure) { |
| 26 | + if (!githubPr.isPr()) { |
| 27 | + return closure() |
| 28 | + } |
| 29 | + |
| 30 | + def files = prChanges.getChangedFiles() |
| 31 | + def hasMatch = false |
| 32 | + |
| 33 | + if (params.regex) { |
| 34 | + params.regex = [] + params.regex |
| 35 | + print "Checking PR for changes that match: ${params.regex.join(', ')}" |
| 36 | + hasMatch = !!files.find { file -> |
| 37 | + params.regex.find { regex -> file =~ regex } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (!hasMatch && params.startsWith) { |
| 42 | + params.startsWith = [] + params.startsWith |
| 43 | + print "Checking PR for changes that start with: ${params.startsWith.join(', ')}" |
| 44 | + hasMatch = !!files.find { file -> |
| 45 | + params.startsWith.find { str -> file.startsWith(str) } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (hasMatch) { |
| 50 | + print "Changes found, executing pipeline." |
| 51 | + closure() |
| 52 | + } else { |
| 53 | + print "No changes found, skipping." |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +return this |
0 commit comments