Jenkins - Pass checkout directory into ant as parameter - ant

In Jenkins how do I specify the checkout directory as a parameter in a ANT call?

You can pass the checkout directory as an Ant system property, as in (e.g. linux):
ant -Dcheckout.dir=${WORKSPACE}/[checkout dir] <target>
or on a Windows agent:
ant -Dcheckout.dir=%WORKSPACE%/[checkout dir] <target>
The checkout directory is what you specified in the scm settings for your project. It can very well be the root directory of your workspace ".", or some explicit sub-folder.
Then, within your ant script, the path for the checkout dir will be available under the ${checkout.dir} property.

Related

Jenkins fails to find SureFire Reports

Jenkins Version : 2.176.2
Executing Selenium tests via Jenkins : Ecplsie+mvn+Jenkins
Selenium Workspace Folder : C:\Users\admin\eclipse-workspace\ACA
The actual location of the testng-results.xml: C:\Users\admin\eclipse-workspace\ACA\target\surefire-reports\testng-results.xml
Jenkins Insatlled / Home Directory: C:\Program Files (x86)\Jenkins
Building in workspace C:\Program Files (x86)\Jenkins\workspace\ACATestAutomationJob
Executing Maven: -B -f C:\Users\admin\eclipse-workspace\ACA\pom.xml clean install
TestNG Reports Processing: START
Looking for TestNG results report in workspace using pattern: **/target/surefire-reports/testng-results.xml
Did not find any matching files.
How do i make jenkins locate this testng-results.xml?
Thanks,
Raj
Look into Jenkins Console Output, it should report where the build is running (so called WORKSPACE) and where TestNG Results Plugin attempts to locate the results file:
The path to the TestNG results file must be relative to the WORKSPACE and have the syntax of Ant FileSet
If you're uncertain regarding how to properly build the path to the test artifacts - post the full paths to WORKSPACE and the testng-results.xml / emailable-report.html and we will help you to come up with the correct definitions.
In the meantime you could use wildcard paths like:
**/testng-results.xml
so Jenkins will scan its WORKSPACE recursively looking for the testng-results.xml file in all available locations
Your install command also looks suspicious, normally you should not be putting your test artifacts to the Maven repository so you might want to use mvn test or mvn verify instead.
More information:
Turbo Boost Your Digital App Test Automation with Jenkins
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Change directory during a build job on jenkins

I am building a gradle project and the source code is in git. After checking our repo to jenkins' workspace, how do i have jenkins go to a sub-directory and do the build?
I tried adding shell commands but cd will not work as it executes script on a separate shell.
If you are building a gradle project, perhaps you should use the "Invoke gradle script" step rather than a shell script?
As part of the gradle build script, it has an option to specify the "Root Build script", which will let allow you to specify a subdirectory if you wish.
See the Gradle plugin for more information.

Jenkins envInject Plugin not picking PATH

I have my Jenkins configured to Inject environment variables to the build process and set the Properties File Path to G:\Jenkins\env.properties
Inside my env.properties
VISUALSTUDIOVERSION=12.0
PATH=$PATH
When I run my job, the PATH is not reflecting my system path instead its just showing $PATH only. How can I edit my env.properties file to pick up system PATH?
THe output of Env variables after build is
VISUALSTUDIOVERSION=12.0
PATH=$PATH
But I am expecting to see it as
VISUALSTUDIOVERSION=12.0
PATH=C:\Program Files (x86)\Perl\site\bin;C:\Program Files (x86)\Perl\bin;
Environment variables in Java are case-sensitive. Try Path instead of PATH.

What's the name of the default ant config file name?

Actually I'm a new to ant. I want to know the name of the default ant config file name. Is it build.xml or other name? If is it can I change that name? If so, how can I execute the script?
Yes, the default build file is build.xml in the current directory.
So if you execute
ant
Then ant will attempt to load build.xml in the current directory.
You can specify a file with a different name or location by specifying with the "-f" option, for example
ant -f ../other_dir/build.xml
or
ant -f special.xml

Is there any way to change the working directory of a Jenkins Maven build?

