gpt4 book ai didi

include - 如何 "include"另一个文件作为 Jenkins 管道定义的一部分

转载 作者:行者123 更新时间:2023-12-04 15:25:13 24 4
gpt4 key购买 nike

我们有一个大型项目,它有多个单独的声明性管道文件定义。这用于从单个代码库构建不同的应用程序和安装程序。

现在,所有这些文件都包含一大块“代码”,用于生成电子邮件正文和 JIRA 更新消息。例子:

// Get a JIRA's to add Comments to
// Return map of JIRA id to comment text from all commits for that JIRA
@NonCPS
def getJiraMap() {
a bunch of stuff
return jiraset
}

// Get the body text for the emails
def getMailBody1() {
return "See: ${BUILD_URL}\n\nChanges:\n" + getChangeString() + "\n" + testStatuses()
}

etc...

我想做的是将所有这些常用方法放在一个单独的文件中,所有其他管道文件都可以包含该文件。这看起来应该很容易,但我发现的所有示例似乎都相当复杂,涉及单独的 SCM - 这不是我想要的。

更新:

通过该链接中给出的各种建议,我创建了以下文件 - BuildTools.groovy:请注意,此文件与使用它的 jenkins 管道文件位于同一目录中。
import hudson.tasks.test.AbstractTestResultAction
import hudson.model.Actionable

Class BuildTools {

// Get a JIRA's to add Comments to
// Return map of JIRA id to comment text from all commits for that JIRA
@NonCPS
def getJiraMap() {
def jiraset = [:]
.. whole bunch of stuff ..

这是我尝试过的各种方法和结果。
File sourceFile = new File("./AutomatedBuild/BuildTools.groovy");
Class gcl = new GroovyClassLoader(getClass().getClassLoader()).parseClass(sourceFile);
GroovyObject bt = (GroovyObject) gcl.newInstance();

Fails with:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method java.lang.Class getClassLoader
evaluate(new File("./AutomatedBuild/BuildTools.groovy"))
def bt = new BuildTools()

Fails with:
15:29:07 WorkflowScript: 8: unable to resolve class BuildTools
15:29:07 @ line 8, column 10.
15:29:07 def bt = new BuildTools()
15:29:07 ^
import BuildTools
def bt = new BuildTools()

Fails with:
15:35:58 WorkflowScript: 16: unable to resolve class BuildTools (note that BuildTools.groovy is in the same folder as this script)
15:35:58 @ line 16, column 1.
15:35:58 import BuildTools
15:35:58 ^
GroovyShell shell = new GroovyShell()
def bt = shell.parse(new File("./AutomatedBuild/BuildTools.groovy"))

Fails with:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new groovy.lang.GroovyShell

最佳答案

这是回答 here

evaluate(new File("../tools/Tools.groovy"))

关于include - 如何 "include"另一个文件作为 Jenkins 管道定义的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62398222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com