Groovy脚本中的Jenkins变量 [英] Jenkins variable in groovy script

查看:1256
本文介绍了Groovy脚本中的Jenkins变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jenkins脚本调用的groovy文件中使用"$ WORSKPACE"变量.但是在SO上找到的所有解决方案都失败了:

I'd like to use "$WORSKPACE" variable into a groovy file called by jenkins script. But all solutions found on SO failed :

// KO : Wks = build.getEnvironment(listener).get('WORKSPACE')
// KO : Wks = "${WORKSPACE}"
/* KO :
def thr = Thread.currentThread()
def build = thr?.executable
def envVarsMap = build.parent.builds[0].properties.get("WORKSPACE")
*/

// KO : def build = this.getProperty('binding').getVariable('build')
// KO : Wks = "%WORKSPACE%"

我收到的消息: 不允许脚本使用groovy.lang.GroovyObject方法setProperty java.lang.String java.lang.Object(JenkinsHelper.name).管理员可以决定是批准还是拒绝此签名.

Message I got : Scripts not permitted to use method groovy.lang.GroovyObject setProperty java.lang.String java.lang.Object (JenkinsHelper.name). Administrators can decide whether to approve or reject this signature.

是否有任何代码或选项可供设置以允许Jenkins脚本正常工作?

我的案例:

JenkinsHelper.groovy文件:

File JenkinsHelper.groovy :

    class JenkinsHelper {
     def init(String sln) { 
      Wks = "%WORKSPACE%"
     }
    }
return new JenkinsHelper();

从jenkins脚本调用:

Call from jenkins script :

def helper = load 'C:/.../test.groovy'
helper.init("Mon SLN")

谢谢:)

推荐答案

这是由Jenkins进程内脚本批准引起的,目的是保护可能执行的恶意脚本.

This is caused by Jenkins In-process Script Approval as a protection of possible execution of malicious scripts.

如果您是管理员,请在管理Jenkins > 配置系统> 全局管道库中注册管道代码,以避免出现这种情况.

If you're an admin, register your pipeline codes in Manage Jenkins > Configure System > Global Pipeline Libraries to avoid this scenario.

参考:

  1. https://jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries -详细介绍了编写库的过程.
  2. https://jenkins.io/doc/book/managing/script-approval
  1. https://jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries - Goes in details on writing a library.
  2. https://jenkins.io/doc/book/managing/script-approval

这篇关于Groovy脚本中的Jenkins变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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