Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear workspace in Jenkins pipeline before job starts

I need to clear workspace before build starts. I tried using cleanDir() in stages, but in the declarative pipeline, check out happens first and when stage with cleadDir runs, checked out code also gets cleared which is not desired. How can we clear the workspace before check out in the declarative pipeline?

like image 582
rashidcmb Avatar asked Sep 08 '17 14:09

rashidcmb


2 Answers

Actually, I have to revise my answer based on recent changes to the pipeline plugins, e.g. GitHub Branch Source Plugin 2.2.0 with JENKINS-43507.

Besides the different branch discovery behaviours, which can be configured, one can now define additional steps to take, including Clean before checkout (and Clean after checkout):

enter image description here

The resulting output in the pipeline execution will then be

Cleaning workspace
  > git rev-parse --verify HEAD # timeout=10
Resetting working tree
 > git reset --hard # timeout=10
 > git clean -fdx # timeout=10

so, pretty close to the calling git clean yourself.

like image 115
StephenKing Avatar answered Nov 06 '22 21:11

StephenKing


stage('Git') {
            steps {
                step([$class: 'WsCleanup'])
                checkout scm
            }
        }

the WsCleanup does the trick

like image 14
Mor Lajb Avatar answered Nov 06 '22 21:11

Mor Lajb