Home > Net >  Jenkins lts : Problem with groovy script path in jenkins workspace
Jenkins lts : Problem with groovy script path in jenkins workspace

Time:06-09

I just upgraded the jenkins version of our development factory to lts. We were previously in an old 2.x version

In fact we have a stage to load all scripts :

stage('Load Utilities Scripts') {
    checkout scm
    echo "${job_name}"
    modules.messages = load "${jenkins_home}/workspace/${job_name}@script/pipelines/utils/slack_functions.groovy"
    modules.ansible = load "${jenkins_home}/workspace/${job_name}@script/pipelines/utils/ansible_functions.groovy"
    modules.jenkins = load "${jenkins_home}/workspace/${job_name}@script/pipelines/utils/jenkins_functions.groovy"
    modules.pic_env = load "${jenkins_home}/workspace/${job_name}@script/pipelines/utils/pic_env.groovy"
    modules.maven = load "${jenkins_home}/workspace/${job_name}@script/pipelines/utils/maven_functions.groovy"
    modules.git = load "${jenkins_home}/workspace/${job_name}@script/pipelines/utils/git_functions.groovy"
}

it worked perfectly well except that now the scripts are positioned in

    /var/jenkins_home/workspace/SAGES2_BUILD_AUTO@script/357f3acc22b0fbb05a66735d46d7d7eb950a2e836ab3762b1905784bc550ee5e on builtin
    /var/jenkins_home/workspace/SAGES2_BUILD_AUTO on builtin

I guess it's a security evolution (i know groovy script plugin on jenkins are often deprecated but it's another problem).

How can i get this string : 357f3acc22b0fbb05a66735d46d7d7eb950a2e836ab3762b1905784bc550ee5e ? Is there any vars like job_name or jenkins_home ? Who generates this token and where ? Can i have the hand on this string ? What does it represent ?

CodePudding user response:

You can get the current directory and build the absolute path by appending the relative part.

modules.messages = load pwd()   "@script/pipelines/utils/slack_functions.groovy"

Or something like this

modules.messages = load "${WORKSPACE}@script/pipelines/utils/slack_functions.groovy"

It seems this has being introduced as a security improvement. So you will have to work around this like below.

script {
    // Locate the jenkins folder
    // This is done because there is a new sub-folder (like : 17a4ba1ed1ce777b18c5...)
    git_jenkins_folder = sh (
        script: "find "   WORKSPACE   "@script -type d -name 'jenkins' -printf '%T@ %Tc %p\n' | sort -rn | head -1 | cut -d' ' -f9",
        returnStdout: true
    ).trim()
    // Load the groovy methods in groovy files
    slack = load git_jenkins_folder   "/pipelines/utils/slack_functions.groovy"
}

Credit: Why does Jenkins creates a subfolder within the workspace@script folder to checkout git code instead of the workspace@script itself?

  • Related