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.

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.

    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.