8

I have a Jenkins pipeline in which I build in one stage and test in another. I'd like them to be different machines since they have different capabilities. I have something like the following so far:

pipeline {
  agent none
  stages {
    stage('Build') {
      agent { node { label 'builder' } }
      steps {
        sh 'build-the-app'
        stash(name: 'app', includes: 'outputs')
      }
    }
    stage('Test’) {
      agent { node { label 'tester' } }
      steps {
        unstash 'app'
        sh 'test-the-app'
      }
    }
  }
}

The problem I have is that since it runs on two different nodes, by default it checks out the repository on both nodes. I'd like for it to not do that because it's not needed. So I added the following option:

options { skipDefaultCheckout() }

But now no repositories are checked out, causing the build to not work. How do I, after specifying skipDefaultCheckout, manually tell the pipeline to checkout the repository in stages which I specify?

1
  • Can you show where exactly in the config did you add the option? (maybe add it commented out?) Commented Aug 9, 2017 at 3:29

2 Answers 2

8

You can use the checkout scm step whenever you need the source:

pipeline {
  agent none
  options { skipDefaultCheckout() }
  stages {
    stage('Build') {
      agent { node { label 'builder' } }
      steps {
        checkout scm
        echo 'build-the-app'
        stash(name: 'app', includes: 'outputs')
      }
    }
    stage('Test') {
      agent { node { label 'tester' } }
      steps {
        unstash 'app'
        echo 'test-the-app'
      }
    }
  }
}
2

You can also as an alternative set the option to false for the stage where you want to check out the git:

pipeline {
  agent none
  options { skipDefaultCheckout(true) }
  stages {
    stage('Build') {
      agent { node { label 'builder' } }
      options { skipDefaultCheckout(false) }
      steps {
        echo 'build-the-app'
        stash(name: 'app', includes: 'outputs')
      }
    }
    stage('Test') {
      agent { node { label 'tester' } }
      steps {
        unstash 'app'
        echo 'test-the-app'
      }
    }
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.