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

查看:12
本文介绍了移动到 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.

那么,试试这个:

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天全站免登陆