是否可以将外部groovy脚本加载到groovy post build插件中,而不是将脚本内容粘贴到每个作业中?我们有大约200个工作岗位,因此更新所有工作相当费时。我知道我可以编写一个脚本来直接更新配置文件(如本文所示:Add Jenkins Groovy Postbuild step to all jobs),但这些作业全天候运行,因此当我重新启动Jenkins或重新加载配置时找到一个窗口是有问题的。
谢谢!
答案 0 :(得分:3)
更新:这就是我真正解决的问题:https://issues.jenkins-ci.org/browse/JENKINS-21480
“我可以通过执行以下操作来做到这一点。在”Groovy脚本“框中输入这些代替脚本:”
// Delegate to an external script
// The filename must match the class name
import JenkinsPostBuild
def postBuild = new JenkinsPostBuild(manager)
postBuild.run()
“然后在”Additional groovy classpath“框中输入该文件的路径。”
答案 1 :(得分:2)
将以下内容放在“Groovy脚本:”字段中:
evaluate(new File(“... groovy script file name ...”));
此外,您可能想要更进一步。 如果脚本名称或路径发生变化怎么办 使用Template plugin,您可以创建一个“模板”作业,在那里定义对groovy脚本(上面的行)的调用,并在需要它的所有作业中添加称为“使用另一个项目的发布者”的后构建操作引用此模板项目。
答案 2 :(得分:1)
我们以下列方式进行。
我们有一个文件c:\somepath\MyScriptLibClass.groovy
(Jenkins可以访问),它包含一个groovy类MyScriptLibClass的代码。该类包含许多功能,旨在表现为静态方法(稍后将混合使用)。
我们在sytem groovy和postbuild groovy步骤的开头包含这个函数编写以下语句:
[ // include lib scripts
'MyScriptLibClass'
].each{ this.metaClass.mixin(new GroovyScriptEngine('c:\\somepath').loadScriptByName(it+'.groovy')) }
这可能看起来有点难看,但你只需要为脚本编写一次。您可以包含多个脚本,也可以在库类之间使用继承。
在这里,您可以看到库类中的所有方法都混合在当前脚本中。所以,如果你的班级如下:
class MyScriptLibClass {
def setBuildName( String str ){
binding?.variables['manager'].build.displayName = str
}
}
在Groovy Postbuild中你可以写:
[ // include lib scripts
'MyScriptLibClass'
].each{ this.metaClass.mixin(new GroovyScriptEngine('c:\\somepath').loadScriptByName(it+'.groovy')) }
setBuildName( 'My Greatest Build' )
它会改变你当前版本的名称。
还有其他方法可以加载外部groovy类,没有必要使用混合。例如,你可以看看Compiling and using Groovy classes from Java at runtime?
答案 3 :(得分:1)
我是如何解决这个问题的:
使用以下内容创建文件$ JENKINS_HOME / scripts / PostbuildActions.groovy:
public class PostbuildActions {
void setBuildName(Object manager, String str ){
binding?.variables['manager'].build.displayName = str
}
}
在这种情况下,在Groovy Postbuild中你可以写:
File script = new File("${manager.envVars['JENKINS_HOME']}/scripts/PostbuildActions.groovy")
Object actions = new GroovyClassLoader(getClass().getClassLoader()).parseClass(script).newInstance();
actions.setBuildName(manager, 'My Greatest Build');
答案 4 :(得分:1)
如果您希望在您的代码存储库中安装Groovy脚本,并将其加载到工作区中的构建/测试从属服务器上,那么您需要注意Groovy Postbuild在主服务器上运行。
对我们来说,主服务器是Unix服务器,而构建/测试从服务器是本地网络上的Windows PC。因此,在使用脚本之前,我们必须打开从主服务器到从服务器的通道,并使用FilePath到该文件。
以下为我们工作:
// Get an Instance of the Build object, and from there
// the channel from the Master to the Workspace
build = Thread.currentThread().executable
channel = build.workspace.channel;
// Open a FilePath to the script
fp = new FilePath(channel, build.workspace.toString() + "<relative path to the script in Unix notation>")
// Some have suggested that the "Not NULL" check is redundant
// I've kept it for completeness
if(fp != null)
{
// 'Evaluate' requires a string, so read the file contents to a String
script = fp.readToString();
// Execute the script
evaluate(script);
}
答案 5 :(得分:1)
我刚刚面临同样的任务,并尝试使用@Blaskovicz方法。 不幸的是,它对我不起作用,但我找到了升级代码here (Zach Auclair)
发布此处稍作修改:
//imports
import hudson.model.*
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
import java.io.File;
// define git file
def postBuildFile = manager.build.getEnvVars()["WORKSPACE"] + "/Jenkins/SimpleTaskPostBuildReporter.GROOVY"
def file = new File(postBuildFile)
// load custom class from file
Class groovy = this.class.classLoader.parseClass(file);
// create custom object
GroovyObject groovyObj = (GroovyObject) groovy.newInstance(manager);
// do report
groovyObj.report();
class SimpleTaskPostBuildReporter {
def manager
public SimpleTaskPostBuildReporter(Object manager){
if(manager == null) {
throw new RuntimeException("Manager object can't be null")
}
this.manager = manager
}
public def report() {
// do work with manager object
}
}
答案 6 :(得分:0)
我没有完全尝试过这个。
您可以尝试Jenkins Job DSL plugin,它允许您使用Groovy DSL从jenkins内部重建作业,并直接从wiki
支持构建常规步骤Groovy Postbuild
构建后执行Groovy脚本。
groovyPostBuild(字符串脚本,行为行为= Behavior.DoNothing) 参数:
script构建后要执行的Groovy脚本。看插件 有关可以做什么的详细信息的页面。行为可选。如果是脚本 失败,允许您将构建标记为失败,不稳定或执行 没有。行为参数使用枚举,目前有三个 值:DoNothing,MarkUnstable和MarkFailed。
示例:
这个例子将运行一个groovy脚本,打印hello,world和if 失败,它不会影响构建的状态:
groovyPostBuild('println "hello, world"') This example will run a groovy script, and if that fails will mark the build as failed: groovyPostBuild('// some groovy script', Behavior.MarkFailed) This example will run a groovy script, and if that fails will mark the
构建为不稳定:
groovyPostBuild('// some groovy script', Behavior.MarkUnstable) (Since 1.19)
有一个工具可以使用模板作业(这是我没有尝试过的),这可能是作业本身所以你只需要添加后期构建步骤。如果您不使用模板,则需要重新编码整个项目。
我的方法是让脚本从头开始重新生成或创建所有作业,这样我就不必多次应用相同的升级。重新生成的作业保留其构建历史记录
答案 7 :(得分:0)
我能够让以下工作(我还发布了on this jira issue)。
/home/jenkins/GitlabPostbuildReporter.groovy
class GitlabPostbuildReporter {
def manager
public GitlabPostbuildReporter(manager){
if(manager == null) {
throw new RuntimeException("Manager object musn't be null")
}
this.manager = manager
}
public def report() {
// do work with manager object
}
}
If type="email" Then
'Logic for email
Exit Sub
End If
If type="sms" Then
'Logic for sms
Exit Sub
End If