# CI\_PULL\_REQUEST no longer when building a PR

**URL:** <https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191>\
**Category:** Bug Reports\
**Tags:** envars\
**Created:** [May 24, 2016, 7:59pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191 "2016-05-24T19:59:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanduynslagerp](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/vanduynslagerp/32/3064_2.png) [@vanduynslagerp](https://discuss.circleci.com/u/vanduynslagerp)\
**Post date:** [May 24, 2016, 7:59pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/1 "2016-05-24T19:59:11Z")

</div>

When pushing to a branch with a PR the variable CI\_PULL\_REQUEST used to be set with the PR URL.  
Since a few days this variable is systematically empty.

The Pull Request number does appear in the build dashboard (see screenshot).

 ![](https://global.discourse-cdn.com/circleci/original/2X/7/76b3d1b8fbf0c3e290c51dce76ef31883094652f.jpg)

Looking at the Github webhook payload, it seems they doesn’t send the PR URL in the “push” notification. They sent it in the “pull\_request” notification though.  
When pushing to a branch that has an open PR, Github generate a “push” and a “pul\_request” notification.  
It seems CircleCI trigger a job based on the “push” but not on the “pull\_request” notification, possibly explaining the missing CI\_PULL\_REQUEST variable.

Is there a solution to this problem ? How can I obtain the PR URL or Number in my build when I push to a branch with a PR open ?  
I guess it would still be possible as Circle CI is able to detect the PR and display it in the dashboard.

---

<div class="post-metadata">

**Author:** ![rcedwards](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rcedwards/32/3077_2.png) [@rcedwards](https://discuss.circleci.com/u/rcedwards)\
**Post date:** [May 25, 2016, 2:25pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/2 "2016-05-25T14:25:23Z")

</div>

I am also seeing this issue along with a number of other empty environment variables.

Below is the output of a echo variables script with each environment variable.

Circle CI: true  
CI: true  
PROJECT USERNAME: REDACTED  
PROJECT REPONAME: REDACTED  
PROJECT BRNACH: REDACTED  
**TAG:**  
SHA1: b0d0dcf8745ac9b8b8c158ed93a9f050fd4f950a  
REPO URL: REDACTED  
COMPARE URL: REDACTED  
BUILD URL: REDACTED  
BUILD NUM: 99  
PREVIOUS BUILD NUM: 98  
**CI PULL REQUESTS:**  
**CI PULL REQUEST:**  
ARTIFACTS: /tmp/circle-artifacts.g9hIIht  
USERNAME: rcedwards  
TEST REPORTS: /tmp/circle-junit.FZQouwT  
**PR USERNAME:**  
**REPO NAME:**  
**PR NUMBER:**  
NODE TOTAL: 1  
NODE INDEX: 0  
BUILD IMAGE: osx  
HOME: /Users/distiller  
**DISPLAY:**  
**LANG:**  
PATH: /usr/local/bin:/usr/local/lib/ruby/gems/2.0.0/bin:/usr/bin:/bin:/usr/sbin:/sbin  
PWD: /Users/distiller/REDACTED

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/tom/32/27680_2.png) [@tom](https://discuss.circleci.com/u/tom)\
**Post date:** [May 25, 2016, 7:52pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/3 "2016-05-25T19:52:14Z")

</div>

Thanks for reporting this issue and for taking the time to describe in detail what you’re seeing.

We’re aware that it’s affecting a considerable number of customers and are looking into how best we can resolve it. I’m afraid I don’t have ETA at this time but we’ll update here when we know more.

---

<div class="post-metadata">

**Author:** ![mr-fixit](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/mr-fixit/32/2181_2.png) [@mr-fixit](https://discuss.circleci.com/u/mr-fixit)\
**Post date:** [June 1, 2016, 6:39pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/4 "2016-06-01T18:39:35Z")

</div>

Don’t mean to pester you, but it’s been 7 days. Any update on this bug?

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/tom/32/27680_2.png) [@tom](https://discuss.circleci.com/u/tom)\
**Post date:** [June 1, 2016, 7:16pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/5 "2016-06-01T19:16:18Z")

</div>

Thanks for the prod @mr-fixit ! Are the env vars listed by @rcedwards still missing for your?

---

<div class="post-metadata">

**Author:** ![mr-fixit](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/mr-fixit/32/2181_2.png) [@mr-fixit](https://discuss.circleci.com/u/mr-fixit)\
**Post date:** [June 1, 2016, 7:17pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/6 "2016-06-01T19:17:30Z")

</div>

CI\_PULL\_REQUEST is, I haven’t looked further.

---

<div class="post-metadata">

**Author:** ![rcedwards](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rcedwards/32/3077_2.png) [@rcedwards](https://discuss.circleci.com/u/rcedwards)\
**Post date:** [June 1, 2016, 7:29pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/7 "2016-06-01T19:29:37Z")

</div>

Tom, I haven’t re-tested this script to observe all the env variables but I understand now why CI\_PULL\_REQUEST is missing from the script. See my reply here [Feature request: Option to disable builds on commit pushes](https://discuss.circleci.com/t/feature-request-option-to-disable-builds-on-commit-pushes/682/15?u=rcedwards)

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/tom/32/27680_2.png) [@tom](https://discuss.circleci.com/u/tom)\
**Post date:** [June 1, 2016, 7:32pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/8 "2016-06-01T19:32:24Z")

</div>

Thanks for the updates - I’ll investigate tomorrow and get back to you then.

---

<div class="post-metadata">

**Author:** ![vanduynslagerp](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/vanduynslagerp/32/3064_2.png) [@vanduynslagerp](https://discuss.circleci.com/u/vanduynslagerp)\
**Post date:** [June 1, 2016, 10:42pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/9 "2016-06-01T22:42:30Z")

</div>

It started to work again for me a few days ago.

Anyway, you can add this script to you build to workaround the problem:

if [-z "{CI\_PULL\_REQUEST:-}"]; then CI\_PULL\_REQUEST=(curl -X GET -u {GITHUB\_TOKEN}:x-oauth-basic 'https://api.github.com/repos/{CIRCLE\_PROJECT\_USERNAME}/{CIRCLE\_PROJECT\_REPONAME}/pulls?head={CIRCLE\_PROJECT\_USERNAME}:{CIRCLE\_BRANCH}’ | jq “.[0].url”)  
fi

If CI\_PULL\_REQUEST is not defined or is empty, this script will call the Github API to retrieve the URL of the first PR that has a the current branch as head.  
Just make sure to generate a token on Github and to add it as an environment variable on Circle CI. In this example the variable to create is named GITHUB\_TOKEN.  
Hope it help someone.

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/tom/32/27680_2.png) [@tom](https://discuss.circleci.com/u/tom)\
**Post date:** [June 2, 2016, 11:07am UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/10 "2016-06-02T11:07:18Z")

</div>

Thanks for the updates everyone. To summarise:

- the env vars relating to PRs are only populated when the build is part of a PR (if the build is part of a PR and CI\_PULL\_REQUEST is missing, please contact support from within the application)
- there was an issue last week relating to PR webhooks from GitHub which we have now fixed
- our suggested workaround to achieve ‘[Running Specific Tests Only On A Pull Request](https://discuss.circleci.com/t/running-specific-tests-only-on-a-pull-request/500/2)’ has a couple of drawbacks as described by @rcedwards [here](https://discuss.circleci.com/t/feature-request-option-to-disable-builds-on-commit-pushes/682/15)
- the script [above](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/9?u=tom) from @vanduynslagerp will help if you need access to CI\_PULL\_REQUEST for all builds
- Please vote for the feature request for ‘[Option to disable builds on commit pushes](https://discuss.circleci.com/t/feature-request-option-to-disable-builds-on-commit-pushes/682/1)’ if that would be helpful for you to only build on PRs. Although the request was made some months ago, the more votes / likes it gets really will move it up our queue of features to implement.

---

<div class="post-metadata">

**Author:** ![mr-fixit](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/mr-fixit/32/2181_2.png) [@mr-fixit](https://discuss.circleci.com/u/mr-fixit)\
**Post date:** [June 6, 2016, 7:46pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/11 "2016-06-06T19:46:34Z")

</div>

I’ve confirmed the `CI_PULL_REQUEST` is back.

FWIW, I also saw that it isn’t mentioned in the ‘Test Summary: Infrastructure: Start Container’ details, so don’t rely on that section in an existence proof. See [A build’s ‘Infrastructure: Start Container’ section is missing certain environment variables](https://discuss.circleci.com/t/a-builds-infrastructure-start-container-section-is-missing-certain-environment-variables/4403/1)

---

<div class="post-metadata">

**Author:** ![emilong](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/emilong/32/11314_2.png) [@emilong](https://discuss.circleci.com/u/emilong)\
**Post date:** [September 8, 2017, 4:50pm UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/12 "2017-09-08T16:50:34Z")

</div>

Another option if you’re using merge commits is simply to parse the top commit message, eg.

```auto
if [-z "${CI_PULL_REQUEST:-}"]; then
  CI_PR_ID=`git log -n 1 | grep 'Merge pull request' | sed -e 's/.*#\([0-9]\+\).*/\1/'`
  export CI_PULL_REQUEST="https://github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CI_PR_ID}"
fi

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [June 18, 2018, 11:34am UTC](https://discuss.circleci.com/t/ci-pull-request-no-longer-when-building-a-pr/4191/13 "2018-06-18T11:34:10Z")

</div>

Closed on June 18, 2018, 11:34am UTC


