Open In App

How To Clean Jenkins Workspace?

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Jenkins is an open-source tool that is used to automate the build, test, and deploy stages of software. The Jenkins server always creates new workspaces on each new build to store the project code, build artifacts, log files, test files, and temporary files. Here in this guide, I will first discuss what is Jenkins. Then I will discuss Jenkins workspace and why it is important to clean Jenkins workspace. After this, i will guide you through the different steps to clean a Jenkins workspace of a particular Jenkins job.

What is Jenkins?

Jenkins is an automation tool used for DevOps practice. It automates the build, test, and deploy stages of software development. Jenkins has a very friendly UI. It also provides many plugins to automate various stages. For example, it provides privacy and was plugins for testing purposes. Jenkins accelerates the software development cycle. By automating various stages and tasks, Jenkins helps any software development team to reduce any type of manual errors and improve efficiency. Jenkins is evolving and becoming more reliable with its every new update. In summary, we can say Jenkins has become an essential tool for organizations to improve and accelerate their software development cycles.

Why To Clean Jenkins Workspace?

Jenkins Workspace is a directory on the Jenkins server where the project code and all other dependency files are stored. Whenever users build a new job, the Jenkins server by default makes a new workspace for that job to store the project code fetched from a version control system like Github, Gitlab, etc. It is important to clean Jenkins Workspace for the following reasons :

  • Over time new files and artifacts accumulate in the Jenkins workspace. It takes unnecessary disk space. So to free disk space, workspace cleanup is necessary.
  • The build process will be faster if the workspace is cleaned.
  • Over time new files and old files accumulate in the workspace. These new and old files can create conflict during building a job which may lead to build failure.
  • There is a chance that on building a job, some sensitive files may be stored in the workspace. Cleaning of the workspace may lead to solving such security and vulnerability issues.

Pre-requisites

Before moving to the next section make sure you have installed Jenkins on your system. If Jenkins is not installed, then follow these detailed geeks for geeks articles to install Jenkins on your system.

Steps To Clean Jenkins Workspace

Step 1 : Go to Jenkins dashboard . Here create a sample freestyle project .

freestyle-pipeline

Step 2 : Now write a script which will generate some files in the workspace . (windows users should use windows batch commands and ubuntu users should use shell to write the script )

echo "hello geeks" > file1.txt
echo "Jenkins is easy" > file2.txt
echo "this is a sample job" > file3.txt
echo "this script is used in my GeeksForGeeks article"

script

Step 3 : Now after this build the project by selecting Build Now .

build

Step 4 : Select workspace to see the files .

workspace

Step 5 : Now to clean the workspace select Wipe Out Current Workspace and then click ok .

clean

Now in your workspace you will not see any files . This workspace clean up should be done before performing any new build as to ensure that there is no conflict between present files and old files . Until now we have cleaned workspace manually , now follow the steps below to automate workspace clean up on each new build .

Step 6 : Select configure . Then go to a section called Build Environment . Here select Delete workspace before build starts .

select-delete-workspace

From now, on each new build , the workspace is cleaned up . Basically all the files of old build are deleted .

Step 7 : Now update the script . Here i have only updated the file1.txt contents .

echo "hello geeks. This is an article on How to clean Jenkins Workspace " > file1.txt
echo "Jenkins is easy " > file2.txt
echo "this is a sample job" > file3.txt
echo "this script is used in my GeeksForGeeks article"

script-update

Step 8 : Now build the project and go to its workspace .

new-build

Step 9 : Now click on the file1.txt to see its content . You will see the updated content .

updated-content

Conclusion

Here in this guide you have first learned what is Jenkins . Then you have learned the various reasons to clean up Jenkins workspace . After this you have created a sample job , where you have used a script to generate some files in the workspace . Then finally you have followed the steps for manual clean up and automatic clean up of Jenkins workspace .

Clean Jenkins Workspace – FAQ’s

What is Jenkins Workspace ?

Jenkins workspace is a directory on Jenkins server , which stores the project code , build artifacts , log files , test files .

What are the potential risks you will face for not cleaning workspace regularly ?

You will face many potential risks such as build failure , security vulnerability , performance degradation and many more if you are not cleaning Jenkins workspace regularly .

Which files need to be deleted from workspace before the new build ?

Artifacts , temporary files , any file containing security related content should be removed from the workspace before any new build proceeds .

What is the difference between a workspace and a build ?

Jenkins build is the execution of jobs . This means to fetch code from a version control system , running build commands and generating the build artifacts . On the other hand Jenkins workspace is where the build process starts . These workspace contains the project code and other files that is necessary to build the job .

Do all Jenkins jobs create workspace by default ?

Yes all the Jenkins jobs create workspace by default .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads