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