Multibranch Jenkins Pipelines Basics

(this post if part of the material I cover in my devops course)

Multibranch Pipelines

These pipelines automatically "build" the code for each branch that has a Jenkinsfile in it.
For example:

  • In a feature branch, you may want to prepare (build image? run container?) and run unit tests alone
  • in a develop branch, you may want to prepare more, and run some integration tests as well
  • in master/main branch you will also want to deploy your code to production

Access token and credentials

  • First, create an access token in github that allows you to do everything for this repository
    • go to: your github account->Settings->Developer settings->Personal Access Tokens
    • both types can do (classic and fine-grained), I prefer the fine grained
    • Generate a new token, give it a name and choose an expiration time
    • choose Only select repositories and select your repository
    • Give it all repository permissions (read/write)
    • When you see the secret text, make sure you keep that, because it will be there just once.
  • Go to: Dashboard->Manage Jenkins->Credentials->System->Global Credentials
  • Add a new credentials of the type Secret Text, of global scope, no ID, add a short description for it (e.g: myrep-PAM)
  • Create another credential based on this text, now of type Username with Password.
    Use your github user, and the secret text as password.
    description example: myrep-content

Creating a multibranch pipeline job

  • Click: NewItem -> (give it a name) -> Multibranch Pipeline -> OK
  • Fill: Display Name + description
  • Add source:
    • When you add source, you choose among several options (git, github, bitbucket etc.)
    • each option corresponds to the specific plugin that would handle accessing the specific repository.
      We'll demonstrate with github, so choose that
    • add HTTPS url of a repository that you have already prepared (that is, multiple branches with Jenkinfiles)
    • Add the myrep-content credential you have created above
    • hit Save

Basic usage

  • After creating a multibranch pipeline, it automatically scans all branches in the repository, and finds all of those branches with a Jenkinsfile.

    Multi-Branch pipeline branch scan

  • If you go bacj to the pipeline status, you will see a list of the branches found
    (remember that these are the branches you'd like to build, branches with Jenkinsfile):

    Branches view

  • You cannot "build" the multi-branch pipeline, you can build only branchse in it.

  • Go into the master branch to see this:

    master branch

  • Hit the "Build Now" button to build each branch.

In the next post we will trigger buidling branch pipelines from github.