Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-72907

Triggering Bitbucket Pull Request Builds on the correct branch in Freestyle projects

      (Sorry if the solution to this problem is already in the documentation. It does not seems super clear in the documentation how this is supposed to work.)

      In the Bitbucket Server Integration (4.0.0) plugin there is a feature to trigger Jenkins builds when Pull Requests are opened.

      Naturally, we would like to build the source branch of the pull request (to run unit tests for example). However, for Freestyle projects is unclear how to configure pull request triggers to build the source branch.

      In Freestyle projects, Bitbucket pull request triggers can be set up as follows:

      1. In the Source Code Management section, we select Bitbucket Server, and fill in the details:

      (note: if we add no Branch Specifier, there will a default one pointing to the main branch => this will always result in building the default branch; we can set an "any" for the Branch Specifier => but, that results in building random/all branches)

      2. In the Build Triggers section, we select "Bitbucket webhook trigger" with the trigger type "Pull request event":

      This works, but new Pull Requests trigger builds the default/main branch, which is not useful.

      Can somebody help me how to configure these Bitbucket pull request triggers to build the source branch of the pull request?

          [JENKINS-72907] Triggering Bitbucket Pull Request Builds on the correct branch in Freestyle projects

          Attila added a comment -

          Attila added a comment - mhenschke_atlassian , jaguilar3_atl , khughes , FYI.  

          Hello atokes 

          Freestyle jobs are generally not very well suited to that type of setup and we would recommend you to use a multibranch pipeline instead as it is much better suited for this.
          I don't see what your branch specifier is, so I don't know if it is supposed to build anything but your main branch.

          Remember that there are two distinct parts you need to configure for this to work

          The trigger, which is when to build, for Freestyle plans this is typically "Push event"
          The second part is what to build and here is where you've run into trouble. You need to configure your Freestyle job to build branches that matches your pull request branch pattern.

          Again I would like to stress that for pull request type builds your best bet with least amount of setup and problem would be the multibranch pipeline. It is certainly possible to do with other solutions but it will be more work on your end to configure everything just right.

          In order to help you at all, you need to also post your branch specifier as well.

          Daniel Kjellin added a comment - Hello atokes   Freestyle jobs are generally not very well suited to that type of setup and we would recommend you to use a multibranch pipeline instead as it is much better suited for this. I don't see what your branch specifier is, so I don't know if it is supposed to build anything but your main branch. Remember that there are two distinct parts you need to configure for this to work The trigger, which is when to build , for Freestyle plans this is typically "Push event" The second part is what to build and here is where you've run into trouble. You need to configure your Freestyle job to build branches that matches your pull request branch pattern. Again I would like to stress that for pull request type builds your best bet with least amount of setup and problem would be the multibranch pipeline. It is certainly possible to do with other solutions but it will be more work on your end to configure everything just right. In order to help you at all, you need to also post your branch specifier as well.

          Attila added a comment - - edited

          Hello dkjellin !

          We have tried several branch specifier variants, but in none of the cases the Source Branch information from the Pull Request trigger is used.

          In simple terms:

          • we want to run builds on Pull Request before merging them to the Main branch
            • the Source Branch of the PR-s is of form PROJ-xxxxx (x being digits)
              • note that this makes using branch specifiers with patterns not suitable - we want to build the and ONLY the Source Branch of a given pull request - we do not want to build any branch that matches PROJ-*
          • the Bitbucket Server Integration plugin supports Pull Request Triggers apparently both on Freestyle and Pipeline projects
            • Pull Requests from Bitbucket will trigger builds in Jenkins (this part works)
            • the Pull Request event published by Bitbucket an consumed by the Jenkins plugin has the Source Branch information (I assume)
              • what is missing, is the ability to use the Source Branch information in the Jenkins job
              • note that it is possible to build user specified branches with Freestyle projects - the branch specifier allows using variables such user specified parameters - we want is similar, but we want to use Source Branch information from pull request trigger event (but this is currently not exposed by the plugin)

          PS We are aware of multi-branch pipeline projects, but it is not a viable option for us.

          Thanks,
          Attia

          Attila added a comment - - edited Hello dkjellin ! We have tried several branch specifier variants, but in none of the cases the Source Branch information from the Pull Request trigger is used. In simple terms: we want to run builds on Pull Request before merging them to the Main branch the Source Branch of the PR-s is of form PROJ-xxxxx (x being digits) note that this makes using branch specifiers with patterns not suitable - we want to build the and ONLY the Source Branch of a given pull request - we do not want to build any branch that matches PROJ-* the Bitbucket Server Integration plugin supports Pull Request Triggers apparently both on Freestyle and Pipeline projects Pull Requests from Bitbucket will trigger builds in Jenkins (this part works) the Pull Request event published by Bitbucket an consumed by the Jenkins plugin has the  Source Branch information (I assume) what is missing , is the ability to use the Source Branch information in the Jenkins job note that it is possible to build user specified branches with Freestyle projects - the branch specifier allows using variables such user specified parameters - we want is similar, but we want to use Source Branch information from pull request trigger event (but this is currently not exposed by the plugin) PS We are aware of multi-branch pipeline projects, but it is not a viable option for us. Thanks, Attia

          Hi Attila, I'm a developer for Bitbucket DC.

          If you've completely ruled out multi-branch pipelines, it's hard to find a good solution for you as the behaviour you're seeing in Freestyle jobs is the intended behaviour. Freestyle jobs, without configuration are non-reactive to whatever triggers them (scm triggers, cron triggers etc.)- they simply register it as a queue to check if any refs matching their branch specifier have been changed, and if so to build them. Multibranch pipelines specifically solve this issue, as the trigger ensures one job for every source branch is created, and only they are built on source branch event, producing the outcome you're expecting.

          To your suggestion about using an env variable to set the source branch, that does seem like a possibility to solve your workflow- that issue is tracked in JENKINS-60340. Because the behaviour here does not appear to be a bug however, I'm going to close this ticket.

          Martin Henschke added a comment - Hi Attila, I'm a developer for Bitbucket DC. If you've completely ruled out multi-branch pipelines, it's hard to find a good solution for you as the behaviour you're seeing in Freestyle jobs is the intended behaviour. Freestyle jobs, without configuration are non-reactive to whatever triggers them (scm triggers, cron triggers etc.)- they simply register it as a queue to check if any refs matching their branch specifier have been changed, and if so to build them. Multibranch pipelines specifically solve this issue, as the trigger ensures one job for every source branch is created, and only they are built on source branch event, producing the outcome you're expecting. To your suggestion about using an env variable to set the source branch, that does seem like a possibility to solve your workflow- that issue is tracked in JENKINS-60340 . Because the behaviour here does not appear to be a bug however, I'm going to close this ticket.

          Attila added a comment -

          Hi mhenschke_atlassian!

          We are not ruling out using multi-branch pipelines, but that would imply a migration with significant effort, so we are trying to avoid it.

          For the moment the most viable solution for us seems to be to implement this kind of Pull Request triggers manually. This would mean creating an application / tool that listens for Pul Request events from BitBucket, and based on the payload triggers the Jenkins Freestyle jobs with the appropriate branch parameter.

          Feel free to close the ticket. Although, from our point of view this is still a bug...

          Thanks,
          Attila

          Attila added a comment - Hi mhenschke_atlassian ! We are not ruling out using multi-branch pipelines, but that would imply a migration with significant effort, so we are trying to avoid it. For the moment the most viable solution for us seems to be to implement this kind of Pull Request triggers manually. This would mean creating an application / tool that listens for Pul Request events from BitBucket, and based on the payload triggers the Jenkins Freestyle jobs with the appropriate branch parameter. Feel free to close the ticket. Although, from our point of view this is still a bug... Thanks, Attila

            Unassigned Unassigned
            atokes Attila
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: