Branches and Pull Requests

In the previous section a Jenkinsfile which could be checked into source control was implemented. This section covers the concept of Multibranch Pipelines which build on the Jenkinsfile foundation to provide more dynamic and automatic functionality in Jenkins.

Creating a Multibranch Pipeline in Jenkins

Creating a Multibranch Pipeline

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.

This eliminates the need for manual Pipeline creation and management.

To create a Multibranch Pipeline:

  • Click New Item on Jenkins home page.

Jenkins Classic UI left column showing options: New Item, Build History, Project Relationship, Check File Fingerprint, Manage Jenkins and My Views.
  • Enter a name for your Pipeline, select Multibranch Pipeline and click OK.

Jenkins uses the name of the Pipeline to create directories on disk. Pipeline names which include spaces may uncover bugs in scripts which do not expect paths to contain spaces.

Jenkins New Item page with item name as 'multibranch-example' and 'Multibranch Pipeline' option selected from the options for item type given as follows: Freestyle project, Pipeline, Multi-configuration project, Folder, Multi-branch Pipeline and Organization Folder.
  • Add a Branch Source (for example, Git).

Configure page of 'multibranch-example' pipeline. In the 'Branch Sources' section 'Git' is selected from the 'Add Source' dropdown menu.
  • Enter the location of the Git Repository.

Configure page of 'multibranch-example' pipeline. Branch Source configuration section for 'Git' branch source option is opened with options to add project repository link, Credentials, Behaviors and Property Strategy.
  • Save the Multibranch Pipeline project.

Upon Save, Jenkins automatically scans the designated repository and creates appropriate items for each branch in the repository which contains a Jenkinsfile.

By default, Jenkins will not automatically re-index the repository for branch additions or deletions (unless using an Organization Folder), so it is often useful to configure a Multibranch Pipeline to periodically re-index in the configuration:

Configure page of 'multibranch-example' pipeline. In the 'Scan Multibranch Pipeline Triggers' section the 'Periodically if not otherwise run' option is checked and the Interval is set to '30 minutes'.

Additional Environment Variables

Multibranch Pipelines expose additional information about the branch being built through the env global variable, such as:

BRANCH_NAME

Name of the branch for which this Pipeline is executing, for example master.

CHANGE_ID

An identifier corresponding to some kind of change request, such as a pull request number

Additional environment variables are listed in the Global Variable Reference.

Supporting Pull Requests

Multibranch Pipelines can be used for validating pull/change requests with the appropriate plugin. This functionality is provided by the following plugins:

Please consult their documentation for further information on how to use those plugins.

Using Organization Folders

Organization Folders enable Jenkins to monitor an entire GitHub Organization, Bitbucket Team/Project, GitLab organization, or Gitea organization and automatically create new Multibranch Pipelines for repositories which contain branches and pull requests containing a Jenkinsfile.

Organization folders are implemented for:



Was this page helpful?

Please submit your feedback about this page through this quick form.

Alternatively, if you don't wish to complete the quick form, you can simply indicate if you found this page helpful?

    


See existing feedback here.