Search code examples
jenkinsjenkins-pipelinemultibranch-pipeline

Is there a way to build jobs only for pull request events in multibranch-pipeline?


I migrated from ghprb (https://plugins.jenkins.io/ghprb/) to multibranch-pipeline.

  • It seems that ghprb is no longer under development.
  • It also has limited functionality.

Anyway, so I changed it to multibranch-pipeline, but when creating a pull request, the branches are scanned together and unnecessary builds are performed twice.

First of all, I added the following option to disable the burdensome step, but it's not neat.

when {
     changeRequest()
}

I don't want the build against the branch to run. Any good way?


Solution

  • When you create a Multi branch pipeline you have several Behaviors options - these options will determine how, or more precisely when, your pipeline is going to be triggered (assuming the relevant origin has a Jenkinsfile in the defined path).
    In addition, each behavior has a specific strategy that can be chosen and will affect the outcome.

    The default behaviors are Discover branches, Discover pull requests from forks, and Discover pull requests from origin. Ill ignore the forks option as it is more relevant for Opensource Git workflows.

    • Discover pull requests from origin - Discovers pull requests where the origin repository is the same as the target repository. It means that for every pull request that is opened a new pipeline will be created with the pull request Id and it will be triggered upon the creation and for any future change in the source code related to the pull request. When the pull request is closed the pipeline will be deleted.
      Regarding strategy you can choose to build the current code in the pull request (without merging), the code after it is merged with the target branch or both of the options.
      When to use? when you want to run a validation flow (pipeline) before merging your code.

    • Discover branches - Discovers all branches in the configured repository and creates a dedicated pipeline for each branch. Whenever the source code of that branch is changed (new code is pushed) the pipeline will be triggered.
      Usually this options is set with the Exclude branches that are also files as PRS strategy - to avoid duplicate runs if the change was already tested as part of a pull request.
      When to use? When you want to run a validation flow, like unit tests or compilation, for every code change on all your branches and get fast feedback.

    There are more behaviors available like Discover Tags and the ability to run via a defined Regex, you can also use the Exceptions and Defaults section to create all kinds of combinations like: run on all pull requests and on branches that start with RELESE. So it is quite flexible to allow easy adaption to your needs. In addition the Jenkins Declarative pipeline comes with build-in conditions to allow you to detect if your are in a branch flow or in a pull request flow, and have one Jenkinsfile that is used for all different flows.

    Back to your question, if you want your pipeline to run only for pull requests, and not run for any branch, remove all behaviors and leave only the Discover pull requests from origin behavior.
    Also consider using the strategy Merging the pull request with the current target branch revision to make sure you actually test the result of the pull request merge.