I have several projects with very similar pipelines, e.g.:
pipeline {
agent any
environment {
JAVA_HOME = '...'
}
options {
timeout(time: 1, unit: 'HOURS')
}
parameters {
extendedChoice(
name: 'modules',
description: 'Modules to build',
type: 'PT_MULTI_SELECT',
value: 'app,common,data,ui',
defaultValue: 'app'
)
}
stages {
stage('Build') {
steps {
script {
def modules = params.modules.split(',')
withGradle {
if ('app' in modules) {
sh './gradlew app:assemble'
}
// other modules...
}
}
archiveArtifacts(artifacts: '**/build/outputs/**/*.jar', allowEmptyArchive: true)
}
}
}
}
I'd like to avoid copying this boilerplate to new projects, so creating a shared library sounds like a good approach.
However, every project has different modules. I'd like these to be configurable at the project level, e.g.:
// my-shared-library/vars/bootstrap.groovy
def call(body) {
pipeline {
agent any
environment {
JAVA_HOME = '...'
}
options {
timeout(time: 1, unit: 'HOURS')
}
stages {
stage('Build') {
steps {
script {
// get parameters from project
body()
def modules = params.modules.split(',')
withGradle {
for (module in modules) {
sh "./gradlew $module:assemble"
}
}
}
archiveArtifacts(artifacts: '**/build/outputs/**/*.jar', allowEmptyArchive: true)
}
}
}
}
}
Ideally I'd be able to use the shared library above like this in one of my projects:
library 'my-shared-library@master'
bootstrap {
parameters {
extendedChoice(
name: 'modules',
description: 'Modules to build',
type: 'PT_MULTI_SELECT',
value: 'app,common,data,ui',
defaultValue: 'app'
)
}
}
When I run this pipeline, the modules parameter is null. I also tried defining parameters
outside of the bootstrap
block like so:
library 'my-shared-library@master'
properties([
parameters([
extendedChoice(
name: 'Modules',
description: 'Modules to build',
type: 'PT_MULTI_SELECT',
value: 'app,common,data,ui',
defaultValue: 'app'
)
])
])
bootstrap()
This pipeline throws the following error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 13: unexpected token: extendedChoice @ line 13, column 13.
extendedChoice(
^
Is something like this possible? I'm struggling to find documentation for this use case, particularly regarding declarative pipelines, though I found a few examples that are pretty close: