简体   繁体   English

Groovy脚本中的Jenkins变量

[英]Jenkins variable in groovy script

I'd like to use "$WORSKPACE" variable into a groovy file called by jenkins script. 我想将“ $ WORSKPACE”变量用于jenkins脚本调用的常规文件。 But all solutions found on SO failed : 但是在SO上找到的所有解决方案都失败了:

// 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%"

Message I got : Scripts not permitted to use method groovy.lang.GroovyObject setProperty java.lang.String java.lang.Object (JenkinsHelper.name). 我得到的消息:脚本不允许使用groovy.lang.GroovyObject方法setProperty java.lang.String java.lang.Object(JenkinsHelper.name)。 Administrators can decide whether to approve or reject this signature. 管理员可以决定是批准还是拒绝此签名。

Any idea of code or option to set to allow Jenkins script to work ? 对设置为允许Jenkins脚本工作的代码或选项有任何想法吗?

My sample case : 我的案例:

File JenkinsHelper.groovy : 文件JenkinsHelper.groovy:

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

Call from jenkins script : 从詹金斯脚本调用:

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

Thanks :) 谢谢 :)

You should use groovy-style environenment variable format, not a Windows-style format: 您应该使用groovy风格的环境变量格式,而不是Windows风格的格式:

class JenkinsHelper {
 def init(String sln) { 
  Wks = "${WORSKPACE}"
 }
}

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. 如果您是管理员,请在“ 管理Jenkins” >“ 配置系统” >“ 全局管道库”中注册管道代码,以避免这种情况。

References: 参考文献:

  1. https://jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries - Goes in details on writing a library. https://jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries-有关编写库的详细信息。
  2. https://jenkins.io/doc/book/managing/script-approval https://jenkins.io/doc/book/managing/script-approval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM