Thursday, December 26, 2024
Google search engine
HomeData Modelling & AIAutomatically clean up Jenkins Workspace after Builds Complete

Automatically clean up Jenkins Workspace after Builds Complete

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

Jenkins usually creates a workspace for your builds automatically and uses it to clone code, build node_modules in case it is a JS application as well as construct the entire application directories within it. In a serious organization where builds are happening often as new branches are created, this directory can soon grow and eat up your disk space. In order to remedy you from this situation, we assume there are those applications that do not need the contents of the workspace after the build is done and it makes sense to clean up the directory after every build is completed. For programming languages like JavaScript, it can be understandable to leave the node modules because it might be cache for future builds. Either way, it depends with how your application is configured and expected to work.

If you are in the group that would wish their workspace to be cleaned up after a build is complete, this is the guide for you. Follow the steps below to configure automatic workspace deletion after your build and deployment is done.

Step 1: Install Workspace Cleanup Plugin

In the most recent Jenkins versions “Workspace Cleanup Plugin” comes installed by default. You can check it by clicking on “Manage Jenkins” > “Manage Plugins” > Click on the “Installed” tab then search for “workspace cleanup“. If it is installed, you should see it in the list as shown below:

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

workspace jenkins show it installed

In case your version does not have this plugin installed, you can follow the following steps to get it. Login to Jenkins, click on “Manage Jenkins” > “Manage Plugins” > Click on the “Available” tab then search for “workspace cleanup“. You will see various plugins listed. Click on the checkbox for “Workspace Cleanup“, plugin then click on install without reboot tab below the page. In this example, we cannot be able to see that because this installation of Jenkins came with the plugin already installed. The screenshots for the above steps are shared below.

Manage Jenkins

jenkins manage jenkins

Manage Plugins

jenkins manage plugins

Available

workspace jenkins check worspace plugin in available tab step1

Let the installation progress then click on “Go back to the top page” once there is a “Success” on each installation item. We are now ready to go ahead and try out the workspace clean up plugin. Stay tuned

Step 2: Making use of the Plugin

Once the plugin is installed successfully from the previous step, we are now ready to make use of it in our builds so that we can conserve our disk space and keep our server clean and performant.

In this step, we are going to use “Pipeline Syntax” tool that comes with Jenkins to generate a configuration we will use to clean up our workspace. First, let us create a new Pipeline, which will bring up the “Pipeline Syntax” option. Login to Jenkins, click on “New Item“.

kubernetes jenkins new item step4

Give the new item a good name then click on “Pipeline” then “OK“.

kubernetes jenkins give new item a name then choose pipeline step4

On the configuration page, click on “Pipeline“.

kubernetes jenkins click on pipeline on the config page step4

You will see a link named “Pipeline Syntax“. Click on it and it should take you to a new tab.

kubernetes jenkins click on the pipeline syntax step4

Therein, under “Steps“, click on the drop-down menu and choose “cleanWS: Delete workspace when build is done” item as shared below:

workspace jenkins select cleanWS step2

Once it is selected, click on the advanced tab where you will be able to fine tune the settings of the plugin when it runs.

workspace jenkins select cleanWS click on advanced step2

Therein, you can add “Patterns for files to be deleted” which will include or exclude certain directories as you will prefer. For example, you can decide to spare “node_modules” folder in a JavaScript application as in this example.

workspace jenkins cleanWS with settings inputed step2

After the fine tuning has been done to your satisfaction, you can go ahead and click on “Generate Pipeline Script” as above to view what you will be using in your script.

As you can see from the image shared above, the script is as below:

cleanWs deleteDirs: true, patterns: [[pattern: 'node_modules', type: 'EXCLUDE']]

Step 3: Sample Jenkinsfile

Now that we have a script to include on our script, let us see a sample Jenkinsfile script that we can use with the “cleanWS” plugin added.

pipeline {
    agent any
    environment {
        projectName = 'Cleaning The Workspace'
    }
    stages {
        stage('Checkout repository') {
            steps {
                // You can choose to clean workspace before build as follows
                cleanWs()
                checkout scm
            }
        }
        stage ('Build Application') {
            steps {
              sh 'npm i'
              sh 'npm run build'
            }
        }

//Other steps can be added here

        stage("Deploy Application"){
            steps{
                sh "kubectl apply -f deployment.yaml"
              //Then clean the workspace after deployment ignoring node_modules directory
                cleanWs deleteDirs: true, patterns: [[pattern: 'node_modules', type: 'EXCLUDE']]
            }
        }

When the stage for the clean up arrives you should see a log section in the console similar to the one shared in the screenshot below:

workspace jenkins cleanup message in console step3

And sure enough, you can verify the contents of your workspace before and after thus:

Before the cleanup

# ls -al
total 704
drwxr-xr-x.    3 jenkins jenkins   4096 Aug  1 00:34 build
-rw-r--r--.    1 jenkins jenkins    163 Aug  1 00:32 Dockerfile
-rw-r--r--.    1 jenkins jenkins    285 Aug  1 00:32 generate-react-cli.json
-rw-r--r--.    1 jenkins jenkins   1973 Aug  1 00:32 Jenkinsfile
drwxr-xr-x.    2 jenkins jenkins   4096 Aug  1 00:32 deployment.yaml
-rw-r--r--.    1 jenkins jenkins   1888 Aug  1 00:32 nginx.conf
drwxr-xr-x. 1119 jenkins jenkins  36864 Aug  1 00:34 node_modules
-rw-r--r--.    1 jenkins jenkins   1632 Aug  1 00:32 package.json
-rw-r--r--.    1 jenkins jenkins 636590 Aug  1 00:34 package-lock.json
drwxr-xr-x.    3 jenkins jenkins   4096 Aug  1 00:32 public
-rw-r--r--.    1 jenkins jenkins   3724 Aug  1 00:32 README.md
drwxr-xr-x.    9 jenkins jenkins   4096 Aug  1 00:32 src

After the cleanup

# ls al
total 40
drwxr-xr-x. 2 jenkins jenkins 36864 Aug  1 00:42 node_modules

Concluding Remarks

There we go guys, We hope the guide was as informative as we intended it to be and in case there is a better way of doing the same, kindly let us know as we continue to learn from each other. Before we close, receive our hearty gratitude for your kind support and your motivating feedback and comments. We appreciate and we are thankful as you spend your time on the blog.

Video Courses For Learning Jenkins:

Other guides that might interest you are hereby shared:

How To Install Jenkins Server on Kubernetes | OpenShift

How To Install Jenkins on Rocky Linux 8

How To Configure Jenkins FreeIPA LDAP Authentication

How To Add Multiple Kubernetes Clusters to Jenkins

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments