Skip to content

Commit ebf05ee

Browse files
committed
Merge pull request #403 from CoreMedia/postbuildscript
added support for PostBuildScript Plugin
2 parents 7d95941 + 9f9ff77 commit ebf05ee

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

docs/Home.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Have a look at the [Jenkins Job DSL Gradle example](https://github.com/sheehan/j
2020
* Added support for [Build Node Column Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Build+Node+Column+Plugin)
2121
* Added support for [Pre-SCM Build Step Plugin](https://wiki.jenkins-ci.org/display/JENKINS/pre-scm-buildstep)
2222
* Added `ignorePostCommitHooks` option for SCM trigger
23+
* Added support for [PostBuildScript Plugin](https://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+Plugin)
2324
* The enum argument of `localRepository` for the Maven job and context has changed, see [[Migration]]
2425
* 1.30 (March 08 2015)
2526
* Added support for [Custom Tools Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Custom+Tools+Plugin)

docs/Job-reference.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ freeStyleJob(String name) { // since 1.30
228228
Boolean sendToIndividuals = false)
229229
mavenDeploymentLinker(String regex) // since 1.23
230230
pmd(String pattern, Closure staticAnalysisClosure = null)
231+
postBuildScripts(Closure postBuildScriptsClosure) // since 1.31
231232
postBuildTask(Closure closure) // since 1.19
232233
publishCloneWorkspace(String workspaceGlob, Closure cloneWorkspaceClosure)
233234
publishCloneWorkspace(String workspaceGlob, String workspaceExcludeGlob,
@@ -3987,6 +3988,37 @@ publishers {
39873988
39883989
(Since 1.19)
39893990
3991+
### Post Build Scripts
3992+
3993+
```groovy
3994+
job {
3995+
publishers {
3996+
postBuildScripts {
3997+
steps(Closure stepClosure)
3998+
onlyIfBuildSucceeds(boolean onlyIfBuildSucceeds = true) // defaults to true
3999+
}
4000+
}
4001+
}
4002+
```
4003+
4004+
Execute a set of scripts at the end of the build. Requires the
4005+
[PostBuildScript Plugin](https://wiki.jenkins-ci.org/display/JENKINS/PostBuildScript+Plugin).
4006+
4007+
```groovy
4008+
job {
4009+
publishers {
4010+
postBuildScripts {
4011+
steps {
4012+
shell('echo Hello World')
4013+
}
4014+
onlyIfBuildSucceeds(false)
4015+
}
4016+
}
4017+
}
4018+
```
4019+
4020+
(since 1.31)
4021+
39904022
### [Post Build Task](https://wiki.jenkins-ci.org/display/JENKINS/Post+build+task)
39914023
39924024
Searches for a regular expression in the console log and, if matched, executes a script. Requires the [Post Build Task Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Post+build+task).
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package javaposse.jobdsl.dsl.helpers.publisher
2+
3+
import javaposse.jobdsl.dsl.Context
4+
import javaposse.jobdsl.dsl.ContextHelper
5+
import javaposse.jobdsl.dsl.DslContext
6+
import javaposse.jobdsl.dsl.JobManagement
7+
import javaposse.jobdsl.dsl.helpers.step.StepContext
8+
9+
class PostBuildScriptsContext implements Context {
10+
final StepContext stepContext
11+
boolean onlyIfBuildSucceeds = true
12+
13+
PostBuildScriptsContext(JobManagement jobManagement) {
14+
this.stepContext = new StepContext(jobManagement)
15+
}
16+
17+
void steps(@DslContext(StepContext) Closure stepClosure) {
18+
ContextHelper.executeInContext(stepClosure, stepContext)
19+
}
20+
21+
void onlyIfBuildSucceeds(boolean onlyIfBuildSucceeds = true) {
22+
this.onlyIfBuildSucceeds = onlyIfBuildSucceeds
23+
}
24+
}

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContext.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,26 @@ class PublisherContext implements Context {
17051705
}
17061706
}
17071707

1708+
/**
1709+
* <org.jenkinsci.plugins.postbuildscript.PostBuildScript>
1710+
* <buildSteps>
1711+
* <hudson.tasks.Shell>
1712+
* <command>echo foo</command>
1713+
* </hudson.tasks.Shell>
1714+
* </buildSteps>
1715+
* <scriptOnlyIfSuccess>true</scriptOnlyIfSuccess>
1716+
* </org.jenkinsci.plugins.postbuildscript.PostBuildScript>
1717+
*/
1718+
void postBuildScripts(@DslContext(PostBuildScriptsContext) Closure closure) {
1719+
PostBuildScriptsContext context = new PostBuildScriptsContext(jobManagement)
1720+
ContextHelper.executeInContext(closure, context)
1721+
1722+
publisherNodes << new NodeBuilder().'org.jenkinsci.plugins.postbuildscript.PostBuildScript' {
1723+
buildSteps(context.stepContext.stepNodes)
1724+
scriptOnlyIfSuccess(context.onlyIfBuildSucceeds)
1725+
}
1726+
}
1727+
17081728
private static createDefaultStaticAnalysisNode(String publisherClassName, Closure staticAnalysisClosure,
17091729
String pattern) {
17101730
StaticAnalysisContext staticAnalysisContext = new StaticAnalysisContext()

job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContextSpec.groovy

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,4 +2992,37 @@ class PublisherContextSpec extends Specification {
29922992
then:
29932993
thrown(IllegalArgumentException)
29942994
}
2995+
2996+
def 'call post build scripts with minimal options'() {
2997+
when:
2998+
context.postBuildScripts {
2999+
}
3000+
3001+
then:
3002+
with(context.publisherNodes[0]) {
3003+
name() == 'org.jenkinsci.plugins.postbuildscript.PostBuildScript'
3004+
children().size() == 2
3005+
buildSteps[0].children().size == 0
3006+
scriptOnlyIfSuccess[0].value() == true
3007+
}
3008+
}
3009+
3010+
def 'call post build scripts with all options'() {
3011+
when:
3012+
context.postBuildScripts {
3013+
steps {
3014+
shell('echo TEST')
3015+
}
3016+
onlyIfBuildSucceeds(false)
3017+
}
3018+
3019+
then:
3020+
with(context.publisherNodes[0]) {
3021+
name() == 'org.jenkinsci.plugins.postbuildscript.PostBuildScript'
3022+
children().size() == 2
3023+
buildSteps[0].children().size == 1
3024+
buildSteps[0].children()[0].name() == 'hudson.tasks.Shell'
3025+
scriptOnlyIfSuccess[0].value() == false
3026+
}
3027+
}
29953028
}

0 commit comments

Comments
 (0)