diff --git a/README.md b/README.md index 9733979..1c8c7bb 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,8 @@ defaults: protocol: http # ssh or https # be ID of Jenkins credentials # https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Plugin - - name: Ciinabox-MultipleGithub +jobs: + - name: Ciinabox-MultipleGithub folder: dsl-doc github: # Multiple GitHub repos defined as list - @@ -648,6 +649,23 @@ End user should use `multifile` key. In example below application secrets are be ``` +### Postbuild Actions + +A postbuild action can be specified using the key `post_trigger`. There are currently two types of actions that are supported: +- `job`, which runs another job +- `groovy`, which can execute Groovy from either a file or inline code +```yaml + - name: PostbuildExample + folder: dsl-doc + parameters: + key1: value1 + post_trigger: + - job: folder/MyJob + current_parameters: true + - groovy: + - file: user/scripts/test.groovy # Or below + - script: "println hello" +``` ### Pipeline jobs diff --git a/src/main/groovy/com/base2/ciinabox/JobHelper.groovy b/src/main/groovy/com/base2/ciinabox/JobHelper.groovy index e1eb2ec..e56b8c2 100644 --- a/src/main/groovy/com/base2/ciinabox/JobHelper.groovy +++ b/src/main/groovy/com/base2/ciinabox/JobHelper.groovy @@ -2,6 +2,7 @@ package com.base2.ciinabox import com.base2.ciinabox.ext.ExtensionBase import com.base2.util.ReflectionUtils +import javaposse.jobdsl.dsl.helpers.publisher.PublisherContext.Behavior class JobHelper { @@ -18,7 +19,7 @@ class JobHelper { steps(job,vars) triggerJobs(job,vars.get('trigger',[:]),vars) publishers(job, vars) - postTriggerJobs(job,vars.get('post_trigger',[]),vars) + postTriggerActions(job,vars.get('post_trigger',[]),vars) archive(job, vars.get('archive', [])) gitPush(job, vars.get('push',[])) @@ -446,17 +447,36 @@ class JobHelper { } } - static postTriggerJobs(def job, def triggers, def vars) { + static postTriggerActions(def job, def triggers, def vars) { job.publishers { - triggers.each { triggerJob -> - downstreamParameterized { - trigger(triggerJob.get('job')) { - parameters { - if(triggerJob.get('current_parameters',false)) { - currentBuild() + triggers.each { action -> + if (action.get('groovy')) { + def scriptDir = lookupDefault(vars,'scripts_dir','ciinaboxes') + def steps = action.get('groovy') + steps.each { step -> + step.each { type, value -> + if(type == 'file') { + File file = new File(scriptDir + '/' + value) + if(file.exists()) { + groovyPostBuild(file.text, Behavior.DoNothing) + } else { + throw new RuntimeException("Groovy script '${value}' does not exist in the 'ciinaboxes' directory.") + } + } else if(type == 'script') { + groovyPostBuild(value, Behavior.DoNothing) } - if(triggerJob.containsKey('parameters')) { - predefinedProps(toUpperCaseKeys(triggerJob.get('parameters',[:]))) + } + } + } else { + downstreamParameterized { + trigger(action.get('job')) { + parameters { + if(action.get('current_parameters',false)) { + currentBuild() + } + if(action.containsKey('parameters')) { + predefinedProps(toUpperCaseKeys(action.get('parameters',[:]))) + } } } }