Skip to content
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 @@ -35,7 +35,7 @@ class AgentDeclaration extends GenericPipelineDeclaration {
}

def docker(String image) {
this.docker = new DockerAgentDeclaration().with { it.image = image; it }
this.docker({ -> owner.image = image })
}

def docker(@DelegatesTo(strategy = DELEGATE_FIRST, value = DockerAgentDeclaration) Closure closure) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class GenericPipelineDeclaration {
static <T> T createComponent(Class<T> componentType, @DelegatesTo(strategy = DELEGATE_FIRST) Closure<T> closure) {
// declare componentInstance as final to prevent any multithreaded issues, since it is used inside closure
final def componentInstance = componentType.newInstance()
def rehydrate = closure.rehydrate(componentInstance, closure, componentInstance)
def rehydrate = closure.rehydrate(closure, componentInstance, closure.thisObject)
if (binding && componentInstance.hasProperty('binding') && componentInstance.binding != binding) {
componentInstance.binding = binding
}
Expand All @@ -24,20 +24,15 @@ abstract class GenericPipelineDeclaration {

static <T> T executeOn(@DelegatesTo.Target Object delegate,
@DelegatesTo(strategy = DELEGATE_FIRST) Closure<T> closure) {
if (closure) {
def cl = closure.rehydrate(delegate, delegate, delegate)
cl.resolveStrategy = DELEGATE_FIRST
return cl.call()
}
return null
return executeWith(delegate, closure)
}

static <T> T executeWith(@DelegatesTo.Target Object delegate,
@DelegatesTo(strategy = DELEGATE_FIRST) Closure<T> closure) {
if (closure) {
def cl = closure.rehydrate(delegate, delegate, delegate)
cl.resolveStrategy = DELEGATE_FIRST
return cl.call()
def rehydratedClosure = closure.rehydrate(closure, delegate, closure.thisObject)
rehydratedClosure.resolveStrategy = DELEGATE_FIRST
return rehydratedClosure.call()
}
return null
}
Expand Down Expand Up @@ -102,9 +97,9 @@ abstract class GenericPipelineDeclaration {
env.env = env
env.currentBuild = delegate.binding.currentBuild

def cl = this.environment.rehydrate(env, delegate, this)
cl.resolveStrategy = DELEGATE_FIRST
cl.call()
def rehydratedEnvClosure = this.environment.rehydrate(env, delegate, delegate)
rehydratedEnvClosure.resolveStrategy = DELEGATE_FIRST
rehydratedEnvClosure.call()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class StageDeclaration extends GenericPipelineDeclaration {
e.value.execute(delegate)
}
if(steps) {
Closure stageBody = { agent?.execute(delegate) } >> steps.rehydrate(delegate, this, this)
Closure stageBody = { agent?.execute(delegate) } >> steps.rehydrate(delegate, this, delegate)
Closure cl = { stage("$name", stageBody) }
executeWith(delegate, cl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class TestDeclarativePipeline extends DeclarativePipelineTest {
assertJobStatusSuccess()
}

@Test void jenkinsfile_thisObject() throws Exception {
def script = runScript('Declarative_thisObject_JenkinsFile')
printCallStack()
assertJobStatusSuccess()
assertCallStack().doesNotContain("This is a script?: false")
assertCallStack().contains("This is a script?: true")
}

@Test void jenkinsfile_failure() throws Exception {
helper.registerAllowedMethod('sh', [String.class], { String cmd ->
updateBuildStatus('FAILURE')
Expand Down
50 changes: 50 additions & 0 deletions src/test/jenkins/jenkinsfiles/Declarative_thisObject_JenkinsFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pipeline {

agent any

environment {
CC = testThis(this, 'gcc')
}

options {
buildDiscarder(logRotator(numToKeepStr: testThis(this, '10')))
}

triggers {
pollSCM(testThis(this, '*/5 * * * *'))
}

stages {
stage('Checkout') {
when {
environment name: 'CC', value: testThis(this, 'clang')
}
steps {
echo testThis(this, "Steps")
}
}

stage('build') {
agent { docker testThis(this, 'maven:3-alpine') }
options {
timeout(time: 20, unit: 'MINUTES')
}
steps {
withEnv(["GRADLE_HOME=${tool name: 'GRADLE_3', type: 'hudson.plugins.gradle.GradleInstallation'}"]) {
echo testThis(this, "In env")
}
}
}
}

post {
always {
echo testThis(this, 'pipeline unit tests completed')
}

}
}

String testThis(Script scriptRef, String text){
return "$text: This is a script?: ${this == scriptRef}"
}