Environment variable CIRCLE_PR_NUMBER does not work

I read the docs at https://circleci.com/docs/2.0/env-vars/#setting-an-environment-variable-in-a-shell-command and I wanted to use the $CIRCLE_PR_NUMBER environment variable but it doesn’t seem to work.

	machine: true
	working_directory: ~/repo
	  - run:
		  name: 'Setup env variables'
		  command: |
			echo 'export MY_PR_NUMBER="$CIRCLE_PR_NUMBER"' >> $BASH_ENV
			echo 'export MY_PR_NUMBER2=${CIRCLE_PULL_REQUEST##*/}' >> $BASH_ENV
			echo 'export MY_PR="$CIRCLE_PULL_REQUEST"' >> $BASH_ENV
	  - run:
		  name: 'What PR am I on?'
		  command: echo $MY_PR
	  - run:
		  name: 'What is my PR number?'
		  command: echo $MY_PR_NUMBER
	  - run:
		  name: 'What is my PR number2?'
		  command: echo $MY_PR_NUMBER2

The one for MY_PR_NUMBER does not output anything. I used a workaround from another thread 2 years ago to get the PR number.

Is this a bug?

Try echoing $CIRCLE_PR_NUMBER in your “What is my PR number” step. Maybe there is a problem with the var export.

Is this build a part of a forked PR?

The number of the associated GitHub or Bitbucket pull request. Only available on forked PRs.

I’d have to say no, it’s just a regular PR.

OK, let me re-think this, if I fork a repo and create a PR on that fork, is that when this env var will be present?

Yes, at least per the docs :slight_smile:

Could we take a step back though, what are you trying to do with this number? There might be a different way to approach this that works for any scenario.

Yeah so the use case I was doing was that for every Pull Request I have a netlify preview deployment which includes the PR number in the URL. In my CircleCi tests I am running a performance tests against this netlify deployed preview URL, so that is why I needed to get just the PR number so I could construct the preview URL.

Hi George,

So, to clarify, are you all set, or are you still running into issues?

No issues, I marked this thread as solved.

Awesome, was just checking :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.