Pull request number

envars
git

#1

Hello!
I’m trying to get the pull request number and detect if I’m on a pull request on a build.

When I look at the docs of the env vars:
https://circleci.com/docs/environment-variables/

I see:
CI__PULL_REQUEST to know I’m on a pull request
CIRCLE_PR_NUMBER to know the PR number I’m into

But none works. Any clue?


#2

Hm, it should be. Do you happen to have a link to a public build or a circle.yml we could take a look at?


#3

Nop, its private and I’m using only the build steps on the interface, no .yml

Here are the tests parameters:

wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip

unzip sonar-runner-dist-2.4.zip

if [ "$CIRCLE_PULL_REQUEST" = true ]; then sonar-runner-2.4/bin/sonar-runner -e -Dsonar.analysis.mode=preview -Dsonar.github.pullRequest=$CIRCLE_PR_NUMBER -Dsonar.github.repository=$REPO_SLUG -Dsonar.github.oauth=$GITHUB_TOKEN -Dsonar.login=$SONAR_LOGIN -Dsonar.password=$SONAR_PASS -Dsonar.host.url=$SONAR_HOST_URL; fi

I tried also with $CI_PULL_REQUEST

If condition is never filled


#4

Something I ran into that may be your issue, env varibles arn’t bool, they are stored as string.

See if comparing to ‘true’ helps.


#5

Thanks for your answer, but I’m unlucky nothing worked out, tried:

if [ "$CI_PULL_REQUEST" = "true" ];
if [ "$CIRCLE_PULL_REQUEST" = "true" ];
if [ "$CI_PULL_REQUEST" = true];
if [ "$CIRCLE_PULL_REQUEST" = true];

#6

Sorry i didnt catch this before, but im pretty sure you should not be putting the varible in quotes.

Ref Is it possible to run deploy section with out the build


#7

Strange thing is that:

if [ "$CIRCLE_BRANCH" = "develop" ];

Works fine


#8

Its possible that its not working and a bug needs to be filed.

I would test with echo $CI_PULL_REQUEST to see if/what it is getting set to.


#9

Thanks for your help, tried with:

if [ $CI_PULL_REQUEST = "true" ];
if [ $CIRCLE_PULL_REQUEST = "true" ];

None works :confused:


#10

What does the echo command get you?


#11

Thanks, more info here:

echo $CI_PULL_REQUEST;
https://github.com/dial-once/service-devices/pull/20

So I have to parse it and extract the number to get the PR number, because it will never be true. Should have tested more, thanks


#12

So final code is (because $CIRCLE_PR_NUMBER don’t work outside of forks it seems):

Check if it is a pull request:

if [ -z "$CIRCLE_PULL_REQUEST" ];

Get PR number:

echo ${CIRCLE_PULL_REQUEST##*/}

#13

Glad that you solved this. We actually suggest using

if [[ ! -z $CIRCLE_PULL_REQUEST ]] ; then <something> ; fi

but that should have the same effect.


#14

these are only available for forked builds :frowning: I’ve opened a separate discussion to get a few ENV’s accessible to non-forked repo builds.


#15