Check if property exists Groovy

git, groovy, jenkins

Solution

Here you go:

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:

As far as I understood the problem is that your class doesn't have neither `bindings`, nor `project` and `GIT_BRANCH_NAME` variables declared (or they're not passed into the binding from outside). Is it possible to publish the entire class and probably the script executor(binding or like that)?

Problem

I am using multi-branch plugin in Jenkins and polling my git repo to see if there are any changes. My problem is that when it polls, my $GIT_BRANCH_NAME variable doesn't exist and the build fails. ``` groovy.lang.MissingPropertyException: No such property: GIT_BRANCH_NAME for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) ``` I tried checking for ``` 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() } ``` and ``` 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() } ``` It doesn't work though. Any help is appreciated

Original source