I’m having this same issue for non-merged PRs. Just as in CIRCLE_PULL_REQUEST _still_ not being set, the CIRCLE_PULL_REQUEST env var is not set when a PR is first created. The creation of the PR triggers a build, but the env var is not set or blank. Rebuilding causes the workflow to have CIRCLE_PULL_REQUEST set. This is obviously a bug in the build engine.
Why don’t you all build in delay / retry logic such that if an expected env var value is not found, your engine retries obtaining them before firing off the scheduled workflow / build?
a developer pushes code in a new branch. that branch has logic in .circleci/config.yml to inspect the CIRCLE_PULL_REQUEST env var
circleci kicks off a build for this branch
circleci runs the workflow that looks for CIRCLE_PULL_REQUEST and it fails, because the branch is not part of a PR
the developer then creates a PR for the above branch
circleci looks and sees that a build has already been started for this branch, at the same commit. therefore, it chooses to associate those builds with this PR. As a result, it ends up attaching a failed build to the PR.
#5 is the problem. CircleCI should not be associating the build from the push to the branch with the PR, specifically because the variables like CIRCLE_PULL_REQUEST can be used to change workflow behavior. Other systems, like Jenkins, will kick off a build for the PR separate from the push to the branch for this very reason.
Unless I’m wrong about the above (and I don’t think I am), I would say this is a design mistake in CircleCI.
A better design for this would be one or more of the following:
always kick off workflows specific to a PR, even if builds are already running for the same branch
always kick off builds specific to the PR, and respect “Auto-cancel redundant builds” which is not being taken into account here – since subsequent builds aren’t being launched
create an Advanced Setting named “Run Separate Pull Request Build” to enable #1, while respecting #2
I’m pretty sure there’s also a bug somewhere in the platform.
While most of my projects work as expected, I still have some projects where CIRCLE_PULL_REQUEST et al aren’t being set, under the following workflow:
“Only build on PR” set to “OFF” (I’ve actually tried both settings, with no difference)
Create a branch, push some commits to it
Create a pull request from the branch against master
Push more commits to the branch
Expectation:
CIRCLE_PULL_REQUEST is NOT set during no.2
CIRCLE_PULL_REQUEST IS set during no.4
Observed:
CIRCLE_PULL_REQUEST is NOT set during no.2 or no.4
I’ve opened a support issue already, but maybe someone on the forum can shed light sooner?
Nope, nothing productive from Circle support on this yet.
Apparently their engineers are “aware” of the issue, but they provided no timeline and no visibility.
Seems to be spreading to other projects now too, which is really cramping my game.
OK, I figured out a very crude workaround for the circle-bitbucket project I’m working on.
If the CIRCLE_PULL_REQUEST variable is not set, but you think it should be, you can query the VCS API directly to fetch it. This script is for bitbucket, but similar will work for github. BITBUCKET_USER and BITBUCKET_PASS are variables specific to my environment - replace with whatever auth is appropriate for you.
if [ $CIRCLE_BRANCH <> "master" ]; then
echo "Double checking for PR on $CIRCLE_BRANCH to work around Circle platform bug"
PR_NUMBER=`curl -X GET -u $BITBUCKET_USER:$BITBUCKET_PASS \
https://api.bitbucket.org/2.0/repositories/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pullrequests/?q=source.branch.name%3D%22$CIRCLE_BRANCH%22 \
| grep -o 'https:\/\/api.bitbucket.org\/2.0\/repositories\/messageagency\/jcb\/pullrequests\/[0-9]*' \
| grep -o '[0-9]*$' | head -1`
if [[ $PR_NUMBER =~ ^-?[0-9]+$ ]] ; then
echo "Found PR ${PR_NUMBER}"
# do whatever you need with the PR number here
# ...
fi
fi
I have exactly the same issue. I create a pull request and CircleCI does not set the env var hence my testing script does not run as it is supposed to stop when it’s not a pull request. Clicking on the Run again button in the GUI however sets the pull request environment variable and executes all my tests.
Hi @ir-fuel. Thanks for bringing this issue back to life. I’m asking around now to see what can be done about this, and who might have the ability to prioritize it.
If it can help, I was about to open a ticket about the same issue.
And in my case, it is a bit a deal-breaker, because the PR value is used to properly configure the extend of the build. Without PR id, it triggers a full build which takes quite some times and involves large machines. Thus, such a build costs much more than expected, making it more important than a mere annoyance.
Years later and this CircleCI platform bug still exists. I’m in several orgs within Circle CI. In most orgs this bug is intermittent. In one of those orgs this bug happens on every single PR.
Here’s an equivalent workaround for GitHub:
if [ -z "$CI_PULL_REQUEST" ]; then
echo "Double checking for PR on $CIRCLE_BRANCH to work around Circle platform bug."
# https://discuss.circleci.com/t/circle-pull-requests-environment-variable-not-being-set/29883/10
CI_PULL_REQUEST=`curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls?head=$CIRCLE_BRANCH&state=open" | grep -o '/pulls/[0-9]*' | head -1 | grep -o '[0-9]*'`
if [ -z "$CI_PULL_REQUEST" ] ; then
echo "No PR found. If this is not a mainline or long-running branch, then you'll likely running into the 'We do not build CI environments' error later."
else
echo "Found PR $CI_PULL_REQUEST"
fi
fi
Here’s an updated version that fixes a bug where sometimes it would choose the wrong PR number:
if [ -z "$CI_PULL_REQUEST" ]; then
echo "Double checking for PR on $CIRCLE_BRANCH to work around Circle platform bug."
# https://discuss.circleci.com/t/circle-pull-requests-environment-variable-not-being-set/29883/16
CI_PULL_REQUEST=`
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls?head=$CIRCLE_PROJECT_USERNAME:$CIRCLE_BRANCH&state=open" \
| grep -o '/pulls/[0-9]*' \
| head -1 \
| grep -o '[0-9]*' \
|| true
`
echo "contacted GitHub"
if [ -z "$CI_PULL_REQUEST" ]; then
echo "No PR found. If this is not a mainline or long-running branch, then you'll likely running into the 'We do not build CI environments' error later."
else
echo "Found PR $CI_PULL_REQUEST"
fi
fi