将命令移到jenkinsfile中的自定义类时,命令将失败 [英] Commands fail when moving to them custom class in jenkinsfile

查看:168
本文介绍了将命令移到jenkinsfile中的自定义类时,命令将失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个新版本.运行一个简单的shell命令非常有效,如下所示:

I'm setting up a new build. Running a simple shell command works perfectly, like below:

stage("Demo") {    
    sh "echo 'Hi There'"
}

我一直试图将我的shell脚本打包"到自己的类中,只是为了使事情更加整洁.问题是,当尝试从一个类中执行相同的完全相同的Shell脚本时,jenkins失败,并显示以下内容:

I have been trying to "package" my shell scripts into their own classes just to neaten things up a bit. The problem is that when trying to execute the same exact shell script from within a class, jenkins fails the builds with:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: 未分类的方法java.lang.Class sh java.lang.String

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified method java.lang.Class sh java.lang.String

这是一个简单的示例,在将上述方法移入其自己的类后,对我来说失败了:

This is a simple example that fails for me after moving the above method into its own class:

stage('Demo stage') {
    Tools.PrintMe("Hi There")   
}

public class Tools {
    public static void PrintMe(String message) {
        sh "echo " + message
    }
}

脚本管理器中也没有提供将此拒绝方法列入白名单的选项.

There is also no option provided in the script manager to Whitelist this rejected method.

有办法解决这个问题吗?还是有我不知道的限制?

Is there a way to get around this? Or is there a limitation that I'm not aware of?

推荐答案

@Crait调用您自己的类中的预定义步骤,您需要将脚本对象路径到您的类中.

@Crait to make a call of predefined steps in your own class you need to path script object to you class.

因此,请尝试以下操作:

So, try this:

stage('Demo stage') {
    Tools.PrintMe(this, "Hi There")   
}

public class Tools {
    public static void PrintMe(def script, String message) {
        script.sh "echo " + message
    }
}

这篇关于将命令移到jenkinsfile中的自定义类时,命令将失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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