Search code examples
jenkinscontinuous-integrationmultibranch-pipeline

How git merge works for multibranch pipeline?


I am setting up a multi-branch pipeline in Jenkins for my application. I am unsure about how different jenkinsfile checked into the repository handles merging.

For example, I have 3 branches, master, develop and feature-1, all those branches require a different pipeline configuration (see below).

master: 1. build 2. test 3. deploy to PROD 4. tell everyone using email

develop: 1. build 2. test 3. deploy to DEV

feature-1: 1. build 2. test

From day 1, master has Jenkinsfile for production release, develop was branched with the same Jenkinsfile content, changes were made to deploy to DEV and removed last step, committed change. Day 2: feature-1 branched off develop, changes were made to remove deploy step and committed.

When feature-1 is completed, we are merging feature-1 into develop branch, since the commit that removed last step is dated after the develop commit, this is going to result in Auto-Merge and will make "develop" branch not deploy. Same will happen when develop is merged into master

Based on above potential issues, I think I must be doing something wrong here, what is the best practice for managing Jenkinsfile in Multi-branch pipeline?


Solution

  • Implement some flow control (https://jenkins.io/doc/book/pipeline/syntax/#flow-control) and maintain the same Jenkinsfile in all your branches.

    if (env.BRANCH_NAME == 'master'){ echo 'do all the things' } else if (env.BRANCH_NAME == 'develop'){ echo 'do a few things' } else if (env.BRANCH_NAME == 'feature-1'){ echo 'do a couple things' }

    The BRANCH_NAME information is exposed by Jenkins when using Multibranch Pipelines (https://jenkins.io/doc/book/pipeline/multibranch/#additional-environment-variables).