Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postbuild actions #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down Expand Up @@ -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

Expand Down
40 changes: 30 additions & 10 deletions src/main/groovy/com/base2/ciinabox/JobHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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',[]))

Expand Down Expand Up @@ -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',[:])))
}
}
}
}
Expand Down