Jenkins: how to run builds in unique directories - jenkins

I am configuring Jenkins for the first time, have been a user of it until now.
I read the documentation and understand what "$WORKSPACE" means on the slave.
I had a misconception that by default the jobs on Jenkins Slave run in a unique directory per job inside the workspace directory. This doesnt seem to be the case.
How do I assign a unique directory per job run?
Will Jenkins automatically cleanup this job run directory after its done?

By default jenkins runs from $WORKSPACE/JOB Name ; here WORKSPACE is defined in the node configuration. This could be changed by choosing Advance Project options -> Use custom workspace under your job configuration.
Regarding cleanup: Choose Build Environment -> Delete workspace before build starts under jobs configuration if you like to cleanup for each new job.

Jenkins will create workspace directory by default if jobs is successfully executed at-least once

Related

When do the jenkins workspaces get preserved?

I have a bunch of pipeline jobs, yet when executed, workspaces of some get preserved, some are deleted. How does jenkins make these decisions?
Based on my findings so far:
All jobs executed on nodes will have their workspace persisted, e.g. /home/ec2-user/workspaces/some-job
Some works on master keep their workspaces but some others' workspaces disappear after the job has finished. For example, after my build job succeeded, if I ssh in I can see the its workspace directory; but all my e2e jobs have no workspace.
Note I didn't use any of clearWs, deleteDir etc in my pipelines.
By the way, the reason I'm looking into workspaces is the disk usage keeps increasing and I want to cleanup. I thought the workspace is overwritten each time a job runs, but yet I get the 'Disk space is too low' warning several times.
Jenkins is creating a new workspace for every build job (= run) per default. You can see that in the path of the ws in your console log: /here/is/the/ws#buildnumber. If you dont want to have that behavior you can set it to an path which is for instance for every repo the same: How to set specific workspace folder for jenkins multibranch pipeline projects
Maybe some of your jobs don't get executed on the Jenkins Master, but on some connected Node (via an agent directive within your Jenkinsfile or Pipeline description). If that's the case you won't see a build directory inside the workspace for this Job on the Jenkins master, but on the connected Node.
You would only get the build results (like artifacts, reports, etc.) under /<JENKINS_HOME>/jobs/My_Job/ on the Master.
Remember that you could trigger a Jenkins build on a node also indirectly if you, for example, run the build within a Dockerfile and have configured (within Jenkins configuration) a specific node label for execution of Docker builds.

Unique workspace directories for build executors

I've got a Jenkins instance running with multiple build executors.
All of which have the workspace in /tmp/workspace.
I've got a new build executor and I want it to have the workspace in a different location. (somewhere with a little more space).
At this option you can configure your path of executing the job for that particular job. At Directory you can set the path.

Jenkins Slave build locations

I have just added a slave to my Jenkins build - with the idea that I can now deploy artefacts to either my dev server or my test server.
However i've now hit a problem.
When I deploy a job on the master slave, the job build directory is
$JENKINS_HOME/localmoduledirectory (as defined in the build job)
However when I deploy my job via the slave the build directory is different which breaks my jobs. The build directory is
$JENKINS_HOME/workspace/build job title/localmoduledirectory
I know I can change the workspace root directory location for the master under configure settings /advances .. so can change it to $JENKINS_HOME/workspace, but I want to stop the slave using the build job title in the path.
The end result I'm after is to have jenkins, building / deploying from the same location on two servers i.e /opt/jenkins/workspace/localmoduledirectory.
Any ideas ?
ok after lots of head scratching ...
managed to discover that the mvn plugin has a custom workspace option hidden under advanced. so configured all jobs with a customer workspace of /opt/jenkins.

How to copy build XMLs from Master to the Slave node in Jenkins?

I have created a Jenkins job which used to run in the master and I have the build.xml file in the master.
Now I have added a slave node and added the setting Restrict where this project can be run so that my job always runs on a particular slave.
Now my build jobs are failing and I can see:
[EnvInject] - Loading node environment variables.
Building remotely on demo_slave_inst2 (slave1) in workspace /root/slave/workspace/demo_job
FATAL: Unable to find build script at /root/slave/workspace/demo_job/autobvt.xml
Build step 'Invoke Ant' marked build as failure
Recording test results
Finished: FAILURE
This autobvt.xml file already exists in the master. So looks like I need to copy this file over to the slave node manually which does not looks like quite handy.
How I can instruct jenkins to copy this as part of the build?
Use "Copy data to Workspace" http://wiki.jenkins-ci.org/display/JENKINS/Copy+Data+To+Workspace+Plugin using which you can copy the files from master to slave and run them as a part of build process (No manual effort needed!)
I sorted the issue using the Copy to Slave plugin.

Way to clone a job from one jenkins to another

I have two Jenkins, both are master. Both have 5 salve Jenkins each. I have one job on first jenkins that needs to be cloned for each job.
I can clone the job on first jenkins and its slave but not on second master jenkins. Is there a way to clone a job from one jenkins to another?
I have one more question can I archive the job at some defined location other than master jenkins, May be on slave?
I assume you have a job called "JOB" on "Jenkins1" and you want to copy it to "Jenkins2":
curl JENKINS1_URL/job/JOB/config.xml | java -jar jenkins-cli.war -s JENKINS2_URL create-job
You might need to add username and password if you have turned on security in Jenkins. The jenkins-cli.war is available from your $JENKINS_URL/cli.
Ideally you should make sure you have the same plugins installed on both Jenkins1 and Jenkins2. More similar you can make the two Jenkins masters, the fewer problems you will have importing the the job.
For the second part of your question: slaves don't store any Jenkins configuration. All configuration is done on Master. There is a lot of backup plugins, some backup the whole Jenkins, some backup just job configuration, some backup individual jobs, export them to files, or even store/track changes from SCM such as SVN.
So "archiving job configuration to slave" simply makes no sense. But at the end of the day, a job configuration is simply an .xml file, and you can take that file and copy it anywhere you want.
As for the first part of the question, it's unclear what you want. Do you want to clone a job automatically (as part of another job's process), programmatically (through some script) or manually (through the UI, other means)?
Edit:
Go to your JENKINS_HOME directory on the server filesystem, navigate to the jobs folder, then select the specific job folder that you want.
Copy the config.xml to another server, this will create the same job with the same configuration (make sure your plugins are same)
Copy the whole job_name folder if you want to preserve history, builds, artifacts, etc

Resources