sshexec command setup - ant

I need to run ant script from a local machine which will invoke ant executions on remote machine.
So on the local ant file I have :
<target name="test">
<sshexec host="${host}" username="${user}"
password="${pwd}" trust="yes"
commandResource="(cd F:\execution; ant -f build.xml run)"/>
</target>
On remote machine I have build.xml` which contains
<target name="run">
<mkdir dir ="F:\Testfolder"/>
</target>
When I execute the loca ant script I get the following error :
java.io.FileNotFoundException: (cd F:\execution; ant -f build.xml run)
(The filename, directory name, or volume label syntax is incorrect)
Why am I getting this error?

We deploy a build file named "remote-build.xml" to the path /root/project/remote-build.xml on the remote machine, and then we use
<sshexec host="${host}"
username="${user}"
password="${pwd}"
trust="yes"
command="ant -f /root/project/remote-build.xml the-targets-to-execute" />
to execute the target.

You could create a file in remote machine ( F:\execution\runmanycommands.sh and execute that file . From the site
Run a set of commands from a command resource (file) on a remote
machine using key authentication with no passphrase
The commandResource expects single resource file execute. '(cd F:\execution; ant -f build.xml run)' is not a resource.
(I am not sure your over all goal. Looking at the number questions, I guess you need Continous Integration solution- should check jenkins with many of its plugins )
(BTW, you can all ant directly with ant -f , avoiding the need for cd)

Related

Cannot run phploc through ANT

I've looked at both these posts.. they don't help:
PHP build for Jenkins failing with 'Cannot run program "phploc"'
Cannot run phploc installed through composer
If I run phploc at a shell prompt, it works just fine. So it must be something with my build.xml file... but I don't know what.
<target name="phploc" description="Measure project size using PHPLOC">
<exec executable="phploc">
<arg path="${basedir}/../src" />
<arg value="--log-csv" />
<arg value="${basedir}/build/logs/phploc.csv" />
</exec>
</target>
This works fine:
C:\projects\project1\build>phploc ../src
phploc 2.0.6 by Sebastian Bergmann.
My folder structure is
c:\projects\project1
build
...
vendor
bin
...
src
tests
c:\projects\project1\build>ant
phploc:
BUILD FAILED
C:\projects\project1\build\build.xml:55: Execute failed: java.io.IOException:
Cannot run program "phploc": CreateProcess error=2, The system cannot find the
file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav
UPDATE
C:\>where phploc
INFO: Could not find files for the given pattern(s).
What am I missing?
From chat discussion, it came out to be a PATH related issue. That's exactly what I was thinking it to be.
Setting absolute path of phploc in <exec executable="phploc"> did the trick. It was working on command line from C:\projects\project1\build directory because phploc was in one of its sub-directory (vendor\bin) and this sub-directory was already in the PATH variable.
PATH=C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Calibre2\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\;C:\Program Files (x86)\Java;c:\ant\bin;.\vendor\bin
Note: Although providing absolute path worked here but it's a good practice to use relative path so that your project is portable. In this case, you could use basedir as the reference point for all relative paths.

Executing Python script from Phing

I have the following in my Phing build file:
<target name="fixModifiedTime">
<echo msg="Fixing file modified time" />
<exec executable="python" >
<arg value="c:\scm\scripts\git-restore-mtime.py" />
<arg value="-v" />
</exec>
</target>
This is causing the following error in my Jenkins output:
[exec] Executing command: python c:\scm\scripts\git-restore-mtime.py -v 2>&1
[exec] 'python' is not recognized as an internal or external command,
[exec] operable program or batch file.
I am able to execute the same command from the Jenkins workspace directory and it works perfectly. The Python directory is added to my environment variables and the script has the right permissions.
I have also tried adding the script to my repository and running it from within the build environment but have the same error.
Any thoughts please?
I suggest the following:
Try creating Windows batch build step in your Jenkins and put the command in it. See if it runs
It's obvious your Jenkins environment is not the same is your console. See what's missing in Jenkins
Add the python home and path in the Jenkins configuration
I hope this helps

Ant task hangs due to suspected permissions issue

I have a STAF job which kicks off an Ant script, all run under a functional ID. The ant script does basic 'init' tasks which work fine, and then hangs on one of the following tasks: delete, mkdir, or junit, depending on how much muddling I do in the file system.
<target name="run.nightly.tests" depends="init">
<delete dir="/path/results/latestDate" />
<mkdir dir="/path/results/latestDate" />
<chmod perm="777" dir="/path/results/latestDate" />
<junit printsummary="on" fork="no">
<!-- typical classpath/tests setup snipped -->
</junit>
</target>
I am able to sudo to this functional ID and run the corresponding commands from a shell just fine (ex: rm -rf /path/results/latestDate). Clearly something is different about running in the Ant environment, under the functional id. (In fact, I can run the script just fine with my own id)
/path/results has 777 permissions before my script runs, and when /path/results/latestDate exists it is owned by the same functional id w/ 777 permissions.
STAF launches this script:
export ANT_HOME=/opt/apache-ant-1.8.2
#This entire directory tree and jar files are world r+x
LIB_DIR=/home/afreed/automation/dependencies/mail
ant -debug -verbose -buildfile nightlyTest.xml -lib ${LIB_DIR}/mail.jar:${LIB_DIR}/activation.jar
I would understand if Ant would fail on any of these tasks with a permissions error but I cannot understand why it would hang.
I would like help either a) determining why there is a hang or b) how to convert the hang to a hard failure
Solved, the Ant script was hanging on an exec command that was waiting for input from STDIN. (Execution had continued past that point because it was launched with 'spawn' attribute.) When I removed this 'exec' task from my 'init' target, the Ant script ran as expected.
<!-- hanging task in init-->
<exec executable="blah.sh" spawn="true">

How to determine Jenkins build directory from Ant?

I am trying to migrate an Ant script I wrote to build and deploy projects from within the Jenkins framework (instead of triggered from an SVN post-commit hook, which was the expedient way we initially approached things). Everything is great, except I need to stage files for the deploy step and I want to stuff them into the 'build' directory Jenkins creates for the job (and since my build.xml lives in a non-project-specific location, ${basedir} and ${user.dir} do not point to the desired location).
within the Jenkins configuration, I've setup the following:
[Jenkins]
Build Record Root Directory: E:/builds/${ITEM_FULLNAME}
[Job-Specific]
Build File: C:\vc-tools\shadow\build.xml
when running a build, the script is appropriately launched and a job-specific build directory is created, e.g.
E:\builds\Test\2012-08-07_12-51-21
I want to get at this directory from within the build script, but cannot figure out how. some of the things I've tried:
[echo] ${basedir}: C:\vc-tools\shadow
[echo] ${user.dir}: C:\vc-tools
[echo] ${env.workspace}: C:\Program Files (x86)\Jenkins\workspace\Test
[echo] ${env.build_id}: 2012-08-07_12-51-21
[echo] ${jenkins_home}: C:\Program Files (x86)\Jenkins
[echo] ${BuildDir}: E:/builds/${ITEM_FULLNAME}
note: for that last one, I tried passing in:
BuildDir=E:/builds/${ITEM_FULLNAME}
as a property configured from the job within Jenkins (clearly ${} expansion doesn't take place in this context).
according to the documentation, there are no specific environment variables that are set to the full build directory path -- I can fudge it by hardcoding the E:\builds root and tacking on ${env.build_id}, but was hoping there would be an easier way to access the complete path from something Jenkins exposes (either an Ant property and an environment variable) in order to make the script more flexible.
I am using Jenkins version 1.476.
thanks
It's always a good idea for your project to have a copy of it's build logic included alongside the source code. It makes your build more portable across machines.
Having said that it's also quite common to setup build files containing common shared build logic. ANT defines the following tasks to support such activity:
include
import
So a possible solution is to store a simple build.xml file, in the root of your project directory:
<project name="my project" default="build">
<include file="C:\vc-tools\shadow\common-build-1.0.xml" as="common"/>
<target name="build" depends="common.build"/>
</project>
Notes:
It's a good idea to use a revision number in the common build file name. This assists in preserving backward compatibility with other builds using the older logic.
Update
When Jenkins runs a job is sets a number of environment variables.
The following ANT logic will print the location of the Jenkins workspace directory:
<property environment="env"/>
<target name="run">
<echo message="Jenkins workspace: ${env.WORKSPACE}"/>
<echo message="Job directory: ${env.WORKSPACE}../../jobs/${env.JOB_NAME}"/>
<echo message="Build data: ${env.WORKSPACE}../../jobs/${env.JOB_NAME}/build/${env.BUILD_ID}"/>
</target>
These days (Jenkins v. 1.484) 'run' target from answer above should look like this:
<target name="run">
<echo message="Jenkins workspace: ${env.WORKSPACE}"/>
<echo message="Job directory: ${env.WORKSPACE}/../../${env.JOB_NAME}"/>
<echo message="Build data: ${env.WORKSPACE}/../../${env.JOB_NAME}/builds/${env.BUILD_ID}"/>
</target>

Cannot run program "p4": CreateProcess error=2, The system cannot find the file specified

I'm developing automate deployment script for Coldfusion project.
Tool: cruisecontrol.net, ant script
Source control: perforce
Executing the following ant script from cruisecontrol.net i'm getting this error:
"Cannot run program "p4": CreateProcess error=2, The system cannot find the file specified"
But its working fine from command line:
ant -f deployment.xml
deployment.xml file content:
<!-- Get Latest revision from perforce -->
<echo message="Perforce code base Get Latest revision Started"/>
<p4sync port="${p4.server}"
client="${p4.workspace}"
globalopts="${p4.password}"
user="${p4.username}"
view="${p4.branch}"/>
<echo message="Perforce code base Get Latest revision completed"/>
ccnet.config:
<project name="TestMGDeployment">
<triggers>
<intervalTrigger seconds="300" />
</triggers>
<tasks>
<exec executable="C:\Apache\apache-ant-1.8.1\bin\ant.bat">
<baseDirectory>C:\cruisecontrol\Projects</baseDirectory>
<buildArgs>-f deployment.xml</buildArgs>
</exec>
</tasks>
</project>
Thanks,
Nagarajan
Your CruiseControl.net is probably running under different user account, make sure you have p4 in system PATH or specify the full path to the executable in your p4sync task.
Try running in command line instead of as a service to negate user environment definitions issue.
Check if you have setup the P4PORT environment variable. That should be set to: [your perforce server]:[perforce port].
For e.g., P4PORT=perforce.xyz.com:1666

Resources