Reputation: 8200
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: '[email protected]', 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: '[email protected]', recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider'], [$class: 'DevelopersRecipientProvider']]
}
}
}
}
and calling it somehow like this:
library 'my-library@master'
callJob{
stage('do something') {
steps {
doSomething()
}
}
}
Upvotes: 3
Views: 2459
Reputation: 2636
I created a full flow
//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()
}
}
}
}
and here is the 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'])
Upvotes: 3