Bastien
Bastien

Reputation: 1011

Jenkins Pipeline: is it possible to avoid multiple checkout?

I've moved a few old Jenkins jobs to new ones using the pipeline feature in order to be able to integrate the Jenkins configuration within the git repositories. It's working fine but I'm asking myself if there is a way to reduce the number of checkout that happens while building.

Setup

Problem

When I push to my remote branche BRANCH_1, the multibranch jenkins job is triggered and my understanding is that the following steps happen:

So for building my branch, I end up with one git fetch and two git checkout.

Questions

Thanks you all

Upvotes: 23

Views: 17203

Answers (3)

gratinierer
gratinierer

Reputation: 2498

it is possible to avoid a second checkout with skipDefaultCheckout:


pipeline {
    options {
        skipDefaultCheckout true
    }
      agent any
         stages {
            stage('Hello2') {
                steps {
                    sh '''
                        sleep 60
                        '''
                }
            }
        }
}

Upvotes: 0

Tareq Sha
Tareq Sha

Reputation: 516

I've run into this several times and the robust solution that I came with was define a tiny "launcher script" inside the job itself (without scm source) that checks-out the correct source revision and loads the actual pipeline from the sources.

If you are using the DSL plugin to general your job, you'll define the pipeline this way:

pipelineJob("myjob") {
  ...
  definition {
    cps {
      script('''
        node {
          checkout scm
          load("path/to/script.groovy")
        }
      ''')
    }
  }
}

If your are configuring the job manually using the jenkins "Configure" screen, this is identical to choosing "Pipeline script" instead of "Pipeline script from SCM" and copying the small checkout-and-load script inside the box.

This decouples the pipeline bootstrap from the actual SCM and allows you to checkout-out once and have both the pipeline definition and the sources to be built. Not the most beautiful approach but definitely does the job well.

Upvotes: 0

fdd
fdd

Reputation: 226

With plain git Jenkins has to do two checkouts: one to get the Jenkinsfile to know what to execute in the job, and then another checkout of the actual repository content for building purposes. Technically Jenkins only needs to load the one single Jenkinsfile from the repo, but git doesn't allow checkout of a single file. Therefore the double checkout cannot be avoided with plain git using the multibranch plugin.

If you host git on Bitbucket or GitHub then you avoid the double checkout by using their specific Jenkins plugins instead of the multibranch plugin.

See the Jenkins plugin site for Bitbucket and GitHub plugins accordingly.

These plugins use the respective Git provider's REST API to load the single Jenkins file. So you technically still have a double checkout, but the first one is a simple REST call to download a single file, rather than doing a full native git checkout of the whole repository.

Upvotes: 13

Related Questions