Task element not showing in Stage in Delivery Pipeline View when inside script block

15 views
Skip to first unread message

Joseph Pratt

unread,
Oct 25, 2017, 4:43:30 PM10/25/17
to Delivery Pipeline Plugin
Hello all,

I have a question centered around using the "task" method in a script block in a declarative Jenkinsfile. What I'm attempting to do is create a DSL using Groovy where the Jenkinsfile is essentially a config file allowing a dev team to fill in string values and have the declarative pipeline build their projects behind the scenes.

Here's an example Jenkinsfile:
// Jenkinsfile
@Library('pipeline-library') _
project {
   commit {
       buildScript 'build.ps1'
   }
}

vars/project.groovy in the shared 'pipeline-library' would look like this:
// vars/project.groovy
import groovy.transform.Field
import com.foo.*
@Field Closure commitClosure

def call(body) {
   body.resolveStrategy = Closure.DELEGATE_FIRST
   body.delegate = this
   body()
// Declarative pipeline
   pipeline {
       agent any
       stages {
           stage('Commit') {
               steps {
                   script {
                       commitClosure()
                   }
               }
           }
       }
   }
}

def commit(Closure cl) {
cl.resolveStrategy = Closure.DELEGATE_FIRST
cl.delegate = commit
commitClosure = cl
}

and vars/commit.groovy would be:
// vars/commit.groovy
def call(body) {
   body.resolveStrategy = Closure.DELEGATE_FIRST
   body.delegate = this
   body()
}

def buildScript(String str) {
task 'Compile'
powershell str
}

However, since the commit closure is executed in the script block, the task is not showing up in the stage in the Delivery Pipeline view.

First, is there a better way I should be creating the DSL, and second, is it intended that tasks not show up in the stage view when executed from a script block?

Thanks in advanced!
Joseph

Joseph Pratt

unread,
Oct 26, 2017, 3:24:41 PM10/26/17
to Delivery Pipeline Plugin
So after further investigation, I found why it wasn't working as expected. The short version is, tasks execute directly in the call() block will not show up in the Delivery Pipeline view
e.g.
def call(body) {
body.resolveStrategy = Closure.DELEGATE_FIRST
body.resolveStrategy = this
// this won't show
task 'Compile'
 body()
}

However, if it is called by the resolution of the closure in another .groovy file, it will show as expected.

This may be a defect, or it may just be due to how Groovy handles closures. Either way, calling it in another .groovy file that is resolved via the previous closure works for me!

Thanks,
Joseph
Reply all
Reply to author
Forward
0 new messages