What is the location of .sh file of the jenkins job? - jenkins

I am looking for the location of .sh file which is generated my jenkins job and can be found in console output. This .sh files starts with jenkins word.
I am using AWS instance for jenkins.
i was looking for /temp folder, but didn't get into /tmp folder.
sample of the .sh file name:
[first_gitbuild] $ /bin/sh -xe /tmp/jenkins8581903656897699418.sh

These files are deleted when job finishes.
I was able to get a copy of this .sh files with these steps:
Set a sleep in my script ( 5 mins )
Build the job
Go to /tmo folder and copy paste the script : /tmp/jenkins......sh

Related

How to run a jar file generated by maven in Jenkins Job Builder

I have a maven project (assume it is just a simple hello-world java program) in git. Now I want to (1) create the jar file; (2) run this jar file. How can I do this through Jenkins Job Builder hourly (like every one hour Jenkins will build the jar file, and execute it)? Thanks.
First of all, create a Jenkins Freestyle Project.
TZ=Asia/Kolkata
0 */1 * * *
Please add the above code in Build periodically box. It will run your code once an hour. For creating the jar file,
cd '<your project location in the disk>'
mvn clean install
Now this will build your jar file. To run the jar file,
cd 'target'
java -jar <project jar file name>.jar
So total script will be like :
cd '<your project location in the disk>'
mvn clean install
cd 'target'
java -jar <project jar file name>.jar
Add the above code to Execute shell block in Build -> Add build step -> Execute shell in the job configuration. Hope this is what you are looking for.

Jenkins: keep only the last file in a directory

I need a job in Jenkins that every time is running keeps only the last file created in a folder and discard all the older ones.
I'm not sure how to do it.
Any suggestion welcome
Add an execute shell step after your build step. And read this post. It will delete other than 10 latest files. In your case which is 1 file. So modified code looks like
cd <PATH HERE TO YOUR FOLDER>
ls -1tr | head -n -1 | xargs -d '\n' rm -f --
I think you have xargs installed. Hope this helps.
Understanding the fact that every time jenkins job run, it clears out the workspace. So in order to keep the last file created you might want to copy file to a different location on the jenkins box apart from /workspace directory and later restore it under the same directory. You can overwrite the file every time.
You can achieve this with a pre/post execute shell script in the jenkins job.

Jenkins pipeline: write file in different directory than workspace

I use this in my Jenkins pipeline:
def script = libraryResource 'tests/checks-test.sh'
file: 'script.sh', text: script
This will write a file script.sh in my workspace. Now I want that this file isn't in my workspace. (A bit like managed files which are in the /tmp/ folder of jenkins.
How can I force my pipeline to write the file in the /tmp folder and not in my workspace?

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 how to rename war file

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.

Resources