3

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

在“Defining a more ststructured 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']]
        }
    }
}

实际上,我尝试过类似方法,试图将闭包传递给管道,但这似乎不起作用。可能如果它有效,有一些关于如何做到这一点的文档。

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()
                }
          }  
  }
4

1 回答 1

3

我创建了一个完整的流程

//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()
            }
        }
    }
}

这是詹金斯文件

@Library('aol-on-jenkins-lib') _
def buildTimeout = 60
def sbtVersion = 'sbt-0.13.11'
OathSbtFlowDockerLarge (buildTimeout, sbtVersion,['clean test-all'],['clean test-all publish'])
于 2018-07-09T07:16:37.750 回答