Jenkins: keep only the last file in a directory - jenkins

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.

Related

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

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

Trigger Jenkins Build on Perforce submit by keyword

I am wondering if anyone knows if it is possible to trigger a Jenkins build on a Perforce commit, only if it has a certain keyword in it?
My use case is that I am looking to find a way to trigger a Jenkins build that will build a Nuget package of the library I am working on, and place it in the correct Nuget server directory. I don't want to build a Nuget package each and every time I make a submit to Perforce, so I was wondering if it is possible to set up some kind of trigger to only run the Jenkins build if some keyword was included in the changelist description.
Does anyone know if this is possible, or any other possible ideas on how to implement this?
This works for me. I'm sure it could be more efficient, but it works. This looks for the string "buildit" in the changelist description.
First, create a change-submit trigger in Perforce, e.g.,
buildit change-submit //... "/usr/bin/buildthis %changelist%"
(wherever you place it should be writable, and create a file called "description" that should also be writable).
Below is the contents of the buildthis script:
#!/bin/sh
changelist="$1"
P4PORT=perforce:1666
P4CLIENT=myclient
P4USER=myuser
p4 -p $P4PORT -c $P4CLIENT -u $P4USER describe $changelist >/usr/bin/description
if grep -q buildit /usr/bin/description;
then
curl -X POST http://jenkinsserver:port/job/jobname/build?token=TOKEN
else
exit 0
fi

Jenkins "Console Output" log location in filesystem

I want to access and grep Jenkins Console Output as a post build step in the same job that creates this output. Redirecting logs with >> log.txt is not a solution since this is not supported by my build steps.
Build:
echo "This is log"
Post build step:
grep "is" path/to/console_output
Where is the specific log file created in filesystem?
#Bruno Lavit has a great answer, but if you want you can just access the log and download it as txt file to your workspace from the job's URL:
${BUILD_URL}/consoleText
Then it's only a matter of downloading this page to your ${Workspace}
You can use "Invoke ANT" and use the GET target
On Linux you can use wget to download it to your workspace
etc.
Good luck!
Edit:
The actual log file on the file system is not on the slave, but kept in the Master machine. You can find it under: $JENKINS_HOME/jobs/$JOB_NAME/builds/lastSuccessfulBuild/log
If you're looking for another build just replace lastSuccessfulBuild with the build you're looking for.
Jenkins stores the console log on master. If you want programmatic access to the log, and you are running on master, you can access the log that Jenkins already has, without copying it to the artifacts or having to GET the http job URL.
From http://javadoc.jenkins.io/archive/jenkins-1.651/hudson/model/Run.html#getLogFile(), this returns the File object for the console output (in the jenkins file system, this is the "log" file in the build output directory).
In my case, we use a chained (child) job to do parsing and analysis on a parent job's build.
When using a groovy script run in Jenkins, you get an object named "build" for the run. We use this to get the http://javadoc.jenkins.io/archive/jenkins-1.651/hudson/model/Build.html for the upstream job, then call this job's .getLogFile().
Added bonus; since it's just a File object, we call .getParent() to get the folder where Jenkins stores build collateral (like test xmls, environment variables, and other things that may not be explicitly exposed through the artifacts) which we can also parse.
Double added bonus; we also use matrix jobs. This sometimes makes inferring the file path on the system a pain. .getLogFile().getParent() takes away all the pain.
You can install this Jenkins Console log plugin to write the log in your workspace as a post build step.
You have to build the plugin yourself and install the plugin manually.
Next, you can add a post build step like that:
With an additional post build step (shell script), you will be able to grep your log.
I hope it helped :)
Log location:
${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_NUMBER}/log
Get log as a text and save to workspace:
cat ${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_NUMBER}/log >> log.txt
For very large output logs it could be difficult to open (network delay, scrolling). This is the solution I'm using to check big log files:
https://${URL}/jenkins/job/${jobName}/${buildNumber}/
in the left column you see: View as plain text. Do a right mouse click on it and choose save links as. Now you can save your big log as .txt file. Open it with notepad++ and you can go through your logs easily without network delays during scrolling.
I found the console output of my job in the browser at the following location:
http://[Jenkins URL]/job/[Job Name]/default/[Build Number]/console
This is designed for use when you have a shell script build step. Use only the first two lines to get the file name.
You can get the console log file (using bash magic) for the current build from a shell script this way and check it for some error string, failing the job if found:
logFilename=${JENKINS_HOME}/${JOB_URL:${#JENKINS_URL}}
logFilename=${logFilename//job\//jobs\/}builds/${BUILD_NUMBER}/log
grep "**Failure**" ${logFilename} ; exitCode=$?
[[ $exitCode -ne 1 ]] && exit 1
You have to build the file name by taking the JOB_URL, stripping off the leading host name part, adding in the path to JENKINS_HOME, replacing "/job/" to "/jobs/" to handle all nested folders, adding the current build number and the file name.
The grep returns 0 if the string is found and 2 if there is a file error. So a 1 means it found the error indication string. That makes the build fail.
Easy solution would be:
curl http://jenkinsUrl/job/<Build_Name>/<Build_Number>/consoleText -OutFile <FilePathToLocalDisk>
or for the last successful build...
curl http://jenkinsUrl/job/<Build_Name>/lastSuccessfulBuild/consoleText -OutFile <FilePathToLocalDisk>

What's the working directory in Jenkins after executing a shell command box?

I'm looking at a Jenkins job and trying to understand it.
I have an Execute shell command box in my Build section:
> mkdir mydir
> cd mydir
>
> svn export --force https://example.com/repo/mydir .
When Jenkins is done executing that command, and moves on to the next build step, what is its working directory?
workspece-root/ or workspace-root/mydir ?
As the next step, I have Invoke top-level Maven targets (still in the Build section).
What I really want to know is: why does that execute successfully?
Is it because Jenkins automatically moves back to the workspace-root/ folder after executing a shell command box, or is it because the next job is a "top-level" job, and Jenkins therefore changes back to the workspace-root/?
Each build step is a separate process that Jenkins spawns off. They don't share anything, neither current directory, nor environment variables set/changed within the build step. Each new build step starts by spawning a new process off the parent process (the one running Jenkins)
It's not that Jenkins "move back" to $WORKSPACE. It's that Jenkins discards the previous session.
I lately saw that if you print the CWD , I would get the Project_NAME.
E.g
D:\jenkins\workspace\My_Project
Any script you might be running wont be found. Hence we can do a "CD path" before we start out scripts.
Slav's explanation is very good and I thought of complementing it by providing a real world example that shows how multiple Windows batch commands look like even if they work in the same directory:
Command 1
REM #ensures that all npm packages are downloaded
cd "%WORKSPACE%"
npm install
Command 2
REM #performs a prod-mode build of the project
cd "%WORKSPACE%"
ng build --prod --aot=true -environment=pp
So, each one ensure that current working directory points to the current project directory.

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