In Jenkins scripted pipeline we are able to create methods and can call them. Is it possible also in the Jenkins declarative pipeline? And how?
Newer versions of the declarative pipelines support this, while this was not possible before (~mid 2017). You can just declare functions as you'd expect it from a groovy script:
pipeline < agent any stages < stage('Test') < steps < whateverFunction() >> > > void whateverFunction() < sh 'ls /' >
answered Dec 4, 2017 at 10:44
StephenKing StephenKing
37.3k 11 11 gold badges 87 87 silver badges 115 115 bronze badges
I was able to get this to work, however it removes the ability to edit the pipeline in the Blue Ocean GUI editor. Do you know of a way around that? I do most of the editing in the Jenkinsfile directly via text editor, but it is nice to be able to stub things out quickly in the UI. I got around that by commenting out the function temporarily.
Commented Mar 18, 2018 at 21:03I'm trying to define a function that defines a stage but this doesn't seem to work (syntax error expected 'stage'). Do they only support running functions defining steps?
Commented Jun 1, 2018 at 0:16This is really cool. However, where is this documented? I fail to find any resource describing this ability. I only find documentation how to import external, shared groovy libraries. I only find this SO answer for embedding them in the Jenkinsfile itself, which is really useful. Jenkins issue tracker with proposals received wontfix status, e.g.: issues.jenkins-ci.org/browse/JENKINS-41396
Commented Aug 14, 2018 at 10:09@WillBrode i am also interested in having functions to define/configure an entire stage. It is my understanding that the declarative pipeline parser simply doesn't allow those type of syntax flexibility: issues.jenkins-ci.org/browse/JENKINS-49135 Have you had any success with this?
Commented Jan 16, 2020 at 16:58@carlos I asked that question and there are some answers here: stackoverflow.com/questions/50634592/…