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

Cannot get source branch in PR build in Bitbucket team project

      I need to get JIRA issue key from the name of the branch, but from the PR build I cannot obtain in. Source branch name is not listed in the 'env' list. It would be useful if we could somehow obtain the source branch name from the PR build

          [JENKINS-41104] Cannot get source branch in PR build in Bitbucket team project

          Mike Kobit added a comment -

          It looks like the env.BRANCH_NAME is not the origin branch in pull requests, it is the PR number.

          Mike Kobit added a comment - It looks like the env.BRANCH_NAME is not the origin branch in pull requests, it is the PR number.

          Michael Neale added a comment -

          Hi mkobit what version of the branch-api-plugin do you have installed?

          Michael Neale added a comment - Hi mkobit what version of the branch-api-plugin do you have installed?

          Mike Kobit added a comment -

          michaelneale this happened with the older (as of whatever the latest version was) yesterday. I tested this today with a simple sh 'env' step after updating all of our plugins to the 2.0 line (based on this blog post) and now we see something like:

          # ... some other env variables
          CHANGE_URL=https://git.corp.com/projects/proj/repos/repository/pull-requests/4/overview
          CHANGE_ID=4
          BRANCH_NAME=PR-4
          CHANGE_TITLE=This is the commit message in the PR
          CHANGE_TARGET=master
          

          I don't believe that CHANGE_TARGET was available before the plugin updates but I could be wrong. I don't see anything regarding the source branch in the PR available as an environment variable (may be separate from this issue).

          Note that this is with Bitbucket Server 4.11 (not Bitbucket Cloud).

          Mike Kobit added a comment - michaelneale this happened with the older (as of whatever the latest version was) yesterday. I tested this today with a simple sh 'env' step after updating all of our plugins to the 2.0 line (based on this blog post ) and now we see something like: # ... some other env variables CHANGE_URL=https://git.corp.com/projects/proj/repos/repository/pull-requests/4/overview CHANGE_ID=4 BRANCH_NAME=PR-4 CHANGE_TITLE=This is the commit message in the PR CHANGE_TARGET=master I don't believe that CHANGE_TARGET was available before the plugin updates but I could be wrong. I don't see anything regarding the source branch in the PR available as an environment variable (may be separate from this issue). Note that this is with Bitbucket Server 4.11 (not Bitbucket Cloud).

          Mike Kobit added a comment -

          michaelneale is this more of a Branch API issue or is it specific to the Bitbucket Branch Source plugin?

          Mike Kobit added a comment - michaelneale is this more of a Branch API issue or is it specific to the Bitbucket Branch Source plugin?

          Ryan Campbell added a comment -

          This seems like a feature request to me, marking as such.

          Ryan Campbell added a comment - This seems like a feature request to me, marking as such.

          Gregory Kman added a comment - - edited

          I don't know that I would consider this a feature request. This seems more like a bug to me. I would imagine that the 'BRANCH_NAME=PR-4' should actually be what the source branch is in Git. 'BRANCH_NAME' is equivalent to 'SOURCE_NAME' and 'CHANGE_TARGET' to be equivalent to `TARGET_NAME`

          Am I mistaken? And if so could someone explain what 'BRANCH_NAME' and 'CHANGE_TARGET' are supposed to mean?

          Gregory Kman added a comment - - edited I don't know that I would consider this a feature request. This seems more like a bug to me. I would imagine that the 'BRANCH_NAME=PR-4' should actually be what the source branch is in Git. 'BRANCH_NAME' is equivalent to 'SOURCE_NAME' and 'CHANGE_TARGET' to be equivalent to `TARGET_NAME` Am I mistaken? And if so could someone explain what 'BRANCH_NAME' and 'CHANGE_TARGET' are supposed to mean?

          Jesse Glick added a comment -

          BRANCH_NAME is the head name as far as Jenkins core infrastructure is concerned; modulo escaping, it will be the same as JOB_BASE_NAME.

          CHANGE_TARGET is the expected merge target, such as master typically.

          Jesse Glick added a comment - BRANCH_NAME is the head name as far as Jenkins core infrastructure is concerned; modulo escaping, it will be the same as JOB_BASE_NAME . CHANGE_TARGET is the expected merge target, such as master typically.

          Stephen Connolly added a comment - https://github.com/jenkinsci/branch-api-plugin/blob/e4399aa99bc8dac1fae47915f995c2f93e7e955e/src/main/java/jenkins/branch/BranchNameContributor.java#L69  should be the fix.

          I claim resolved in branch-api 2.0.11

          Stephen Connolly added a comment - I claim resolved in branch-api 2.0.11

          Marcin L added a comment - - edited

          I updated to the latest 2.0.11 but still I can't get the source branch name. It's always set to "PR-" but I think it should be set to `head.ref` from Github api?

          Marcin L added a comment - - edited I updated to the latest 2.0.11 but still I can't get the source branch name. It's always set to "PR-" but I think it should be set to `head.ref` from Github api?

          Jesse Glick added a comment -

          marcinl $CHANGE_BRANCH

          Jesse Glick added a comment - marcinl $CHANGE_BRANCH

          Marcin L added a comment -

          I checked and $CHANGE_BRANCH environment variable is not set. This is my current setup:

          Are you sure it's working with GitHub PRs? I saw some comments online that it's only working with BitBucket.

          Marcin L added a comment - I checked and $CHANGE_BRANCH environment variable is not set. This is my current setup: Are you sure it's working with GitHub PRs? I saw some comments online that it's only working with BitBucket.

          Joe Pea added a comment -

          Hello, how can we use the original branch for pull request builds, instead of the PR-XXX branches?

          Joe Pea added a comment - Hello, how can we use the original branch for pull request builds, instead of the PR-XXX branches?

          Joe Pea added a comment -

          Figured it out. I set the `Discover branches Strategy` to `only branches that are filed as PRs`, and removed the `discover pull requests` options. Now it builds straight from my pull request branches, exactly like I wanted.

          Joe Pea added a comment - Figured it out. I set the `Discover branches Strategy` to `only branches that are filed as PRs`, and removed the `discover pull requests` options. Now it builds straight from my pull request branches, exactly like I wanted.

            Unassigned Unassigned
            pkozikow Paweł Kozikowski
            Votes:
            4 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: