简体   繁体   English

Jenkins lts:jenkins 工作区中 groovy 脚本路径的问题

[英]Jenkins lts : Problem with groovy script path in jenkins workspace

I just upgraded the jenkins version of our development factory to lts.我刚刚将我们开发工厂的jenkins版本升级到lts。 We were previously in an old 2.x version我们之前使用的是旧的 2.x 版本

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).我想这是一个安全演进(我知道 jenkins 上的 groovy 脚本插件经常被弃用,但这是另一个问题)。

How can i get this string : 357f3acc22b0fbb05a66735d46d7d7eb950a2e836ab3762b1905784bc550ee5e ?我怎样才能得到这个字符串:357f3acc22b0fbb05a66735d46d7d7eb950a2e836ab3762b1905784bc550ee5e? Is there any vars like job_name or jenkins_home ?有没有像 job_name 或 jenkins_home 这样的变量? Who generates this token and where ?谁生成这个令牌,在哪里生成? Can i have the hand on this string ?我可以把手放在这根弦上吗? What does it represent ?它代表什么?

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?信用: 为什么 Jenkins 在 workspace@script 文件夹中创建一个子文件夹来检查 git 代码而不是 workspace@script 本身?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM