简体   繁体   English

Jenkins groovy.lang.MissingPropertyException:没有这样的属性:对于类:Script1

[英]Jenkins groovy.lang.MissingPropertyException: No such property: for class: Script1

The purpose of this program is to replace all TFS user's password.该程序的目的是替换所有 TFS 用户的密码。 It works if I directly use the userid and password inside the code, but fails with this error when I convert it to a paramaterized build.如果我直接在代码中使用用户名和密码,它就可以工作,但是当我将它转换为参数化构建时会出现此错误。

Started by user Jirong Hu
[EnvInject] - Loading node environment variables.
Building remotely on public_jenprodslave_1 in workspace D:\public_jenprodslave_1\workspace\DevOps\Update-TFSPlugin-Password
param userid value : devops_test_user
ERROR: Build step failed with exception
groovy.lang.MissingPropertyException: No such property: userid_param_value for class: Script1


import hudson.model.*
import hudson.triggers.*
import hudson.util.Secret;
import hudson.plugins.tfs.TeamFoundationServerScm

def thr = Thread.currentThread()
def build = thr?.executable
def resolver = build.buildVariableResolver

def userid_param = "userid"
def userid_param_value = resolver.resolve(userid_param)
println "param ${userid_param} value : ${userid_param_value}"

def password_param = "password"
def password_param_value = resolver.resolve(password_param)
//println "param ${password_param} value : ${password_param_value}"


updateTFSPluginPassword(Hudson.instance.items)

def updateTFSPluginPassword(items) {    

    for(item in items) {
        if (item.class.canonicalName != 'com.cloudbees.hudson.plugins.folder.Folder') {

          if (item.scm instanceof TeamFoundationServerScm) {                                    

            // Update the TFS user id here:
            //if (item.scm.getUserName() == 'devops_test_user') {
            if (item.scm.getUserName() == userid_param_value) {
                println("Working on project <$item.name>")
                println item.scm.getType()
                println item.scm.getServerUrl()
                println item.scm.getProjectPath()
                println item.scm.getWorkspaceName()
                println item.scm.isUseUpdate()
                println item.scm.getUserName()
                println item.scm.getPassword()

                // Update the TFS user password hash here:                  
                Secret secret = Secret.fromString(password_param_value)

                tfsSCM = new TeamFoundationServerScm(item.scm.getServerUrl(), 
                                                     item.scm.getProjectPath(), 
                                                     null, 
                                                     item.scm.isUseUpdate(), 
                                                     item.scm.getWorkspaceName(), 
                                                     item.scm.getUserName(), 
                                                     secret) 
                item.scm = tfsSCM

                println ("")

            }            
          }

        }  else  {              
            updateTFSPluginPassword(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems())
        }      
    }
}

引发groovy.lang.MissingPropertyException的原因之一是当您尝试访问其范围之外的变量或您尚未定义该变量时。

I guess you may need to use Field annotation, on the mentioned property.我想您可能需要在提到的属性上使用Field注释。 Please also have a look here .也请看这里

You need to check if the parameter is defined before accessing it:您需要在访问参数之前检查参数是否已定义:

if ( System.getenv('VARIABLE') ) {
  println "VARIABLE parameter exists, and its value is: " + VARIABLE
}

These methods might also work in the groovy script:这些方法也可能适用于 groovy 脚本:

  • binding.variables.containsKey('VARIABLE') binding.variables.containsKey('VARIABLE')
  • currentBuild.buildVariableResolver.resolve('VARIABLE') currentBuild.buildVariableResolver.resolve('VARIABLE')
  • build.environment.get('VARIABLE') build.environment.get('VARIABLE')

暂无
暂无

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

相关问题 groovy.lang.MissingPropertyException:没有这样的属性:类经理:Script1 - groovy.lang.MissingPropertyException: No such property: manager for class: Script1 groovy.lang.MissingPropertyException:没有这样的属性:Jenkins 用于 class:哈德森 - groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson "Jenkins groovy.lang.MissingPropertyException:没有这样的属性 Bash" - Jenkins groovy.lang.MissingPropertyException: No such property Bash groovy.lang.MissingPropertyException: 没有这样的属性: jenkins for class: groovy.lang.Binding - groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding Jenkins 方法无法获取变量(groovy.lang.MissingPropertyException:没有这样的属性:类的变量:groovy.lang.Binding) - Jenkins method cannot get variable (groovy.lang.MissingPropertyException: No such property: variable for class: groovy.lang.Binding) Jenkins 声明性管道 groovy.lang.MissingPropertyException:没有此类属性:class 的阶段:Z5F202E7AB75AE600CZ. - Jenkins declarative pipeline groovy.lang.MissingPropertyException: No such property: stage for class: groovy.lang 詹金斯 Android groovy.lang.MissingPropertyException: 没有这样的属性: 类的 HOME: groovy.lang.Binding - Jenkins Android groovy.lang.MissingPropertyException: No such property: HOME for class: groovy.lang.Binding groovy.lang.MissingPropertyException:没有这样的属性:类的脚本:groovy.lang.Binding - groovy.lang.MissingPropertyException: No such property: script for class: groovy.lang.Binding 外部groovy脚本给出错误:groovy.lang.MissingPropertyException:无此类属性:hudson。 在詹金斯中使用时 - external groovy script is giving error : groovy.lang.MissingPropertyException: No such property: hudson. while using in Jenkins groovy.lang.MissingPropertyException:没有这样的属性:任何类:WorkflowScript - groovy.lang.MissingPropertyException: No such property: any for class: WorkflowScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM