How do i pass parameters from Jenkins to Ant scripts? - ant

For some GUI testing I'm creating a Jenkins task for each GUI module to be tested.
Once created I'm using Ant to build these tests, but I'm not aware of how to actually pass parameters from Jenkins to Ant build file? Basically how do I do variable substitution in Ant?
I'm using the Sahi framework to test GUI components, so the flow goes like this...
Jenkins → Ant build script → Sahi file to execute
Can anyone please take a look at it?

"Using ant -Dname=value lets you define values for properties on the Ant command line." http://ant.apache.org/faq.html#passing-cli-args
To use a jenkins parameter as a variable when you call any use ${variablename}
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build

Click Advance under configure section of your job in jenkins and use "Properties" section to pass parameter value to Ant script.
e.g
jenkins.param=10
ant.prop=$jenkins.param where jenkins.param is the parameter defined in the jenkins job .
Now in your ant build script ,you can get the value by using ${ant.prop}.

From Jenkins to SAHI Pro via ANT.
In the ant target that you call from Jenkins, give the following within sahi tag.
<customfield key="variable_name" value=" variable _value"/>
Now, such values from Jenkins will be available in SAHI Pro through the ant target. To retrieve them in SAHI, you should set them in “CUSTOM_FIELDS” of testrunner file.
For example:
SET CUSTOM_FIELDS= -variable_name jenkinsToSahiVariable
Where -variable_name should be same key that you set in ant target. And second string will contain the value you set from Jenkins. To get this in a sahi file, use sahiSuite API like following.
$jenkinsValues = _suiteInfo();
$sahiVariable = $ jenkinsValues ["jenkinsToSahiVariable"];

Related

jenkins parameter From Properties file

