Jenkins共享库 - 如何为声明性管道定义公共选项

时间:2017-12-08 10:54:59

标签: jenkins groovy closures jenkins-pipeline jenkins-declarative-pipeline

我在声明性管道中有许多类似的配置,例如代理,工具,选项或帖子部分。是否有任何选项以某种方式定义这些选项,以便单个作业只需定义步骤(可能来自共享库)?

有一个description at "Defining a more stuctured DSL",其中有类似于我想要实现的内容,但这似乎适用于脚本管道。

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']]
        }
    }
}

实际上,我试过类似that的东西,尝试将一个闭包传递给管道,但这似乎不起作用。可能如果它有效,就会有一些关于如何做的文档。

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']]
            }
        }
    }
}

并以某种方式称呼它:

library 'my-library@master'

callJob{

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

1 个答案:

答案 0 :(得分:1)

我创建了一个完整的流程

//SbtFlowDockerLarge.groovy
def call(int buildTimeout,String sbtVersion,List<String> fbGoals, List<String> masterGoals ){

    def fbSbtGoals = fbGoals.join ' '
    def masterSbtGoals = masterGoals.join ' '

    pipeline {
        agent { label DPgetLabelDockerLarge() }
        options {
            timestamps()
            timeout(time: buildTimeout, unit: 'MINUTES')
            disableConcurrentBuilds()
            buildDiscarder(logRotator(daysToKeepStr: '35'))
        }

        stages {
            stage('Prepare') {
                steps {
                    setGitHubBuildStatus("Running Build", "PENDING")
                    echo "featureTask :  ${fbSbtGoals}"
                    echo "masterTask :  ${masterSbtGoals}"
                }
            }
            stage('Build') {
                when {
                    not { branch 'master' }
                }
                steps {
                    sbtTask tasks: "${fbSbtGoals}", sbtVersion: sbtVersion
                }
            }
            stage('Deploy') {
                when {
                    branch 'master'
                }
                environment {
                    TARGET_ENVIRONMENT='prod'
                }
                steps {
                    sbtTask tasks: "${masterSbtGoals}", sbtVersion: sbtVersion
                }
            }
        }
        post {
            success {
                setGitHubBuildStatus("Build complete", "SUCCESS")
            }
            failure {
                setGitHubBuildStatus("Build complete", "FAILED")
            }
            always {
                junit allowEmptyResults: true, testResults: '**/target/test-reports/*.xml'
                dockerCleanup()
            }
        }
    }
}

这是Jenkinsfile

@Library('aol-on-jenkins-lib') _
def buildTimeout = 60
def sbtVersion = 'sbt-0.13.11'
OathSbtFlowDockerLarge (buildTimeout, sbtVersion,['clean test-all'],['clean test-all publish'])