kicksticks
kicksticks

Reputation: 239

How to include git branch name as variable for ant build.xml

I'm trying to use the git branch name as a variable so that I can include it in the SonarQube project key. Does anyone know of how I can do this? I'm on Windows if that helps. I've tried doing things like this:

property name="sonar.projectKey" value="${GIT_BRANCH}"

or setting environment variables in Jenkins and calling those but I can't seem to get it.

Thanks in advance :)

Additional info:

I'm building the project from Jenkins and the part where it fails is when I call ant sonar. Using ${GIT_BRANCH} results in this:

"C:\Program Files (x86)\Jenkins\jobs\PR Projects\jobs\PR_JmxMonitor_RPM\workspace\projects\JmxMonitor\build.xml:132: Validation of project reactor failed: o "${GIT_BRANCH}" is not a valid project or module key. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit."

Upvotes: 1

Views: 1287

Answers (2)

Sankar
Sankar

Reputation: 323

try this, But you have use Job --> Source Code Management -> Git , then GIT_BRANCH will work.

property name="sonar.projectKey" value="${env.GIT_BRANCH}"

Upvotes: 0

kicksticks
kicksticks

Reputation: 239

When I went to localhost:8080/job/TEST/10/injectedEnvVars (for example) I couldn't see GIT_BRANCH as an environment variable. I realized I hadn't selected 'inject environment variables to the build process' in the build environment.

Then in build.xml, I made value="$(branch)" and added -Dbranch=%GIT_BRANCH% to my command line.

Thanks all for the help :)

Upvotes: 2

Related Questions