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

Added support for beforeAgent property in when statements #680

Merged
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 @@ -21,6 +21,7 @@ class WhenDeclaration extends GenericPipelineDeclaration {
Closure<Boolean> expression
String envName
String envValue
Boolean beforeAgent

def allOf(@DelegatesTo(strategy = DELEGATE_FIRST, value = AllOfDeclaration) Closure closure) {
this.allOf = createComponent(AllOfDeclaration, closure)
Expand Down Expand Up @@ -66,6 +67,10 @@ class WhenDeclaration extends GenericPipelineDeclaration {
}
}

def beforeAgent (Boolean arg) {
this.beforeAgent = arg
}

def buildingTag () {
this.buildingTag = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,24 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
assertJobStatusSuccess()
}

@Test void when_nested_when_beforeAgent_with_matching_branch_expression() throws Exception {
addEnvVar('BRANCH_NAME', 'main')
runScript('Nested_BeforeAgent_Jenkinsfile')
printCallStack()
assertCallStack().contains('Executing nested when beforeAgent expression')
assertCallStack().contains('Executing on agent [label:beforeAgent-testLabel]')
assertJobStatusSuccess()
}

@Test void when_nested_when_beforeAgent_with_nonmatching_branch_expression() throws Exception {
addEnvVar('BRANCH_NAME', 'dev')
runScript('Nested_BeforeAgent_Jenkinsfile')
printCallStack()
assertCallStack().contains('Skipping stage Example nested when beforeAgent expression')
assertCallStack().doesNotContain('Executing on agent [label:beforeAgent-testLabel]')
assertJobStatusSuccess()
}

@Test void when_branch_using_explicit_equals_comparator() throws Exception {
addEnvVar('BRANCH_NAME', 'develop')
runScript('Branch_Jenkinsfile')
Expand Down
22 changes: 22 additions & 0 deletions src/test/jenkins/jenkinsfiles/Nested_BeforeAgent_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pipeline {
agent any
stages {
stage('Example Build') {
steps {
echo 'Hello World'
}
}
stage('Example nested when beforeAgent expression') {
agent {
label "beforeAgent-testLabel"
}
when {
beforeAgent true
branch 'main'
}
steps {
echo 'Executing nested when beforeAgent expression'
}
}
}
}
Loading