Skip to content

Commit dcbddce

Browse files
committed
Rehydrate components with thisObject pointing to the script. This will make this.sh work and make this in the pipeline script passable to methods.
1 parent aba88e7 commit dcbddce

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

src/main/groovy/com/lesfurets/jenkins/unit/declarative/AgentDeclaration.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AgentDeclaration extends GenericPipelineDeclaration {
3535
}
3636

3737
def docker(String image) {
38-
this.docker = new DockerAgentDeclaration().with { it.image = image; it }
38+
this.docker({ -> owner.image = image })
3939
}
4040

4141
def docker(@DelegatesTo(strategy = DELEGATE_FIRST, value = DockerAgentDeclaration) Closure closure) {

src/main/groovy/com/lesfurets/jenkins/unit/declarative/GenericPipelineDeclaration.groovy

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class GenericPipelineDeclaration {
1414
static <T> T createComponent(Class<T> componentType, @DelegatesTo(strategy = DELEGATE_FIRST) Closure<T> closure) {
1515
// declare componentInstance as final to prevent any multithreaded issues, since it is used inside closure
1616
final def componentInstance = componentType.newInstance()
17-
def rehydrate = closure.rehydrate(componentInstance, closure, componentInstance)
17+
def rehydrate = closure.rehydrate(closure, componentInstance, closure.thisObject)
1818
if (binding && componentInstance.hasProperty('binding') && componentInstance.binding != binding) {
1919
componentInstance.binding = binding
2020
}
@@ -24,20 +24,15 @@ abstract class GenericPipelineDeclaration {
2424

2525
static <T> T executeOn(@DelegatesTo.Target Object delegate,
2626
@DelegatesTo(strategy = DELEGATE_FIRST) Closure<T> closure) {
27-
if (closure) {
28-
def cl = closure.rehydrate(delegate, delegate, delegate)
29-
cl.resolveStrategy = DELEGATE_FIRST
30-
return cl.call()
31-
}
32-
return null
27+
return executeWith(delegate, closure)
3328
}
3429

3530
static <T> T executeWith(@DelegatesTo.Target Object delegate,
3631
@DelegatesTo(strategy = DELEGATE_FIRST) Closure<T> closure) {
3732
if (closure) {
38-
def cl = closure.rehydrate(delegate, delegate, delegate)
39-
cl.resolveStrategy = DELEGATE_FIRST
40-
return cl.call()
33+
def rehydratedClosure = closure.rehydrate(closure, delegate, closure.thisObject)
34+
rehydratedClosure.resolveStrategy = DELEGATE_FIRST
35+
return rehydratedClosure.call()
4136
}
4237
return null
4338
}
@@ -102,9 +97,9 @@ abstract class GenericPipelineDeclaration {
10297
env.env = env
10398
env.currentBuild = delegate.binding.currentBuild
10499

105-
def cl = this.environment.rehydrate(env, delegate, this)
106-
cl.resolveStrategy = DELEGATE_FIRST
107-
cl.call()
100+
def rehydratedEnvClosure = this.environment.rehydrate(env, delegate, delegate)
101+
rehydratedEnvClosure.resolveStrategy = DELEGATE_FIRST
102+
rehydratedEnvClosure.call()
108103
}
109104
}
110105

src/main/groovy/com/lesfurets/jenkins/unit/declarative/StageDeclaration.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class StageDeclaration extends GenericPipelineDeclaration {
6161
e.value.execute(delegate)
6262
}
6363
if(steps) {
64-
Closure stageBody = { agent?.execute(delegate) } >> steps.rehydrate(delegate, this, this)
64+
Closure stageBody = { agent?.execute(delegate) } >> steps.rehydrate(delegate, this, delegate)
6565
Closure cl = { stage("$name", stageBody) }
6666
executeWith(delegate, cl)
6767
}

src/test/groovy/com/lesfurets/jenkins/unit/declarative/TestDeclarativePipeline.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
2323
assertJobStatusSuccess()
2424
}
2525

26+
@Test void jenkinsfile_thisObject() throws Exception {
27+
def script = runScript('Declarative_thisObject_JenkinsFile')
28+
printCallStack()
29+
assertJobStatusSuccess()
30+
assertCallStack().doesNotContain("This is a script?: false")
31+
assertCallStack().contains("This is a script?: true")
32+
}
33+
2634
@Test void jenkinsfile_failure() throws Exception {
2735
helper.registerAllowedMethod('sh', [String.class], { String cmd ->
2836
updateBuildStatus('FAILURE')
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
pipeline {
2+
3+
agent any
4+
5+
environment {
6+
CC = testThis(this, 'gcc')
7+
}
8+
9+
options {
10+
buildDiscarder(logRotator(numToKeepStr: testThis(this, '10')))
11+
}
12+
13+
triggers {
14+
pollSCM(testThis(this, '*/5 * * * *'))
15+
}
16+
17+
stages {
18+
stage('Checkout') {
19+
when {
20+
environment name: 'CC', value: testThis(this, 'clang')
21+
}
22+
steps {
23+
echo testThis(this, "Steps")
24+
}
25+
}
26+
27+
stage('build') {
28+
agent { docker testThis(this, 'maven:3-alpine') }
29+
options {
30+
timeout(time: 20, unit: 'MINUTES')
31+
}
32+
steps {
33+
withEnv(["GRADLE_HOME=${tool name: 'GRADLE_3', type: 'hudson.plugins.gradle.GradleInstallation'}"]) {
34+
echo testThis(this, "In env")
35+
}
36+
}
37+
}
38+
}
39+
40+
post {
41+
always {
42+
echo testThis(this, 'pipeline unit tests completed')
43+
}
44+
45+
}
46+
}
47+
48+
String testThis(Script scriptRef, String text){
49+
return "$text: This is a script?: ${this == scriptRef}"
50+
}

0 commit comments

Comments
 (0)