Home:ALL Converter>Jenkins - set options in a shared library for all pipelines that use the shared library

Jenkins - set options in a shared library for all pipelines that use the shared library

Ask Time:2022-10-24T15:16:50         Author:cyau

Json Formatter

I have a bunch of repositories which use (parts of) the same Jenkins shared library for running tests, docker builds, etc. So far the shared library has greatly reduced the maintenance costs for these repos.

However, it turned out that basically all pipelines use the same set of options, e.g.:

@Library("myExample.jenkins.shared.library") _
import org.myExample.Constants

pipeline {
    options {
        disableConcurrentBuilds()
        parallelsAlwaysFailFast()
    }

    agent {
        label 'my-label'
    }

    stages {
        stage {
            runThisFromSharedLibrary(withParameter: "foo")
            runThatFromSharedLibrary(withAnotherParameter: "bar")
            ...

...

In other words, I need to copy-and-paste the same option snippets in any new specific pipeline that I create. Also, this means that I need to edit separately each Jenkinsfile (along with any peer-review processes we use internally) when I decide to change the set of options.

I'd very much like to remove this maintenance overhead somehow. How can I delegate the option-setting to a shared library, or otherwise configure the required options for all pipelines at once?

Author:cyau,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74177703/jenkins-set-options-in-a-shared-library-for-all-pipelines-that-use-the-shared
yy