'is there any easy way to remove (clean) the pipeline ws, when using another node?

When using a jenkins pipeline, on an ephemeral node (e.g. fargate):

pipeline {
    agent {
        label 'build-swarm'
    }

    stages {
    ...
    }

    post {
        always {
             cleanWs()
        }
    }

the ws cleanup [plugin][1] will try and remove the ws on the ephemeral node, which is pointless.

In an ideal world, we would use lightweight checkout on the controller, but because reasons this is not possible. So we have a fairly large repo checkout, that is not cleaned up.

This is the best thing I've managed to come up with:

pipeline {
    ...
}

node('master') {
    folder = JOB_NAME.split('/')[0]
    job = JOB_NAME.split('/')[1]
    ws("${JENKINS_HOME}/jobs/${folder}/jobs/${job}/workspace@script") {
        stage('clean up ws') {
            cleanWs()
        }
    }
}

which seems to work, but feels very fragile. Am I missing something obvious? [1]: https://plugins.jenkins.io/ws-cleanup/

https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#node-allocate-node



Solution 1:[1]

If I got you right you need to run cleanWs() step before pipeline run. Use:

pipeline {     
  agent any     
  options {
  // This is required if you want to clean before build
    skipDefaultCheckout(true)
  }
  stages {
    stage('Build') {
      steps {
        // Clean before build
        cleanWs()
        // We need to explicitly checkout from SCM here
        checkout scm
        echo "Building ${env.JOB_NAME}..."
      }
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitriy Tarasevich