3

I have many similar configs in declarative pipelines, like agent, tools, options, or post section. Is there any option to define those option somehow, so that an individual job has only to define the steps (which may come from a shared library)?

There is a description at "Defining a more stuctured DSL", where there is something similar to that what I want to achieve, but this seems to apply to scripted pipelines.

pipeline {
    agent {
        label 'somelabel'
    }
    tools {
        jdk 'somejdk'
        maven 'somemaven'
    }
    options {
        timeout(time: 2, unit: 'HOURS')
    }

    stages {

         stage('do something') {
            steps {
                    doSomething()
            }
         }            

    }
    post {
        failure {
            emailext body: '${DEFAULT_CONTENT}', subject: '${DEFAULT_SUBJECT}',
                    to: 'some@mail.com', recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider'], [$class: 'DevelopersRecipientProvider']]
        }
    }
}

Actually, I tried something like that, trying to pass a closure to the pipeline, but this does not seem to work. Probably if it worked, there was some documentation on how to do it.

def call(stageClosure) {
    pipeline {
        agent {
            label 'somelabel'
        }
        tools {
            jdk 'somejdk'
            maven 'somemaven'
        }
        options {
            timeout(time: 2, unit: 'HOURS')
        }

        stages {

             stageClosure()           

        }
        post {
            failure {
                emailext body: '${DEFAULT_CONTENT}', subject: '${DEFAULT_SUBJECT}',
                        to: 'some@mail.com', recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider'], [$class: 'DevelopersRecipientProvider']]
            }
        }
    }
}

and calling it somehow like this:

library 'my-library@master'

callJob{

     stage('do something') {
          steps {
                doSomething()
                }
          }  
  }
4

1 に答える 1