Jenkins2.0 管道脚本拒绝日期算法
Jenkins2.0 pipeline script reject date arithmetic
我在 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 实例中的 /scriptApproval/
页面并批准签名。当您在 运行 配置您的脚本后遇到此异常时,您将在脚本批准页面中看到类似这样的内容:
只需单击批准并再次运行您的脚本。
或者,您可以尝试计算两个日期之间的天数差异:
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.reject(SandboxInterceptor.java:261)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:381)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:284)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checke
我在 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 实例中的 /scriptApproval/
页面并批准签名。当您在 运行 配置您的脚本后遇到此异常时,您将在脚本批准页面中看到类似这样的内容:
只需单击批准并再次运行您的脚本。
或者,您可以尝试计算两个日期之间的天数差异:
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.reject(SandboxInterceptor.java:261)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:381)
at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:284)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checke