1

我在 groovy Jenkinsfile 中有以下代码:

def current = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss.SSSZ').parse(currenttime.trim())
println current
def end_date = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss.SSSZ').parse(scheduled_end_date.trim())
println end_date
schedule_grace_period_validity = current - end_date > 5 ? false : true

输出是:

Tue Feb 27 13:20:54 EST 2018
[Pipeline] echo
Mon Dec 18 18:00:00 EST 2017
[Pipeline] echo
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DateGroovyMethods minus java.util.Date java.util.Date 

这在我的本地盒子中工作得很好,但在 Jenkins 的沙盒模式下,这失败了,我无法在 Jenkins 中关闭沙盒模式。

有什么解决方法吗?

4

1 回答 1

2

最简单的方法是转到/scriptApproval/您的 Jenkins 实例中的页面并批准签名。当您在运行脚本后遇到此异常时,您将在脚本批准页面中看到类似以下内容:

在此处输入图像描述

只需单击Approve并再次运行您的脚本。

或者,您可以尝试以天计算两个日期之间的差异:

int diff = BigDecimal.valueOf((current.time - end_date.time) / 86400000).setScale(0, java.math.RoundingMode.UP).intValue()

但在这种情况下,您也可能会遇到RejectedAccessException. 我尝试在本地 Jenkins 实例的 Groovy 沙箱中运行它,结果如下:

[Pipeline] End of Pipeline
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method java.util.Date getTime
    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:175)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor$6.reject(SandboxInterceptor.java:261)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:381)
    at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:284)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checke
于 2018-02-27T19:10:58.020 回答