Jenkins管道-如何在不首先调用node()的情况下加载Jenkinsfile? [英] Jenkins pipeline - how to load a Jenkinsfile without first calling node()?

查看:107
本文介绍了Jenkins管道-如何在不首先调用node()的情况下加载Jenkinsfile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常独特的设置,在该设置中,我需要能够动态加载生活在我正在构建的src之外的Jenkinsfiles. Jenkinsfiles本身通常调用node(),然后执行一些构建步骤.这会导致不必要地吞噬多个执行程序,因为我需要已经调用node()才能使用加载步骤来运行Jenkinsfile,或者如果我将Jenkinsfile读取为字符串并执行它就可以执行常规操作.

I have a somewhat unique setup where I need to be able to dynamically load Jenkinsfiles that live outside of the src I'm building. The Jenkinsfiles themselves usually call node() and then some build steps. This causes multiple executors to be eaten up unnecessarily because I need to have already called node() in order to use the load step to run a Jenkinsfile, or to execute the groovy if I read the Jenkinsfile as a string and execute it.

我今天在工作界面中拥有什么:

What I have in the job UI today:

@Library(value='myGlobalLib@head', changelog=fase) _

node{
    load "${JENKINSFILES_ROOT}/${PROJECT_NAME}/Jenkinsfile"
}

加载的Jenkins文件通常也调用node().例如:

The Jenkinsfile that's loaded usually also calls node(). For example:

node('agent-type-foo'){
    someBuildFlavor{
        buildProperty = "some value unique to this build"
        someConfig = ["VALUE1", "VALUE2", "VALUE3"]
        runTestTarget = true
    }
}

这导致在管道运行期间消耗2个执行程序.理想情况下,我无需先调用node()就加载Jenkinsfiles,但是每当尝试尝试时,我都会收到一条错误消息,指出:

This causes 2 executors to be consumed during the pipeline run. Ideally, I load the Jenkinsfiles without first calling node(), but whenever I try, I get an error message stating:

"Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node"

在没有hudson.FilePath上下文的情况下,是否可以加载Jenkinsfile或执行groovy?我似乎在文档中找不到任何内容.我现在要预处理Jenkinsfile,以删除它们对node()的初始调用,并使用Jenkinsfile所使用的值调用node(),然后加载文件的其余部分,但是,这也有点令我感到脆弱.

Is there any way to load a Jenkinsfile or execute groovy without first having hudson.FilePath context? I can't seem to find anything in the doc. I'm at the point where I'm going to preprocess the Jenkinsfiles to remove their initial call to node() and call node() with the value the Jenkinsfile was using, then load the rest of the file, but, that's somewhat too brittle for me to be happy with.

推荐答案

在使用load步骤时,Jenkins将评估文件.您可以将Jenkinsfile的逻辑包装到一个函数中(在我的示例中命名为run()),以使其能够加载但不会自动运行.

When using load step Jenkins evaluates the file. You can wrap your Jenkinsfile's logics into a function (named run() in my example) so that it will load but not run automatically.

def run() {
    node('agent-type-foo'){
        someBuildFlavor{
            buildProperty = "some value unique to this build"
            someConfig = ["VALUE1", "VALUE2", "VALUE3"]
            runTestTarget = true
        }
    }
}

// This return statement is important in the end of Jenkinsfile
return this 

从您的工作脚本中调用它,如下所示:

Call it from your job script like this:

def jenkinsfile
node{
    jenkinsfile = load "${JENKINSFILES_ROOT}/${PROJECT_NAME}/Jenkinsfile"
}
jenkinsfile.run()

这样,不再有嵌套的node块,因为在调用run()函数之前,第一个块已关闭.

This way there is no more nested node blocks because the first gets closed before run() function is called.

这篇关于Jenkins管道-如何在不首先调用node()的情况下加载Jenkinsfile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