Jenkins Simple Multibranch Pipeline

Configure Credentials

This is optional if you intend to leave you repository public for now.

Configure label for node

  • The pipeline we use spcifies which agent should run it (I use workers in my example):
1agent { label 'workers' }
  • So, I'd label my controller with that
  • Go to:
    Dashboard->Manage Jenkins ->Nodes
  • You should see your only node there:
    Click on it, and choose Configure from the left menu
  • Add a label, and save

Create your service repository

  • Start by cloning this repository
  • Delete your remote, and add a remote of your own, so that you will push to your own repository in your github account.

Add branches

  • You should have 2 branches:
    • master (or **main if you wish)
    • develop
  • Both branches should have the same content, including the Jenkinsfile(s).
  • Make sure you also push to your repository in github

Create a multibranch job

  • Create a multibranch pipeline job (name it), save
  • Configure (click Configure on the left menu)
  • Add source:
    • If credentials are needed, add them
    • add HTTPS url
    • wait for the scan to succeed
  • run both branches, make sure build is successful

Different operations in different branches

  • We'll add a 2nd stage that will be run only in the develop branch
  • Add the following code to you Jenkinsfile:
 1      stage('cat') {
 2            when {
 3                branch 'dev*'
 4            }
 5            steps {
 6                sh '''
 7                cat README.md
 8                '''
 9            }
10        }
  • Merge, so that it is in both branches
  • Try building in both branches