0
0
Jenkinsdevops~15 mins

Workspace cleanup in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Workspace cleanup
📖 Scenario: You are managing a Jenkins job that builds software projects. Over time, the workspace folder where Jenkins runs builds can fill up with old files. This can cause disk space issues and slow down builds.To keep the workspace clean, you want to add a step in your Jenkins pipeline to delete all files and folders in the workspace before starting a new build.
🎯 Goal: Build a Jenkins pipeline script that cleans the workspace folder at the start of the build using the deleteDir() command.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add an agent any directive to run on any available agent
Add a stage named Cleanup Workspace
Inside the stage, use the steps block to call deleteDir() to clean the workspace
Print a message Workspace cleaned after cleanup
💡 Why This Matters
🌍 Real World
Cleaning the workspace before builds prevents leftover files from previous builds causing errors or using too much disk space.
💼 Career
Jenkins pipeline scripting is a key skill for DevOps engineers to automate build and deployment processes reliably.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Create a Jenkins pipeline script with a pipeline block and an agent any directive.
Jenkins
Need a hint?

The pipeline block is the root of the Jenkins pipeline script. The agent any means the job can run on any available Jenkins agent.

2
Add a stage named Cleanup Workspace
Add a stage named Cleanup Workspace inside the pipeline block with an empty steps block.
Jenkins
Need a hint?

The stage groups steps in the pipeline. The steps block contains the commands to run.

3
Add the workspace cleanup command
Inside the steps block of the Cleanup Workspace stage, add the command deleteDir() to delete all files and folders in the workspace.
Jenkins
Need a hint?

The deleteDir() command removes all files and folders in the current workspace directory.

4
Print a confirmation message after cleanup
After the deleteDir() command inside the steps block, add a echo command to print Workspace cleaned.
Jenkins
Need a hint?

The echo command prints messages to the Jenkins console output.