4

I have created very basic Multibranch Pipeline on my local Jenkins via BlueOcean UI. From default config I removed almost all behaviors except one for discovering branches. The config looks line follows:

config

Within Jenkinsfile I'm trying to setup following scenario:

  • Checkout branch
  • (optionally) Merge it to master branch
  • Build Back-end
  • Build Front-end

Snippet from my Jenkinsfile:

pipeline {
  agent none   
  stages {    
    stage('Setup') {
      agent {
        label "master"
      }    
      steps {
        sh "git checkout -f ${env.BRANCH_NAME}"
      }
    }

    stage('Merge with master') {
      when {
        not {
          branch 'master'
        }
      }    
      agent {
        label "master"
      }    
      steps {
        sh 'git checkout -f origin/master'
        sh "git merge --ff-only ${env.BRANCH_NAME}"
      }
    }

    stage('Build Back-end') {
      agent {
        docker {
          image 'openjdk:8'
        }
      }

      steps {
          sh './gradlew build'              
      }
    }

    stage ('Build Front-end') {
      agent {
        docker {
          image 'saddeveloper/node-chromium'
        }
      }

      steps {
        dir ('./front-end') {
          sh 'npm install'              
          sh 'npm run buildProd'
          sh 'npm run testHeadless'
        }
      }
    }
  }
}

Pipeline itself and building steps works fine, but the problem is that Jenkins adds "Check out from version control" step before each stage. The step looks for new branches, fetches refs, but also checks out current branch. Here is relevant output from full build log:

// stage Setup
 > git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh git checkout -f my-branch
// stage Merge with master
 > git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh git checkout -f origin/master
sh git merge --ff-only my-branch
// stage Build Back-end
 > git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh ./gradlew build
// stage Build Front-end
 > git checkout -f f067047bbdd3a5d5f9d1f2efae274bc175829595
sh npm install
sh npm run buildProd
sh npm run testHeadless

So as you see it effectively resets working directory to particular commit before every stage git checkout -f f067...595.

Is there any way to disable this default checkout behavior? Or any viable option how to implement such optional merging to master branch?

Thanks!

Vitaljok
  • 594
  • 1
  • 6
  • 13

1 Answers1

11

By default, git scm will be executed in a Jenkins pipeline. You can disable it by doing:

pipeline {
    agent none
    options {
        skipDefaultCheckout true
    }
    ...

Also, I'd recommend take a look to other useful pipeline options https://jenkins.io/doc/book/pipeline/syntax/#options

Cartucho
  • 3,257
  • 2
  • 30
  • 55
  • 3
    Thanks, now I see the root cause of the problem. **skipDefaultCheckout** _Skip checking out code from source control by default in the `agent` directive._ I have defined agents in each stage and by default it does checkout for each agent. However it is not very clear for me why it has such default behavior. What is the reason to perform checkout if workspace is shared anyways? – Vitaljok Dec 18 '18 at 07:48
  • 1
    I agree, I always prefer explicit behavior over obscure automagical features. – Cartucho Dec 18 '18 at 12:57
  • The reason is pretty clear: If you use a different image for each stage, your SCM files are not present and need to be checked out, if you need them. When pushing artifacts from one image to the next one with stash, you could omit the checkout. – Mick Belker - Pseudonym Dec 09 '19 at 10:22