简体   繁体   English

如何在Jenkins Groovy Post Build插件中重用groovy脚本?

[英]How to re-use groovy script in Jenkins Groovy Post Build plugin?

I have some groovy code which I am planning to re-use in Jenkins Groovy Post Build plugin of multiple jobs. 我有一些groovy代码,我打算在多个作业的Jenkins Groovy Post Build插件中重复使用。 How can I achieve this? 我怎样才能做到这一点? Is there a place I can store the script in a global variable and call that in the jobs where ever I need? 有没有一个地方我可以将脚本存储在一个全局变量中,并在我需要的工作中调用它?

You can load any groovy file living on the Jenkins master within the groovy postbuild and execute it. 您可以在groovy postbuild中加载生活在Jenkins主服务器上的任何groovy文件并执行它。 For example, you could have a special directory on the c drive where all the common scripts live. 例如,您可以在c驱动器上有一个特殊目录,其中包含所有常用脚本。 I'll update my answer later with some code that shows you how to load the script in. 稍后我将使用一些代码更新我的答案,该代码向您展示如何加载脚本。

Update 更新

Assuming you have a test.groovy file on your C: drive, it should be as simple as the following in Groovy Postbuild: 假设您的C:驱动器上有一个test.groovy文件,它应该像Groovy Postbuild中的以下内容一样简单:

evaluate(new File("C:\\test.groovy"))

Please view the comment section of the Groovy Postbuild for more examples and possibly other ways. 请查看Groovy Postbuild的评论部分以获取更多示例和其他可能的方法。

Here is the solution that worked for me: 这是适合我的解决方案:

  1. Installed Scriptler plugin for Jenkins and saved the Groovy script in that. 为Jenkins安装了Scriptler插件并保存了Groovy脚本。 Now the script is available in JENKINS_HOME/scriptler/scripts directory. 现在该脚本在JENKINS_HOME/scriptler/scripts目录中可用。 This way we can avoid manual step of copying files to Jenkins master. 这样我们就可以避免手动将文件复制到Jenkins master。
  2. Used the groovy file in Post build: 在Post build中使用了groovy文件:

def env = manager.build.getEnvironment(manager.listener) evaluate(new File(env['JENKINS_HOME'] + "\\\\scriptler\\\\scripts\\\\GroovyForPostBuild.groovy"))

This is a copy of my answer to this similar question on StackOverflow : 这是我对StackOverflow上类似问题的回答的副本:

If you wish to have the Groovy script in your Code Repository, and loaded onto the Build / Test Slave in the workspace, then you need to be aware that Groovy Postbuild runs on the Master. 如果您希望在代码存储库中安装Groovy脚本,并将其加载到工作区中的构建/测试从属服务器上,那么您需要注意Groovy Postbuild在主服务器上运行。

For us, the master is a Unix Server, while the Build/Test Slaves are Windows PCs on the local network. 对我们来说,主服务器是Unix服务器,而构建/测试从服务器是本地网络上的Windows PC。 As a result, prior to using the script, we must open a channel from the master to the Slave, and use a FilePath to the file. 因此,在使用脚本之前,我们必须打开从主服务器到从服务器的通道,并使用FilePath到该文件。

The following worked for us: 以下为我们工作:

// 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);
} 

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

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