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

bugfix(declarative): Stage with any 2 of parallel, steps, matrix, stages should throw error #506

Merged
merged 2 commits into from
Mar 21, 2022
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 @@ -42,9 +42,24 @@ class StageDeclaration extends GenericPipelineDeclaration {

def execute(Object delegate) {
String name = this.name
def actions = 0
if(parallel) {
actions++
}
if(stages.size()>0) {
actions++
}
if(steps) {
actions++
}
if (actions > 1 ) {
throw new IllegalArgumentException ("""Only one of "matrix", "parallel", "stages", or "steps" allowed for stage "${name}" """)
}

this.options.each {
executeOn(delegate, it)
}

if(parallel) {
parallel.execute(delegate)
}
Expand All @@ -56,15 +71,20 @@ class StageDeclaration extends GenericPipelineDeclaration {

if (!when || when.execute(delegate)) {
super.execute(delegate)

// TODO handle credentials

Closure stageBody = { agent?.execute(delegate) }
Closure cl = { stage("$name", stageBody) }
if(steps) {
stageBody = stageBody >> steps.rehydrate(delegate, this, delegate)
}
executeWith(delegate, cl)

this.stages.entrySet().forEach { e ->
e.value.execute(delegate)
}
if(steps) {
Closure stageBody = { agent?.execute(delegate) } >> steps.rehydrate(delegate, this, delegate)
Closure cl = { stage("$name", stageBody) }
executeWith(delegate, cl)
}

if (post) {
this.post.execute(delegate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,30 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
printCallStack()
assertCallStack().contains('writeFile({file=messages/messages.msg, text=text})')
}

@Test void test_agent_in_stage_with_no_steps() {
runScript("AgentStageNoSteps_Jenkinsfile")
assertJobStatusSuccess()

assertCallStack().contains('post A')
assertCallStack().contains('post B')
assertCallStack().contains('post C')

assertCallStack().contains('echo(A)')
assertCallStack().contains('echo(C)')
assertCallStack().contains('stage(A, groovy.lang.Closure)')
assertCallStack().contains('stage(C, groovy.lang.Closure)')
assertCallStack().contains('labelA')
assertCallStack().contains('labelC')
// assertion bellow would fail
assertCallStack().contains('stage(B, groovy.lang.Closure)')
assertCallStack().contains('labelB')

}

@Test(expected = IllegalArgumentException)
void test_stage_and_steps() {
runScript("StageAndSteps_Jenkinsfile")
}
}

25 changes: 25 additions & 0 deletions src/test/jenkins/jenkinsfiles/AgentStageNoSteps_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pipeline {
agent none
stages {
stage('A') {
agent { label 'labelA' }
post { always {echo 'post A'}}
steps {
echo 'A'
}
}
stage('B') {
agent {label 'labelB'}
post { always {echo 'post B'}}
stages {
stage('C') {
agent {label 'labelC'}
post { always {echo 'post C'}}
steps {
echo 'C'
}
}
}
}
}
}
19 changes: 19 additions & 0 deletions src/test/jenkins/jenkinsfiles/StageAndSteps_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pipeline {
agent none
stages {
stage('B') {
agent {label 'labelB'}
post { always {echo 'post B'}}
steps { echo 'B' }
stages {
stage('C') {
agent {label 'labelC'}
post { always {echo 'post C'}}
steps {
echo 'C'
}
}
}
}
}
}