Is there a way to add pre-build step for Jenkins pipeline?

mirza picture mirza · Mar 4, 2018 · Viewed 11.1k times · Source

Currently I'm able to use a post directive in my Jenkinsfile. Is there a way to trigger a pre-build step similar to this ?

  post {
    always {
      sh '''rm -rf build/workspace'''
    }
  }

Answer

Matt R picture Matt R · Nov 6, 2018

I believe this newer question may have the answer: Is there a way to run a pre-checkout step in declarative Jenkins pipelines?

pre is a cool feature idea, but doesn't exist yet. skipDefaultCheckout and checkout scm (which is the same as the default checkout) are the keys:

pipeline {
  agent { label 'docker' }
  options {
    skipDefaultCheckout true
  }
  stages {
    stage('clean_workspace_and_checkout_source') {
      steps {
        deleteDir()
        checkout scm
      }
    }
    stage('build') {
      steps {
        echo 'i build therefore i am'
      }
    }
  }
}