2017-05-09 106 views
1

我在詹金斯中使用多分支插件并轮询我的git仓库以查看是否有任何更改。我的问题是,当它轮询时,我的$ GIT_BRANCH_NAME变量不存在,构建失败。检查属性是否存在Groovy

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() 
    } 

它不工作,虽然。任何帮助表示赞赏

+3

难道不是'hasProperty(“GIT_BRANCH_NAME”)'? – Opal

+0

这给出了一个错误 - >'groovy.lang.MissingPropertyException:类项目:无财产groovy.lang.Binding \t在groovy.lang.Binding.getVariable(Binding.java:63) \t在org.jenkinsci .plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)' – Illusionist

回答

1

在这里你去:

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:

据我了解的问题是,你的类没有既不bindings,也不projectGIT_BRANCH_NAME变量声明(或者它们没有从外部传递到绑定中)。是否有可能发布整个类和可能的脚本执行者(绑定或类似的)?

+0

这给出了一个错误 - > 'groovy.lang.MissingPropertyException:没有这样的属性:类绑定:groovy.lang.Binding \t在groovy.lang.Binding.getVariable(Binding.java:63) \t at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)' – Illusionist

+2

这是否意味着你正在尝试得到一个失踪的财产fr om缺失的属性? –

+1

哈哈是的。我现在通过禁用自动更新来避开它。 – Illusionist