Jenkins: how can I clean the workspace except the vagrant directory? - jenkins

I have the following setup:
I use the workspace cleanup plugin in Jenkins to clean my workspace before each build. During my build-process I also trigger a vagrant up to setup a VM for phpunit tests:
$ vagrant up
$ ./runtest.sh
$ vagrant suspend
Now when I re-build the project, the VM gets build as a new one instead of just resuming the previous one. I guess this is because of the cleanup plugin removing the .vagrant-directory, therefore making Vagrant think it should build a new machine instead of just resuming the previous one.
Now I have configured the plugin to exclude the following patterns and I have the 'Apply pattern also on directories'-checkbox also checked:
**/*.vagrant
.vagrant
.vagrant/
./.vagrant
./.vagrant/
But still the .vagrant-directory gets deleted from the workspace on each new build, spawning a brand new VM each time...
Does anyone know how I can exclude the .vagrant-directory from the workspace cleanup plugin?

Turned out I was having a logical error. What I did as first task was checkout and hard-reset a specific branch to the master. So it was this step that actually deleted the .vagrant-directory after the cleanup-task did what it was expected to do.

According to the plugin documentation, it provides include and exclude patterns. You need to change the dropdown to exclude pattern (marked with arrow #2 in the diagram on plugin page), and type .vagrant in the pattern. Should probably also click "apply to directories"

Related

How can I automatically svn tag build artefacts with Jenkins?

I'm automating the following manual process with Jenkins:
Check out the trunk from svn
Build the code
Run tests
If the tests pass then tag the code and built artefacts
Steps 1-3 are working, but I need some help with tagging in step 4. There are some possible solutions that I've excluded:
The svn-tag plugin which has security issues and not developed
since 2015
The "tag this build" button which is a manual step and doesn't allow
me to select which files are tagged
Using svn command line tools on the slave, because I don't want to rely on them being install (and the same version as Jenkins), also I don't want to expose credentials to the build
Tools seem to be available for Git. Is there another way to do it for svn that I haven't thought of?
What about svn commandline?
Your could try to create a tag using shell :
svn copy http://svn/mywebsite/trunk http://svn/mywebsite/tags/2.2.1 -m "Release 2.2.1 - added new feature"
Source:
http://svnbook.red-bean.com/en/1.6/svn.branchmerge.tags.html
If it works, your step 4 could be a build step called : Execute Shell :
Just put your shell script in the text-area called command. You could use variables for your url, tag number, etc
Finally, you could create a jenkins plugin called svn command line tool to make life easier. Is not complex as many believe.

How to delete a build from Jenkins job workspace

I wonder if it is possible to remove only one build (including artifacts) from job workspace.
I tried to "Delete Build" in Build History but all it does is remove build reference from Build History table. I know I can ssh to a server and delete files from the command line but I am looking for a way to do it from Jenkins web interface.
After installing Workspace Cleanup Plugin I am able to wipe out current workspace but I want to keep my other builds in the workspace.
In your Jenkins instance, to be able to have folder/per build - set flag "Use custom workspace" in your job's settings. Here is a brief help info from the setting description:
For each job on Jenkins, Jenkins allocates a unique "workspace directory."
This is the directory where the code is checked out and builds happen.
Normally you should let Jenkins allocate and clean up workspace directories,
but in several situations this is problematic, and in such case, this option
lets you specify the workspace location manually.
One such situation is where paths are hard-coded and the code needs to be
built on a specific location. While there's no doubt that such a build is
not ideal, this option allows you to get going in such a situation.
...
And your custom directory path would look like this:
workspace\$JOB_NAME\$BUILD_NUMBER ~> workspace\my-job-name\123
where $JOB_NAME will be "my-job-name" and $BUILD_NUMBER is the build number, eq. "123".
There is one nasty problem with this approach and this is why I wouldn't recommend to use it - Jenkins will not be able to reclaim disk space for outdated builds. You would have to handle cleanup of outdated builds manually and it is a lot of hassle.
Alternative approach, that gives you more control, tools and is able to keep disk space usage under control (without your supervision) is to use default workspace settings and archive your build output (files, original source code, libraries and etc.) as a post-build action. Very-very handy and gives you access to a whole bunch of great tools like, Copy Artifact Plugin or ArtifactDeployer Plugin in other jobs.
Hope that info helps you make a decision that fits your needs best.
I also use "General/Advanced/Use custom workspace" (as in #pabloduo's answer) on a Windows machine with something like:
C:\${JOB_NAME}\${BUILD_NUMBER}
Just wanted to add a solution for getting rid of the build job's workspaces.
I use Groovy Events Listener Plugin for this.
Using the plug-in's standard configuration I just use the following Groovy script:
if (event == Event.JOB_DELETED){
new File(env.WORKSPACE).deleteDir()
}
And now the custom workspace is deleted when the build job is deleted.
Just be aware that this would also delete non-custom workspaces (because the event is triggered for all jobs on your Jenkins server).

Gradle Project Not Naming Archives Properly Under Jenkins

I have a number of Gradle builds that work very well from the command line, from buildship, etc.
However now I am porting them to a Jenkins system. And it is producing some very strange results. I'm pretty much a total newbie to Jenkins, so this may have an easy answer. So far I haven't found it.
I am using the Gradle Plugin for Jenkins, v.1.24 to configure my build in Jenkins. However, Jenkins (at least as I have it configured) organizes its build structure as {jenkins root}/data/jobs/{project_name}/workspace. When code is checked out of source control it is deposited in that directory, not in a directory named {project_name}.
Gradle seems to assume that the directory in which it is running names the project, and when I'm running outside of Jenkins this assumption is true. The name of the project that Gradle sees is the name of the project that was checked out from source control. Project.name is a gettable but not a settable property of a gradle Project. So in the Jenkins case, the archives that gradle builds are named workspace* rather than {project_name}*. It is also named workspace in the repositories it publishes into. I must be missing something very obvious but for the life of me I cannot figure out what it is.
Has anyone grappled with this?
UPDATE - It appears that the problem is that the people who designed my Jenkins instance knew nothing about Gradle. The {jenkins root}/data/jobs/{project_name}/workspace layout that I described above is not required by Jenkins, but apparently was felt to be useful for some reason in some other, non-Gradle context. So the question becomes, where is the project layout set up in the Jenkins configuration - OR - can Gradle be modified somehow to assume a different project layout/naming strategy.
Set Manage Jenkins → Configure System → Advanced... (the one right at the top) → Workspace Root Directory: ${JENKINS_HOME}/workspace/${ITEM_FULLNAME}.
The inline help:
Specify where Jenkins would store job workspaces on the master node. (It has no effect on builds run on slaves.) This value can include the following variables.
${JENKINS_HOME} — Jenkins home directory.
${ITEM_ROOTDIR} — Root directory of a job for which the default workspace is allocated.
${ITEM_FULL_NAME} — '/'-separated job name, like "foo/bar".
Changing this value allows you to put workspaces on SSD, SCSI, or even ram disks. Default value is ${ITEM_ROOTDIR}/workspace.
.../jenkins/config.xml
...
<workspaceDir>${JENKINS_HOME}/workspace/${ITEM_FULLNAME}</workspaceDir>
...
Gradle seems to assume that the directory in which it is running names the project
Yes this is gradle's default behavior, but can be easily overridden. If it is just the output artifact name you're concerned about, override the jar name with:
jar{
baseName 'actualProjectName'
}

let Tycho/Jenkins build fail when p2 site doesn't exist

I have an RCP application which I build in Jenkins using "mvn clean verify" to run Maven/Tycho. When the target platform of my application contains a p2 repository site that is not available anymore, Tycho prints a warning but still uses it's local cache.
[WARNING] Failed to access p2 repository http://download.eclipse.org/technology/babel/update-site/R0.11.1/indigo, use local cache. Neither http://download.eclipse.org/technology/babel/update-site/R0.11.1/indigo/content.jar nor http://download.eclipse.org/technology/babel/update-site/R0.11.1/indigo/content.xml found.
It took my quite some time to notice this problem because the Jenkins build succeeds without any issues and I am not going to read all console output...
I would like to get notified of this issue so I can take measures when a repository is moved or deleted. Tycho should still use it's cache under normal circumstances.
Can I solve this using Jenkins or can I instruct Tycho to fail fast (drawback: also fails on temporary outage)?
To partly answer my question the best solution is probably to set up a local p2 mirror, but I think this requires a lot more work.
You can mark a Jenkins build unstable or failed based on it's output by using the Jenkins Text finder plugin.
Manage Jenkins plugins and install the text finder plugin from the
Available plugins.
Edit the project to add a post-build action "Jenkins Text Finder".
Check 'Also search the console output', set the Regular expression to
'Failed to access p2 repository' and check 'Unstable if found'.

Jenkins creating wrong folder for the new jobs that were copied from other existing jobs

Sorry, for the confusing title. I am totally new to Jenkins and have been handed over Jenkins to maintain which was set-up by someone else.
This is Jenkins Master slave config. I have 1 Master and 3 Slaves.
When I create a new job by "copying an existing" job, the new job works fine and no issues.
QUESTION: I see that in Jenkins workspace, this new job is creating a folder with the name of the original job that it was copied from. Why it is not creating a folder with the name of the new job instead?
Now, this is certainly not a show stopper for me, but it seems that Jenkins is creating a folder in workspace for each job that is run. And hence this particular folder is causing some confusion (although notional it is).
Hence, could you help me find out why the new job is creating a workspace folder with the name of original job it was copied from.
BTW, above issue was seen on the Jenkins slave.
It can be solved by configuring the correct building workspace in jenkins job.
General > Advanced > custom workspace > "give your correct workspace"
I had the same problem:
copied some jenkins project and wondered about hard coded workspace paths
Console output of the copied project. Job failed due to missing D: drive.
12:30:44 java.io.IOException: Failed to mkdirs: D:\TEAMS\WORKSPACE\RELEASE_1_1
The problem i had: the 'Advanced project options' were not expanded and the configure GUI had an enormous width, that i didn´t see the button to expand and show the 'advanced' settings.
In fact (thanks to sti): the original project had some hard coded workspace path.
One possibility is that you accidentally triggered the wrong job. You could change the job to print the directory where it executes by adding something like:
echo "XXX $JOB_NAME running in directory $WORKSPACE"
into the build step script. Then look for XXX in the build console log.
Second possibility is that you found an old workspace of the original job. Jenkins leaves workspaces lying around just in case it needs them again so it does not have to make them from scratch.
Third possibility is the original job is configured to use a hard-coded path as workspace. (Custom workspace). If you clone such a job, it would be a good idea to change the hard-coded path. An even better idea would be to let Jenkins manage the workspace and it's naming.
And finally, if all the other possibilities have been checked, you may have found a bug. You could look for it in https://issues.jenkins-ci.org/ and create a bug report if it is a new one.

Resources