I have 3 Jenkins jobs to be run in serial.
Run a Ant File
Run another ANT File
Run a command line
All the above jobs use a file path which is set in a properties file.
Ex Job 1 , Executes ANT file placed in file path location
Job 2 , Executes another file placed in same file path location
Job 3 , Executes command line to do SVN update in same file path location
I need to parameterize the file path in all three builds from properties file.
Can anyone help me with possible approach?
Thanks In Advance
This answer could be a little high level. You can use Jenkins Pipeline as a code for this approach instead of using 3 freestyle jobs.
You can create 3 stages which performs these 3 steps. Pipeline as a code supports reading of properties from different file types (json, yaml etc.)
Look for the "EnvInject" plugin. This lets you inject properties into your build as environment variables; these assignments survive build step boundaries.
If the property file is checked in, you can load it in the Build Environment section before the build steps start executing. If the property file is generated during the build sequence, you can add a build step between where the property file is created and where it is used.
Once set, if the property file contains "FOO=/path/to/folder" then in configuring Jenkins things you would refer to $FOO or ${FOO} (for example, an Ant build step might specify "${FOO}/build.xml"; in Windows batch script execution FOO shows up as an environment variable and is referenced by %FOO% (i.e., "#echo Some_Useful_Piece_Of_Data > %FOO%\data.txt"
More information can be found here: https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin

how to scan test source code with the SonarQube Jenkins plugin?

I'm trying to make a Jenkins job that only scans the test source files, so everything under /src/test/java (using Maven). I use the SonarQube Jenkins post-action for this.
When we used to configure Sonar in the pom file directly we could do this in a profile:
<sonar.sources>/src/test/java</sonar.sources>
<sonar.tests/>
That worked fine.
But in the Jenkins job I have to specify these as 'Additional properties' and I can't seem to specify an emtpy sonar.tests element. I tried -Dsonar.tests, -Dsonar.tests=,-Dsonar.tests="", nothing works. When this element is not empty Sonar will attempt to scan the test files twice and crash.
The post-build step is specifically and explicitly a Maven operation. Your problem comes from trying to use Maven to do something un-Mavenish; i.e. ignore the convention that files in the tests directory should be treated as tests.
Since you want to scan your tests as code, your best bet is to use the build step (which uses SonarQube Scanner) and set your scanner properties manually. That will make it easy to set your sources directory and to omit the tests directory.

How to override user.home property of ant from Jenkins server?

Hi I need some help in overriding the user.home property of ant from Jenkins. I created a simple job on jenkins that runs a build script on a slave machine of the master. One of the tasks of the scripts is to use the user.home property of ant and access certain files. The problem is that when I try to echo this property from the command line of the slave machine, I see C:\Users\Administrator but when I run the exact the same script from the Jenkins server I see C:\ . This difference is causing the build to fail on Jenkins. Can anyone please tell me what is the reason for this difference? Is there any way I can override this?
I tried having a property called user.home in the config file of my job but it doesn't seem to pick it up. Another interesting point is that when I changed the user.home value to some gibberish in the config file and run ant from command line on the slave, it still picks it up as C:\Users\Administrator
I am using ant 1.7.1 and Jenkins 1.598
You likely have jenkins running as a different ID than Administrator. Which is a good thing.
Properties in ant are immutable so you can specify those properties from the ant plugin in jenkins and they'll override any other values. (click advanced under the invoke ant buildstep to see the properties text box).
I'd recommend you not override user.home but rather refactor your ant so there is a config.location property that defaults to user.home but can be overidden to whatever you want from jenkins. It just seems unnatural to ovrride user.home

Jenkins - How to access BUILD_NUMBER environment variable

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!
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?
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}"/>
For Groovy script in the Jenkinsfile using the $BUILD_NUMBER it works.
Use $env:BUILD_NUMBER in PowerShell script

Set a Jenkins variable after reading console log line / value --OR-- using the variables used by scripts / commands what Jenkins calls

I DONT need the following:
How to set a Jenkins env variable or
How to use a environment variables in Jenkins / windows shell / ant / etc scripts.
What I need is opposite of that.
Summary:
1. I have a Jenkins job: ABC_Build
2. This job calls a .bat file (which calls an ANT code / target for packaging / building a
build). As we are creating a build, this job know what's the new build label name and
ANT is storing it in a variable called "new.build.label". File used is build.xml.
(A NOTE to novice users: If you want to call many Windows commands (.bat / .cmd or
commands which creates a windows shell) then, you should call it using "call
script.bat -Dparam1 -Dparam2...." way).
Now, this job calls an another .bat file (which calls an ANT code /target) and uses one
of the parameter value which gets generated by first .bat file / ANT package target
call (i.e. "new.build.label"). As this is a separate .bat command call to call a new
session of ANT code/target, I need to pass the value of "new.build.label" during the
call of this step. File used here is deploy.xml.
Basically, I'm trying to see how can I set a variable in Jenkins, either by using:
a. reading the console output of my Jenkins job as I'm echoing the value of
new build label in the standard output / console output.
b. any other way, where I can set a jenkins variable using "new.build.label" ANT
variable (once first .bat / ANT package target is finished) and I'm ready to call
the 2nd .bat / .cmd / ANT call for doing deployment. Unfortunately, I can't do both
package / deploy at the same time.
I'm also not interested in knowing WHY CAN'T I call target deploy from first ANT
session when I already know the value of "new.build.label" as my main request is:
HOW TO set a jenkins variable using a "variable" which was used by one of the scripts (ANT/Jelly/Groovy/Maven/etc) that Jenkins called.
You can pass environment variables among Jenkins build steps via EnvInject plugin. In your particular case the following is probably the best way:
The first ANT should echo new.build.label into a properties file that can be read by EnvInject plugin, e.g.:
<echo message="new.build.label=${new.build.label}" file="envars.props" />
Create an Inject environment variables build step and set "Properties File Path" to envars.props (make sure you are dealing with paths correctly). Then new.build.label will be available as an environment variable to the rest of your build steps.
By the way, I think it is not a good practice to call ANT from batch files in Jenkins. Use ANT build step instead.

Resources