Search code examples
jenkinsmultibranch-pipeline

How to merge before build in a multi-branch pipeline?


For simple pipeline jobs I have the "merge before build" under Additional Behaviours. But how do I do that in a multi-branch pipeline? Can't find that option.

Thanks in advance.


Solution

  • I will just post here what I did through code:

    def mergeBeforeBuild() {
    
      // Pull the source and test a merge
      try {
        def sourceScmCheck = checkout changelog: true, poll: true, scm: [
          $class: 'GitSCM',
          branches: [[name: "origin/$sourceBranch" ]],
          doGenerateSubmoduleConfigurations: false,
          extensions: [[
            $class: 'PreBuildMerge',
            options: [
              fastForwardMode: 'FF',
              mergeRemote: 'origin',
              mergeStrategy: 'default',
              mergeTarget: "${env.target_branch}"]],
            [$class: 'UserIdentity',
              email: '[email protected]',
              name: 'jenkins'
          ]],
          submoduleCfg: [],
          userRemoteConfigs: [[
            credentialsId: 'githubCredentials',
            name: 'origin',
            url: "$repoUrl"
          ]]
        ]
      } catch (error) {
        currentBuild.result = 'FAILURE'
        echo "ERROR: ${error}"
        sh 'false'
      }
    }