Jenkins how to rename war file - jenkins

I have deployed a war file in a remote machine using Jenkins. Now I want to rename the war file through jenkins before it extracts the work folder? How can this be done? I tried post deployment action -> execute shell and mv file.war to new-file.war but it returns an error saying : mv: cannot stat `file.war': No such file or directory.
Suppose there was something wrong with my path it would not even have gone to remote location. but for me, after scp' ing it to remote location thru jenkins, and when i try to do a mv, it fails.. What could the reason be??

Adding additional Step of Execute shell during Add build Step or Add post-build action stage, normal renaming shell command mv can be used to rename artifacts.
Note: Make sure use the correct path(Relative to project/workspace root)

Your mv command is probably executed in another directory than the one you are expecting.
To know the directory your script is running in without reading the jenkins / plugin documentation add
echo "pwd of script is: " `pwd`
to your shell script and inspect the output of the jenkins build - that way you can be sure about the directory the script is run in.

Related

Get sourcecode into Jenkins WORKSPACE subdirectory

Is it possible to configure Jenkins to get source code into a subdirectory of a %WORKSPACE%? Right now the source gets pulled into %WORKSPACE% and for the build output I explicitly specify a directory outside of the %WORKSPACE%.
Ideally I would like to have something similar to this:
%WORKSPACE%\source for source code and %WORKSPACE%\artifacts for build outputs. Is it possible to have this configuration?
Create a 'run batch command' build step and use xcopy, this is presuming jenkins is running on a Windows machine, if it's a deployment directory then make it a post build step.
cd c:/
xcopy /Y "c:/program files 86/junkies/workspace/app" "c:/path to new directory"
This is just a guess at your directories, replace with correct ones, the /Y forces it to be overwritten every time it's copied.

Read DSL from file in Jenkins outside of workspace

I know its possible to run a .dsl file from an external source instead of just writing the code of the flow in the job's description, but every time I try to run lets say:
/home/flows/flow_script.dsl
I get the following error:
java.io.FileNotFoundException:/home/flows/flow_script.dsl (No such file or directory)
The path is correct, I can see the file through that path from the shell, but it doesnt let me select anything outside the "builds workspace" apparetly.
I recently ran into this very issue: my DSL script was outside of my workspace (installed via a package). The problem is that the DSL Scripts path is an Ant format that only allows specific patterns (and not absolute paths).
My workaround is hacky, but it did work: add an Execute Shell step before the "Process Job DSLs" step that symlinks the external directory into the workspace.
Something like this:
echo "Creating a symlink from /home/flows to workspace"
ln -sf "/home/flows" .flows
Then you can set the DSL Scripts path to ".flows/flow_script.dsl".
This has some additional caveats, of course: the directory you're symlinking from will need to be accessible by the jenkins user. And it likely violates a lot of best practices.

Jenkins cannot run program "phing" - Phing command execution failed

I'm new to jenkins and phing and having what appears to be a basic error with jenkins not being able to run my phing commands. Console log of my failed build is below. I only have one install of phing so I understand I don't need to "configure the job to choose one", I've also tried to add a PATH to my jenkins global environment variables and restarted Jenkins after adding the variables and still no go. I already have Phing in my local PATH as I can invoke the phing command anywhere. I'm also able to invoke the same phing command that jenkins does from terminal and the the build finishes. However, Jenkins' Build Now always fails. Any help would be great! thanks :)
Started by user anonymous
Building in workspace /Users/Shared/Jenkins/Home/jobs/php-template/workspace
Fetching changes from the remote Git repository
Fetching upstream changes from ssh://git#mystash.server:7999/myproject/myrepo.git
Checking out Revision 8fb6f843d6fd232cdde1684e18c455cef6f20460 (origin/master)
looking for '/Users/Shared/Jenkins/Home/jobs/php-template/workspace/build.xml' ...
use '/Users/Shared/Jenkins/Home/jobs/php-template/workspace' as a working directory.
[workspace]
$ phing -buildfile /Users/Shared/Jenkins/Home/jobs/php-template/workspace/build.xml clean try_ParallelTasks -logger phing.listener.DefaultLogger
FATAL: command execution failed.Maybe you need to configure the job to choose one of your Phing installations?
java.io.IOException: Cannot run program "phing" (in directory "/Users/Shared/Jenkins/Home/jobs/php-template/workspace"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at hudson.Proc$LocalProc.<init>(Proc.java:244)
at hudson.Proc$LocalProc.<init>(Proc.java:216)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:773)
at hudson.Launcher$ProcStarter.start(Launcher.java:353)
at hudson.Launcher$ProcStarter.join(Launcher.java:360)
at hudson.plugins.phing.PhingBuilder.perform(PhingBuilder.java:215)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:804)
at hudson.model.Build$BuildExecution.build(Build.java:199)
at hudson.model.Build$BuildExecution.doRun(Build.java:160)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:585)
at hudson.model.Run.execute(Run.java:1670)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:231)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
... 15 more
Build step 'Invoke Phing targets' marked build as failure
To be clearer, as Cweiske pointed out, Jenkins fails the build because it cannot find the correct phing executable, as it looks for it in the wrong path (the one in the workspace).
Anyway, Jenkins search for that executable in other paths too, as the one you cited: "in my local PATH as I can invoke the phing command anywhere".
The problem with Your Local Path is that it is the YOUR and not the Jenkin's one. As Jenkins act on your Mac with its own user (jenkins, created during the installation process), so it cannot use Your Local Path.
Understood the problem, there is more than one possible solution (and i've tried them one by one with no success!), but the simpler (and the one that finally works!) is the "symlinking".
A note: please, take care of the difference between various types of "links" on Mac: SymLinks are different and not equals to Aliases or to Hard Links (http://macs.about.com/od/faq1/f/What-Are-Aliases-Symbolic-Links-And-Hard-Links-In-Mac-Os-X.htm).
Coming back, as in the usr/bin folder there are executables that each user on the mac can use, let's create a symlink into this folder to the executable you use every time, the one included in the folder in Your Local Path.
From Terminal, go to the usr/bin folder. Use the command "cd" to change the current directory and to go to the top of the root use something like:
> cd ../
With the command "pwd" you can see the complete path to the directory in which you currently are, and with the "ls" command you can list all the files and folders in it: use them to guide yourself in the folders tree.
So, assuming you already are in the usr/bin folder and assuming you are using MAMP as webserver, the command is:
> ln /Applications/MAMP/bin/php/php5.5.10/bin/phing ./phing
As you can see, the first parameter is the path to the executable you want to symlink, and the second parameter is the path to the folder in which you want to create the symlink. Don't forget to append the name of your new symlink!
Once done, try again to build with Jenkins: all should work well now... i hope!
Some troubleshooting:
1) You created the symlink in the wrong folder: use "rm" to remove a file
2) You haven't the right permissions: use "sudo" to act as a super admin
> sudo ln /Applications/MAMP/bin/php/php5.5.10/bin/phing ./phing
The directory in which the phing script is installed is not in $PATH of the jenkins user.
Symlink it into /usr/bin/, and you should be set.
From the following error message
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
I got this "IOException when starting a process"
It seems like ProcessBuilder doesn't use the env variable PATH, so it can't find find phing
What I did is I removed Invoke Phing targets and added the Build as Execute Shell, and in Command I added /usr/local/bin/phing. and that is working for me.

Executing custom shell script to build in jenkins

I have setup a jenkins job to build my project. I have a jake.sh file in my project and the code is pulled from github. I want "npm install" command to be executed and then jake.sh to be executed once the the code is checked out.
How can I configure this in jenkins? I have tried givin ./jake.sh and jake.sh in Build->Execute Shell section
According what you tell I think the problem can be
The script is not marked as a executable. In this case add in Build -> Execute Shell (in case you have linux) sudo chmod 777 path_to_script/jake.sh.
The script is not in the base directory. Remembeber that when you execute a bash script, the current directory is /path_to_job/workspace. So you have first to move to the script folder (cd path_to_script) or specify the path when running it: ./path_to_script/jake.sh.
I hope this solves your problem.
A workaround for shell scripts can be to run the script as
bash ./jake.sh
instead of
./jake.sh
Then you don't have to do chmod. Useful when you wipe the workspace before every build.
In the same manner, if you have a nodejs shell script or python script, you can run node myscript.js / python myscript.py.

Jenkins and SCP

I have set up a jenkins build and everything is working fine except the very last step.
The whole build creates a directory called: build
This directory contains a web-inf and all the files in it I would like to publish via SCP to a different location, so that all the content of the build/web-inf folder will become the content of the target folder.
The settings for jenkins scp plugin are (it is a post-build step):
source: build/web-inf/**
destination: public_html/
that results in:
public_html/build/web-inf/...
but should be:
public_html/...
(the keep hierarchy box is ticked)
how can I make that happen??
EDIT
I could solve the problem without any additional script. The solution is so simple that my question turned out to be stupid.
All I did was telling ant to copy all the webfiles to ./public_html instead of ./build/web-inf/ what made the jenkins scp copy all files from public_html to public_html exactly as it was intended to.
If your goal is just to SCP files generated during the build, and the plugin doesn't seem to be working (I couldn't see anything wrong in your configuration) you can use an "Execute shell" build step and type the scp command something like (try it in a shell first in your job's build directory to get the syntax right):
scp -r build/web-inf/* user#host:/destination-directory

Resources