13

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'''
    }
  }
mirza
  • 5,685
  • 10
  • 43
  • 73
  • Can't you just make your first stage the pre step? – Jon S Mar 04 '18 at 19:31
  • Actually I'm trying to add a skip ci step. It works for pull requests but it does not work main branches like a direct commit to master even has '[ci skip]' it's still being triggered. -> https://wiki.jenkins.io/display/JENKINS/GitHub+Commit+Skip+SCM+Behaviour+Plugin – mirza Mar 04 '18 at 19:38
  • 2
    Possible duplicate of [Is there a way to run a pre-checkout step in declarative Jenkins pipelines?](https://stackoverflow.com/questions/45348629/is-there-a-way-to-run-a-pre-checkout-step-in-declarative-jenkins-pipelines) – froblesmartin Oct 24 '19 at 12:36

1 Answers1

7

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'
      }
    }
  }
}
Matt R
  • 161
  • 2
  • 7
  • 2
    I'm looking for the same concept. But the problem here is that I want to perform a task even previous agent docker is setup on the master server. With this method the "pre-task" will be run inside docker – RuBiCK Jan 22 '19 at 14:42
  • you should be able to declare actions on a seperate node for certain steps using node('master') { steps { doSomething() } } – Matt R Apr 24 '19 at 09:21