Is there any way to change the working directory of a Jenkins Maven build?
Use Case
There is a Maven build that will only work if the current working directory is the directory with pom.xml in it, so I want the Maven build to execute in that directory.
The directory with the pom.xml is in a subdirectory of a git project.
Create a target to checkout the parent.
Checkout the parent dir of your build target
Set Build -> Goal = clean
Run build and ascertain the destination workspace directory (second line of console output, in my sample /var/lib/jenkins/workspace/my_project)
Create a target to build your path
DO NOT check "Delete workspace before build starts"
In Build -> Advanced - Check "Change folder of workspace" and paste your /yourtargetdirectory
It worked for me.
Not sure when this was added, but for the Jenkins in front of me (Jenkins ver. 1.627 per the bottom of the page), there's now an option for defining where the root pom.xml is (help message says:
If your workspace has the top-level pom.xml in somewhere other than
the 1st module's root directory, specify the path (relative to the
module root) here, such as parent/pom.xml. If left empty, defaults to
pom.xml
). I was facing the same issue but then realized the answer was staring me in the face (note that I did not have to hit advanced as Hua2308 suggested, probably because they changed the UI)
For a reference, here is what works for me. In the "Invoke top-level Maven targets" build step, there is an "Advanced..." button. If you click this button, you can specify the relative path to your POM.xml file. This should ensure you are only building the desired project in the subdirectory.
You can use Maven's -f option
-f,--file <arg> Force the use of an alternate POM file (or directory with pom.xml).
For example if your project has a structure:
README.md
sources
|----pom.xml
|----services
| |----pom.xml
| +----src
|----webservices
| |----pom.xml
| +----src
+----....
useful
docs
then you can use Goals in Invoke top-level Maven targets:
-f sources/pom.xml clean install
I came here because I had the same situation described, The version of Hudson / Jenkins that I have has the same problem, it does not allow setting the working directory before calling maven.
The workaround is to configure a parameter that has the desired working directory, create a new command "mvn" that makes the change directory and then invoke the original command "mvn" of maven with all the arguments.
steps to follow:
In the project it should be noted that the project needs to be parameterized, in my case create the parameter WORKING_DIRECTORY with value $ {WORKSPACE}/maven, being "maven" the subfolder that will be the working directory.
enter image description here
Create a "custom" version of maven to modify the "mvn" command:
cp -ip /opt/apache-maven-3.5.4 /opt/maven_custom
mv /opt/maven_custom/mvn /opt/maven_custom/mvn_original
vi /opt/maven_custom/mvn:
-- content new mvn command --
cd $WORKING_DIRECTORY
/opt/maven_custom/bin/mvn_original "$#"
-- end content new mvn command --
chmod ugo+x /opt/maven_custom/mvn
Add the new maven_custom in hudson/Jenking:
Hudson Admin --> System Configurations --> Maven --> Add maven
Use the custom maven in the project
With these changes maven runs in a different working directory
Go to the configuration of your maven project, then:
Build > click the "Advanced" button > check "Use custom workspace" > set the directory
(I'm using Jenkins version 1.508)
When the subdirectory is a direct child of the parent root, you could use mvn --projects since that changes into the subdirectory. Here a proof (see last line):
U:\git\ics-VCCUSTOM-422>mvn --projects icsIntegrationTest clean verify
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.afklm.vccustom:icsIntegrationTest >----------------
[INFO] Building icsIntegrationTest 13.3.4-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # icsIntegrationTest ---
[INFO] Deleting U:\git\ics-VCCUSTOM-422\icsIntegrationTest\target
[INFO]
[INFO] --- buildnumber-maven-plugin:1.3:create (default) # icsIntegrationTest ---
[INFO] ShortRevision tag detected. The value is '7'.
[INFO] Executing: cmd.exe /X /C "git rev-parse --verify --short=7 HEAD"
[INFO] Working directory: U:\git\ics-VCCUSTOM-422\icsIntegrationTest
The Jenkins we are working with is in German so I'm trying to guess how the options are translated.
Go to the configuration of your project and there go to enhanced project configuration (it's the second section) and expand it. The last point should be something like Change folder of workspace. I think that might be what you are looking for.
Otherwise you can always go to the Build options and change the path to your pom.xml
Solution:
cd subfolder; mvn -f ../pom.xml task
Maven does not provide an option to do it the other way round, meaning the sensible way for this use-case, probably because Maven hates you.

Resources