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

Add post build task for multiply conditions #1239

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/The-Configure-Block.md
Expand Up @@ -851,6 +851,22 @@ job('example') {
}
```

## Configure Post Build Task Publisher with multiply conditions
DSL:
```groovy
job('example') {
publishers {
def conditions = [
['Build was aborted', 'AND'],
['Simulation.*started', 'AND']
]
postBuildTaskChainConditions {
task(conditions, 'git clean -fdx')
}
}
}
```

## Configure Post Build Sonar Task

In order to trigger a Sonar analysis as a Post Build Task in your job you can use the following code.
Expand Down
@@ -0,0 +1,33 @@
package javaposse.jobdsl.dsl.helpers.publisher

import groovy.transform.Canonical
import javaposse.jobdsl.dsl.Context
import javaposse.jobdsl.dsl.Preconditions

class PostBuildTaskMultiplyContext implements Context {
List<PostBuildTask> tasks = []

/**
* Adds a script that will be executed if the output in the console log matches the regular expression. Can be
* called multiple times to add more scripts.
*/
void task(List<String[]> conditions, String script, boolean escalate = false,
boolean runIfSuccessful = false) {
Preconditions.checkNotNullOrEmpty(script, 'Script to run is required!')

tasks << new PostBuildTask(
conditions: conditions,
script: script,
escalateStatus: escalate,
runIfJobSuccessful: runIfSuccessful
)
}

@Canonical
static class PostBuildTask {
List<String[]> conditions
String script
boolean escalateStatus
boolean runIfJobSuccessful
}
}
Expand Up @@ -746,6 +746,37 @@ class PublisherContext extends AbstractExtensibleContext {
}
}

/**
* Searches with a few conditions for a regular expression in the console log and, if matched, executes a script.
*
* @since 1.19
*/
@RequiresPlugin(id = 'postbuild-task')
void postBuildTaskMultiplyConditions(@DslContext(PostBuildTaskMultiplyContext) Closure postBuildClosure) {
PostBuildTaskMultiplyContext postBuildContext = new PostBuildTaskMultiplyContext()
ContextHelper.executeInContext(postBuildClosure, postBuildContext)

publisherNodes << new NodeBuilder().'hudson.plugins.postbuildtask.PostbuildTask' {
tasks {
postBuildContext.tasks.each { PostBuildTaskMultiplyContext.PostBuildTask task ->
'hudson.plugins.postbuildtask.TaskProperties' {
logTexts {
task.conditions.each { conditionMap ->
'hudson.plugins.postbuildtask.LogProperties' {
logText(conditionMap[0])
operator(conditionMap[1])
}
}
}
EscalateStatus(task.escalateStatus)
RunIfJobSuccessful(task.runIfJobSuccessful)
script(task.script)
}
}
}
}
}

/**
* Aggregates downstream test results.
*
Expand Down
Expand Up @@ -2028,6 +2028,31 @@ class PublisherContextSpec extends Specification {
1 * jobManagement.requirePlugin('postbuild-task')
}

def 'call postBuildTaskChain with two conditions'() {
def conditions = [
['Build was aborted', 'AND'],
['Simulation.*started', 'AND']
]
when:
context.postBuildTaskMultiplyConditions {
task(conditions, 'git clean -fdx')
}

then:
context.publisherNodes.size() == 1
context.publisherNodes[0].name() == 'hudson.plugins.postbuildtask.PostbuildTask'
with(context.publisherNodes[0].tasks[0].'hudson.plugins.postbuildtask.TaskProperties'[0]) {
logTexts[0].'hudson.plugins.postbuildtask.LogProperties'[0].logText[0].value() == 'Build was aborted'
logTexts[0].'hudson.plugins.postbuildtask.LogProperties'[0].operator[0].value() == 'AND'
logTexts[0].'hudson.plugins.postbuildtask.LogProperties'[1].logText[0].value() == 'Simulation.*started'
logTexts[0].'hudson.plugins.postbuildtask.LogProperties'[1].operator[0].value() == 'AND'
EscalateStatus[0].value() == false
RunIfJobSuccessful[0].value() == false
script[0].value() == 'git clean -fdx'
}
1 * jobManagement.requirePlugin('postbuild-task')
}

def 'call postBuildTask with two tasks'() {
when:
context.postBuildTask {
Expand Down