If statement within Jenkins Plugin or in sonar-project.properties

I am trying to use the Jenkins plugin. I have this statement that I want to use in either the “Analysis properties” or the sonar-project.properties file.

If ghprbTargetBranch exist then set and use sonar.pullrequest.branch, sonar.pullrequest.base, sonar.pullrequest.key but it does not work. All 4 end up in the CLI parameters.

if [ ${ghprbTargetBranch} != "" ]
then
    sonar.pullrequest.branch=${GIT_BRANCH}
    sonar.pullrequest.base=origin/${ghprbTargetBranch}
    sonar.pullrequest.key=${ghprbPullId}
fi

if [ ${ghprbTargetBranch} == "" ]
then
    sonar.branch.name=${GIT_BRANCH}
fi

also, I used this approach, and this time it was just showing SONAR_BRANCH, and SONAR_TARGET as a word, not value.

if [ ${env.BRANCH_NAME} != "master" ]
then
  SONAR_BRANCH=${env.BRANCH_NAME}
  SONAR_TARGET=master
else
  SONAR_BRANCH=${env.BRANCH_NAME}
  SONAR_TARGET=
fi
sonar.branch.name=${SONAR_BRANCH}
sonar.branch.target= ${SONAR_TARGET}

How can I use the If statement either in the “Analysis properties” or the sonar-project.properties file.

Hi @engin,

Neither the Jenkins analysis properties nor the sonar-project.properties file are interpreted by a scripting language, so you can’t use conditional statements in them. If you want to use Jenkins to analyze multi-branch projects, the best way to do it is with the Branch Source plugin, following the instructions here. The SonarQube Jenkins plugin will auto-detect all the Branch and PR parameters that you need.

Regards,

Cameron.

Thanks, Cameron for the answer.

I saw this post and thought it is possible to do that.

Just want to make sure, approach in this post is not valid, right?

Hi @engin,

I tried this in a simple project in my Jenkins setup and it doesn’t work (the post you referenced is two years old so things may have changed). You might want to look at using environment variables in a Jenkinsfile as an alternative.

Regards,

Cameron.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.