检查属性是否存在Groovy

时间:2017-05-09 16:21:11

标签: git jenkins groovy

我在Jenkins中使用多分支插件并轮询我的git repo以查看是否有任何更改。我的问题是,当它进行轮询时,我的$ GIT_BRANCH_NAME变量不存在,并且构建失败。

groovy.lang.MissingPropertyException: No such property: GIT_BRANCH_NAME for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)

我试过检查

if (!bindings.hasProperty(GIT_BRANCH_NAME)){
echo "Why is this not working"
env.GIT_BRANCH_NAME = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
}

 if (!project.hasProperty(GIT_BRANCH_NAME)){
    echo "Why is this not working"
    env.GIT_BRANCH_NAME = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
    }

虽然它没有用。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

你走了:

if (!bindings.properties."${GIT_BRANCH_NAME}")){
    echo "Why is this not working"
    env.GIT_BRANCH_NAME = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
}

UPD:

据我所知,问题是您的班级没有声明bindingsprojectGIT_BRANCH_NAME变量(或者他们没有传递到绑定中外)。是否可以发布整个类,可能还有脚本执行器(绑定或类似)?