Jenkins管道脚本中的@NonCPS有什么影响 [英] What is the effect on @NonCPS in a Jenkins pipeline script

查看:2489
本文介绍了Jenkins管道脚本中的@NonCPS有什么影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我曾经得到这个异常:


org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:
脚本不允许使用方法groovy.json.JsonSlurperClassic
parseText java.lang.String


我查看了异常,并发现了一些迹象表明我应该使用 @NonCPS 注释发生异常的方法。 。我这样做了,但没有真正理解它的作用。

然后,我抛出该方法的异常不再被尝试子句。



那么 @NonCPS 后面的想法是什么?使用它的效果是什么?

解决方案

你看到的异常是由于脚本安全性和沙箱。基本上,默认情况下,当你运行一个管道脚本时,它运行在一个只允许使用某些方法和类的沙箱中。有许多白名单操作的方法,请查看上面的链接。



当您有方法时, @NonCPS 它使用不可序列化的对象。通常情况下,您在管道脚本中创建的所有对象都必须是可序列化的(原因是Jenkins必须能够序列化脚本的状态,以便它可以暂停并存储在磁盘上)。

当您在方法中放置 @NonCPS 时,Jenkins将一次执行整个方法,而无法暂停。此外,您不允许从 @NonCPS 注释方法内引用任何管道步骤或CPS转换方法。 关于此的更多信息可以在这里找到



至于异常处理:不是100%确定你正在经历什么;我已经尝试了以下方法,并且按预期工作:

  @NonCPS 
def myFunction(){
抛出新的RuntimeException();
}

尝试{
myFunction();
} catch(Exception e){
echoCaught;
}

  @NonCPS 
def myFunction(){
throw new RuntimeException();


def mySecondFunction(){
try {
myFunction();
} catch(Exception e){
echoCaught;
}
}

mySecondFunction();

最后:

  @NonCPS 
def myFunction(){
throw new RuntimeException();


@NonCPS
def mySecondFunction(){
try {
myFunction();
} catch(Exception e){
echoCaught;
}
}

mySecondFunction();

所有打印抓住都如预期。


I have a pipeline script in Jenkins.

I used to get this exception:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.json.JsonSlurperClassic parseText java.lang.String

I looked the exception up and I found some indications that I should annotate the method where theexception occurs with @NonCPS. I did this, without really understanding what this does.

After that however, an Exception that I was throwing in that method was no longer caught by a try clause.

So what's the idea behind @NonCPS? What are the effects of using it?

解决方案

The exception that you are seeing is due to script security and sandboxing. Basically, by default, when you run a pipeline script, it runs in a sandbox which only allow usage of certain methods and classes. There are ways to whitelist operations, check the link above.

The @NonCPS annotation is useful when you have methods which use objects which aren't serializable. Normally, all objects that you create in your pipeline script must be serializable (the reason for this is that Jenkins must be able to serialize the state of the script so that it can be paused and stored on disk).

When you put @NonCPS on a method, Jenkins will execute the entire method in one go without the ability to pause. Also, you're not allowed to reference any pipeline steps or CPS transformed methods from within an @NonCPS annotated method. More information about this can be found here.

As for the exception handling: Not 100% sure what you are experiencing; I've tried the following and it works as expected:

@NonCPS
def myFunction() {
    throw new RuntimeException();
}

try {
    myFunction();
} catch (Exception e) {
    echo "Caught";
}

and

@NonCPS
def myFunction() {
    throw new RuntimeException();
}

def mySecondFunction() {
    try {
        myFunction();
    } catch (Exception e) {
        echo "Caught";
    }
}

mySecondFunction();

and finally:

@NonCPS
def myFunction() {
    throw new RuntimeException();
}

@NonCPS
def mySecondFunction() {
    try {
        myFunction();
    } catch (Exception e) {
        echo "Caught";
    }
}

mySecondFunction();

All print "Caught" as expected.

这篇关于Jenkins管道脚本中的@NonCPS有什么影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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