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

[JENKINS-32924] Add LogRotator to Workflow Multibranch #746

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ multibranchWorkflowJob('example') {
numToKeep(20)
}
}
logRotator {
daysToKeep(5)
numToKeep(1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class BranchSourcesContext implements Context {
excludes(context.excludes ?: '')
ignoreOnPushNotifications(context.ignoreOnPushNotifications)
}
strategy(class: 'jenkins.branch.DefaultBranchPropertyStrategy') {
properties(class: 'empty-list')
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import javaposse.jobdsl.dsl.ContextHelper
import javaposse.jobdsl.dsl.DslContext
import javaposse.jobdsl.dsl.Folder
import javaposse.jobdsl.dsl.JobManagement
import javaposse.jobdsl.dsl.LogRotatorContext
import javaposse.jobdsl.dsl.helpers.triggers.MultibranchWorkflowTriggerContext
import javaposse.jobdsl.dsl.helpers.workflow.OrphanedItemStrategyContext
import javaposse.jobdsl.dsl.helpers.workflow.BranchSourcesContext
Expand Down Expand Up @@ -41,6 +42,32 @@ class MultibranchWorkflowJob extends Folder {
}
}

/**
* Adds a logRotator.
*/
void logRotator(@DslContext(LogRotatorContext) Closure closure) {
LogRotatorContext context = new LogRotatorContext()
ContextHelper.executeInContext(closure, context)

Node propertiesNode = new NodeBuilder().'properties'(class: 'java.util.Arrays$ArrayList') {
a(class: 'jenkins.branch.BranchProperty-array') {
'jenkins.branch.BuildRetentionBranchProperty' {
buildDiscarder(class: 'hudson.tasks.LogRotator') {
daysToKeep(context.daysToKeep ?: -1)
numToKeep(context.numToKeep ?: -1)
artifactDaysToKeep(context.artifactDaysToKeep ?: -1)
artifactNumToKeep(context.artifactNumToKeep ?: -1)
}
}
}
}
Node strategyNode = new NodeBuilder().strategy(class: 'jenkins.branch.DefaultBranchPropertyStrategy')

configure { Node project ->
project / sources / data / 'jenkins.branch.BranchSource' / strategyNode / propertiesNode
}
}

/**
* Sets the orphaned branch strategy.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BranchSourcesContextsSpec extends Specification {
context.branchSourceNodes.size() == 1
with(context.branchSourceNodes[0]) {
name() == 'jenkins.branch.BranchSource'
children().size() == 2
children().size() == 1
with(source[0]) {
children().size() == 6
id[0].value() instanceof UUID
Expand All @@ -23,12 +23,6 @@ class BranchSourcesContextsSpec extends Specification {
excludes[0].value().empty
ignoreOnPushNotifications[0].value() == false
}
with(strategy[0]) {
children().size() == 1
attribute('class') == 'jenkins.branch.DefaultBranchPropertyStrategy'
properties[0].value().empty
properties[0].attribute('class') == 'empty-list'
}
}
}

Expand All @@ -46,7 +40,7 @@ class BranchSourcesContextsSpec extends Specification {
context.branchSourceNodes.size() == 1
with(context.branchSourceNodes[0]) {
name() == 'jenkins.branch.BranchSource'
children().size() == 2
children().size() == 1
with(source[0]) {
children().size() == 6
id[0].value() instanceof UUID
Expand All @@ -56,12 +50,6 @@ class BranchSourcesContextsSpec extends Specification {
excludes[0].value() == 'ipsum'
ignoreOnPushNotifications[0].value() == true
}
with(strategy[0]) {
children().size() == 1
attribute('class') == 'jenkins.branch.DefaultBranchPropertyStrategy'
properties[0].value().empty
properties[0].attribute('class') == 'empty-list'
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MultibranchWorkflowJobSpec extends Specification {

then:
job.node.sources[0].data.size() == 1
job.node.sources[0].data[0].'jenkins.branch.BranchSource'[0].children().size() == 2
job.node.sources[0].data[0].'jenkins.branch.BranchSource'[0].children().size() == 1
job.node.sources[0].data[0].'jenkins.branch.BranchSource'[0]
.'source'[0].attribute('class') == 'jenkins.plugins.git.GitSCMSource'
}
Expand All @@ -44,6 +44,27 @@ class MultibranchWorkflowJobSpec extends Specification {
job.node.orphanedItemStrategy[0].daysToKeep[0].value() == 20
}

def 'can add logRotator'() {
when:
job.logRotator {
daysToKeep(5)
numToKeep(1)
}

then:
job.node.sources[0].data[0].'jenkins.branch.BranchSource'[0]
.'strategy'[0].attribute('class') == 'jenkins.branch.DefaultBranchPropertyStrategy'
job.node.sources[0].data[0].'jenkins.branch.BranchSource'[0]
.'strategy'[0].'properties'[0].a[0].'jenkins.branch.BuildRetentionBranchProperty'[0]
.'buildDiscarder'[0].attribute('class') == 'hudson.tasks.LogRotator'
job.node.sources[0].data[0].'jenkins.branch.BranchSource'[0]
.'strategy'[0].'properties'[0].a[0].'jenkins.branch.BuildRetentionBranchProperty'[0]
.'buildDiscarder'[0].children().size() == 4
job.node.sources[0].data[0].'jenkins.branch.BranchSource'[0]
.'strategy'[0].'properties'[0].a[0].'jenkins.branch.BuildRetentionBranchProperty'[0]
.'buildDiscarder'[0].numToKeep[0].value() == 1
}

def 'call triggers'() {
when:
job.triggers {
Expand Down