Home > Blockchain >  Create and use global stages and steps for declarative JenkinsFile in JenkinsLib
Create and use global stages and steps for declarative JenkinsFile in JenkinsLib

Time:07-13

I would like to have some global JenkinsFile that can be used as a lib for stages in any JenkinsFile we use. Specifically, stage that declares environment variables in the JenkinsFile that uses that JenkinsLib

We already use declarative JenkinsFile as a shared JenkinsLib, we have a lots of Gloabl JenkinsFiles that we use in all sort of Jobs. But in almost any of the global JenkinsFiles we have the same several stages. Like checkout, init, etc. And I would like to have some global stages lib file that I can use in any of the JenkinsFiles we create for our JenkinsLib

For example, specifically, we have an "init" stage that declares several ENV and Global params. So I created a "getGloabalStage.groovy", Like -

// Init stage
def initStage = "
            env.STARTED_BY = buildInfo.getStartedByUser()
            env.SVN_REVISION = shell.getCommandOutput(\"svn info --show-item last-changed-revision\")
            env.SVN_AUTHOR = scmExtras.getEmailsFromChangeList().join(\",\")
            env.PRODUCT_VERSION = versionHelper.getMainVersion()
            def timeInSeconds = (long) (currentBuild.startTimeInMillis / 1000)
            env.TEST_REPORT = \"TestsResults_${currentBuild.number}.xml\"
            currentBuild.description = \"${env.STARTED_BY} on ${env.NODE_NAME}, ${env.PRODUCT_VERSION} - r.${env.SVN_REVISION}\"
            buildInfo.printBuildInfo()
            "

// svn checkout stage
def svnCheckoutStage = "svn.checkout(\"${env.REPO_NAME}\",\"${env.BRANCH_NAME}\")"

// generate stage
def generateStage(stageRequested) {
    stageToRun = "${stageRequested}Stage"
    return {
        "echo \"Gloabl Stage ${stageRequested}\"
        ${stageToRun}
        "
    }
}

then I tried to use it in other JenkinsFile like

stages {
    stage('Init') {
        steps{
            script {
                getGloabalStage.generateStage("init")
            }
        }
    }
    stage ('Checkout'){
        steps{
            script {
                getGloabalStage.generateStage("svnCheckout")
            }
        }
    }

But the "init" stage won't work. I tried several option with " and \ but it didn't work

I also tried to put the "stage -> step -> script" in it, but we use declarative JenkinsFile so it didn't accept it in the calling jenkinsFile.

I also tried to use initStage as a function and call it, but also with no success.

Currently the error I get -

2: expecting anything but ''\n''; got it anyway @ line 2, column 63. = buildInfo.getStartedByUser()

So, bottom line, I need the way in a declarative JenkinsFile to have some global place where I can define global stages and steps that can declare global variables and define parameters in a JenkinsFile that include and execute it.

CodePudding user response:

You can try something like

def setVars() {
    env.STARTED_BY = buildInfo.getStartedByUser()
    env...
}

def svnCheckout() {
    svn.checkout("${env.REPO_NAME}", "${env.BRANCH_NAME}")
}

And call them

stages {
    stage('Init') {
        steps{
            script {
                getGloabalStage.setVars()
            }
        }
    }
    stage ('Checkout'){
        steps{
            script {
                getGloabalStage.svnCheckout()
            }
        }
    }
}
  • Related