CI_PULL_REQUEST no longer when building a PR

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).

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.

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

1 Like

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.

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

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

CI_PULL_REQUEST is, I haven’t looked further.

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

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

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.

1 Like

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’ has a couple of drawbacks as described by @rcedwards here
  • the script above 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’ 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.

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

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

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