Question

Is it possible to do this?

For example could one do something like:

System.getEnv("$(env.BUILD_URL)");

I do not have access to Jenkins, so can't try it out myself :(

Any help would be greatly appreciated.

Was it helpful?

Solution

Assuming you're using Maven (you've tagged this question as such), I simply add the following to my pom.xml file:

...
<properties>
    <!-- Hudson properties: see http://wiki.hudson-ci.org/display/HUDSON/Building+a+software+project#Buildingasoftwareproject-HudsonSetEnvironmentVariables -->
    <jenkins.buildId>${env.BUILD_ID}</jenkins.buildId>
    <jenkins.buildNumber>${env.BUILD_NUMBER}</jenkins.buildNumber>
    <jenkins.buildTag>${env.BUILD_TAG}</jenkins.buildTag>
    <jenkins.cvsBranch>${env.CVS_BRANCH}</jenkins.cvsBranch>
    <jenkins.executorNumber>${env.EXECUTOR_NUMBER}</jenkins.executorNumber>
    <jenkins.hudsonUrl>${env.HUDSON_URL}</jenkins.hudsonUrl>
    <jenkins.javaHome>${env.JAVA_HOME}</jenkins.javaHome>
    <jenkins.jobName>${env.JOB_NAME}</jenkins.jobName>
    <jenkins.svnRevision>${env.SVN_REVISION}</jenkins.svnRevision>
    <jenkins.workspace>${env.WORKSPACE}</jenkins.workspace>
</properties>
...

...and then from your code you can simply do a:

String url = System.getProperty("jenkins.hudsonUrl");  // could be null

Putting these into properties makes life more simple for my purposes, especially when using Maven profiles to control my builds. For example, I make sure to create a "jenkins" profile that is activated when I build on a Jenkins build server. When this is done, all the aforementioned jenkins properties are set. When not run as a jenkins profile, those properties are set to some other default value. Anyway, that's another topic, but food for thought. Hopefully it make sense.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top