HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to check out GitHub repo after specifying skipDefaultCheckout in Jenkins declarative pipeline?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
afterspecifyingdeclarativeskipdefaultcheckoutjenkinsgithubhowcheckoutpipeline

Problem

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?

Solution

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'
      }
    }
  }
}

Code Snippets

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'
      }
    }
  }
}

Context

StackExchange DevOps Q#1683, answer score: 8

Revisions (0)

No revisions yet.