Search code examples
jenkinsgroovy

No such DSL method '$' found among steps - env.CHANGE_BRANCH


I have a condition in my groovy file, which checks if env.CHANGE_BRANCH is not equal null. In that case variable branch is set as env.CHANGE_BRANCH, otherwise branch is env.BRANCH_NAME.

When env.CHANGE_BRANCH is null I get a following error:

java.lang.NoSuchMethodError: No such DSL method '$' found among steps

def branch = ""
script {
   if (env.CHANGE_BRANCH != null) {
                        branch = env.CHANGE_BRANCH
                    } else {
                       branch = env.BRANCH_NAME
                    }
    echo "Print branch"
}
    echo ${branch}

Solution

  • Your echo statement has to be in double quotes for string interpolation to work.

    echo "${branch}"
    

    or do

    println branch