jenkins,

Cleanup workspace directory in Jenkins

Jul 06, 2023 · 1 min read · Post a comment

Starting a CI job with a clean empty workspace directory ensures not having to deal with flaky tests in a way. Here’s how to do it.

Prerequisites

  • Jenkins

Solution

Step 1. Install Workspace Cleanup plugin.

Step 2. Use the function as part of your Jenkinsfile. For instance:

pipeline {
    agent any
    
    stages {
        stage('Clean Workspace') {
            steps {
                script {
                    // Clean the workspace
                    cleanWs()
                }
            }
        }
        
        stage('Build') {
            steps {
                // Your build steps go here
            }
        }
        
        // Additional stages...
    }
}

Conclusion

In case you face any issues, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.