Remove a directory in jenkins before the git checkout happens - jenkins

I am using the cleanup feature in Jenkins, which delete the previous build directory and create a new one every time. This is great, except that I need to maintain certain files in the build directory, so I am trying to delete just the source folder that contain the code.
The problem is that when the build start, the first thing that happen is the git checkout of the code, which means that if I put a delete command in the jenkins script area, it will delete the directory that was checked out, and that obviously won't work.
Is there a way to tell Jenkins to perform commands before the git checkout happen? Or to cleanup selectively the build folder, so Jenkins know what to keep and what to delete?

Use the pre-scm-buildstep plugin. It will let you do all sorts of things prior to touching your SCM.

Related

Jenkins - Deleting artifacts automatically

JENKINS
I am noticing that the every time I run one of my jobs in Jenkins, there are two files created in the /workspace/build/distributions dir. The two files have the extensions of .tar and .tgz. Every time, I run the job, another set of these files are created. So, if I run the job 3 times, there will be 6 files all together. I have noticed that during the dependency check phase, these artifacts slow things down. Therefore, I wanted to remove them automatically before each time this job runs. I have attempted the configs in the image below. In addition, I have tried the workspace cleanup plugin and that completely deleted the workspace. That is definitely not what I wanted.
Therefore, what would be the best way to go about this.
What scm plugin are you using? Some of the scm plugins allow you to clean the workspace before an update (e.g. SVN's "Emulate clean checkout" and Git's "Clean before checkout" options).
If you're not using a scm plugin, can you remove the files in a batch/shell script during the first build step?
Or perhaps you can go about it from the reverse direction. Can you get rid of the files as the last build step of the job? That way, they are gone when the next build comes along.

How to show git diff in Jenkins

I have a build job in Jenkins, which is based on a git project.
Looking at the changes page for each build, I can see the commits that are new to this version, who contributed them, and the list of files modified.
What I'm missing is the option to see the actual diff - what lines of code were changed.
How can this be achieved?
I'm using a local git repo hosted on the same PC as the jenkins server
You can use Last Changes Plugin to accomplish that.
Just install it and make sure to choose from Post-build Actions drop down menu the option Publish Last Changes.

Hudson - How can I do a "SVN clean up"?

I have made a checkout in my directory of an SVN repository. The project take a lot of time to be completely checked. And so while creating my Hudson job I need this in order :
Clean up the directory (this resolves some ambiguous problems such as : "Hudson workspace locked while building" )
Revert the changes
Update
The choices that I have for Check-out Strategy, in the Hudson job creation form, are:
Use svn update as much as possible, with 'svn revert' before update
Use 'svn switch' as much as possible
Use 'svn update' as much as possible
Clean checkout folders and then checkout
Emulate clean checkout by first deleting unversioned/ignored files, then 'svn update'
Clean workspace and then checkout (Eliminated)
What is the right option for my case?
Thank you a lot!
If your build is done correctly, you should be able to simply do use 'svn update as much as possible. This is the fastest way to update your files. This means not modifying committed files, or placing build artifacts in directories where they will interfere with the build process. In a Java shop, simply keeping all built objects in a subdirectory (we use target to match Maven, but others use build or diet) and out of the way of the rest of the process.
Most people do a clean as part of their build step. This, in theory, should not be necessary, and doing so will lengthen build times. The idea of the build is not to do any unnecessary work. If a source file isn't changed, the corresponding object file should not need to be rebuilt. However, Java is pretty fast at compiling, that most Java projects simply wipe the build directory clean. In C projects, not deleting old objects is better since it really reduces build time.
If there is a problem with your build process where use 'svn update' as much as possible can't work, you should fix your build process. However, there are a couple of projects on our old Jenkins server that do have problems, and they simply aren't updated enough to worry about it. For those, I do Always checkout a fresh copy. This takes the longest, but if you're having problems with your build process, I wouldn't bother using emulate a clean checkout by first deleting unversioned/modified files and use svn revert first. These can cause update conflicts, and cause problems with your build. Either get the build working correctly, or do a clean checkout.
I would go with "Use svn update as much as possible, with 'svn revert' before update". If that is not sufficient, check out the EnvInject Plugin. It can run a script before the SCm checkout happens. You can use it to run a svn cleanup for your job, before the Subversion plugin takes over with the revert and the update. Caveat, you need to install some kind of SVN command line client on your build server.

how to make jenkins use an already checked out codebase?

I am just starting with Jenkins 1.487 and wanted to integrate Jenkins in my Ant project. But while configuring it, I can't find any way to make Jenkins re-use an already checked out codebase, instead of downloading a fresh copy relative to its workspace root. Is there a way to do that ?
I tried to specify a custom workspace manually (where my codebase was already checked out), and clicked on 'Build now'. The result was that it wiped out my current checked out code saying
"Checking out a fresh workspace because there's no workspace at /home/daud/Work
Cleaning local Directory ."
Not even a warning..
If you really want to build from an existing checkout somewhere on the file system, then do not use "Source Code Management" section of Jenkins. Leave it as "none"
Go straight to the "Build" section
Click "Add Build Step"
Select Invoke Ant"
Click Advanced
And under "Build File", provide a full path to the ant build file on your file system. You would have to include the drive letter (if on Windows) or a leading / (if on Linux) to break from the Workspace (by default, this path is relative to Workspace). Or use a lot of ../../../.. if needed.
But like others have said, this is not the way a CI system is supposed to be used
The idea behind Jenkins and CI is that it works on a fresh copy of the codebase. Every build done by Jenkins should not depend on any external preconditions and it should be reproducible.
You might want to try using the Clone Workspace SCM Plugin for Jenkins. It will allow you to zip up the workspace from one job and use it to create the workspace for another one. I've used this for downstream jobs that need to act on the work from a previous job.
This is also helpful if you're using something like Git for source control and want to avoid a second Git clone (or SVN checkout). Furthermore, you can limit the content of zip file that is used to recreate the workspace, for example to avoid carrying unnecesary files (e.g. the .git or .svn directories) downstream.

Hudson build scripts location - recommendation?

I'm already finishing my project build automation :) with Hudson and Nant.
My project structure is something like
$/Project
build.scripts
script1.build
script2.build
build.properties.xml
Code
Project1
Project2
So Hudson downloads from the root $/Project to the workspace folder.
And everything is ok since the build.scripts are in the workspace, I run them very easily, however what is bugging me is the fact that since the build scripts are inside the workspace, then I can't program Hudson to run automatically either based on time or changes because it will always detect changes to the files (note build.properties.xml which I check out and check in at build time to store some stats).
Where do you recommend these files to go in and still get the advantage of having them source-controlled?
What I ended up doing is to NOT check-in changes to those files. I changed my CI workflow to create another file (local to the workspace only) where the changes are written to.
This way, I still get the last build info written somewhere to pick it up, and avoid the issue of Jenkins detecting the change.
PS: I changed from Hudson to Jenkins since I saw that most plugins ran away from the former. The transition was too smooth to be true.

Resources