Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins - How to access BUILD_NUMBER environment variable

Tags:

jenkins

Are Jenkins parameters case-sensitive? I have a parametrized build which needs an ant parameter named "build_parameter" to be set before the build. When I try to access the ${BUILD_NUMBER} set by Jenkins, I get the value set for the ant parameter. If the build parameters are not case sensitive, can anyone suggest me a work around to this issue? I cannot change the build parameter name as I will have to change my build scripts (which is not an option). Thanks!

like image 289
Anand Avatar asked Oct 04 '13 10:10

Anand


People also ask

How can I see Jenkins environment variables?

Via env-vars. The environment variables can be viewed on an HTML page. You have to open the page on your Jenkins controller server. The steps to view the jenkins environment variables list are : At the address bar of chrome, type ${YOUR_JENKINS_HOST}/env-vars.

What is Build_number in Jenkins?

BUILD_NUMBER will return the current build number. You can also use a shorthand version BUILD_NUMBER , but in this variant may be confusing to some users - it misses the context that the BUILD_NUMBER comes from the environment variable.

How do you pass an environment variable in Jenkins pipeline?

Like if I need to inject a value within Steps/Script section, in Jenkins pipeline, I can define globally in the environment variables or using Jenkins project Configure General Check mark Prepare an environment for the run Check mark Keep Jenkins environment variables I can provide the environment variable in the ...


2 Answers

To Answer your first question, Jenkins variables are case sensitive. However, if you are writing a windows batch script, they are case insensitive, because Windows doesn't care about the case.

Since you are not very clear about your setup, let's make the assumption that you are using an ant build step to fire up your ant task. Have a look at the Jenkins documentation (same page that Adarsh gave you, but different chapter) for an example on how to make Jenkins variables available to your ant task.

EDIT:

Hence, I will need to access the environmental variable ${BUILD_NUMBER} to construct the URL.

Why don't you use $BUILD_URL then? Isn't it available in the extended email plugin?

like image 57
Peter Schuetze Avatar answered Oct 02 '22 12:10

Peter Schuetze


Assuming I am understanding your question and setup correctly,

If you're trying to use the build number in your script, you have two options:

1) When calling ant, use: ant -Dbuild_parameter=${BUILD_NUMBER}

2) Change your script so that:

<property environment="env" /> <property name="build_parameter"  value="${env.BUILD_NUMBER}"/> 
like image 35
Sagar Avatar answered Oct 02 '22 13:10

Sagar