Jenkins multibranch pipeline pass BRANCH_NAME to gradle build

Hi,
a multibranch jenkins pipeline calls a gradle build.
Need to pass the BRANCH_NAME to the sonarqube task in build.gradle:

sonarqube {
properties {
property “sonar.host.url”, “https://sonar
property “sonar.projectKey”, “com.foo.bar:Foobar”
property “sonar.projectName”, “com.foo.bar-Foobar”
property “sonar.binaries”, “build”
property “sonar.branch.name”, “${branchname}” <---------------
property “sonar.branch.target”, “master”
}
}

Tried with:

  • direct use of ${BRANCH_NAME} in gradle build
  • gradlew Dbranchname=${BRANCH_NAME} in Jenkinsfile
  • gradlew Pbranchname=${BRANCH_NAME} in Jenkinsfile
  • def branchname=${BRANCH_NAME} before calling gradlew in Jenkinsfile

but nothing worked, the property branchname is always unknown.
How to pass a property from Jenkinsfile to gradle build ?
Regards,
Gilbert

Solved, works with directly passing the sonarqube property via commandline like that:

stage(‘Sonarscan’) {
withSonarQubeEnv(‘SONAR’) {
bat “gradlew sonarqube -Dsonar.branch.name=${BRANCH_NAME} --info”
}
}