Jenkins environment variables for bitbucket Pull request are showing null values, why?

We have a test-application with webhook(bitbucket scm source hook) configured in repository settings.
We want to check if bitbucket Pull Request is open or not from a branch to master branch based on bitbucket environment variable values in Jenkins.
But we get the values of bitbucket environment variables as null in the jenkins console logs, even though the PR is open.

There is a Pull Request section in Jenkins Job that shows PR with it’s ID, there I am able to see the PR-14(head) and PR-14(merge), where 14 is the PR ID. But then why is the value of ${env.BITBUCKET_PULL_REQUEST_ID} coming out to be null?

Can some one please help me with this?

Jenkinsfile build stage code :-
/* Development zone */
				stage('Build') {
						steps{
								echo "Value of env.BITBUCKET_PULL_REQUEST_ID is ${env.BITBUCKET_PULL_REQUEST_ID}"
								echo "Value of env.BITBUCKET_SOURCE_BRANCH is ${env.BITBUCKET_SOURCE_BRANCH}"
								echo "Value of env.BITBUCKET_TARGET_BRANCH is ${env.BITBUCKET_TARGET_BRANCH}"

								echo "Value of env.BRANCH_NAME is ${env.BRANCH_NAME}"
								echo "Value of env.CHANGE_ID is ${env.CHANGE_ID}"
						}
				}

We’re using BitBucket Server in our organization, and I’m also seeing the same thing, and would also appreciate any help figuring this out. Also any suggestions on alternate methods of figuring out if a branch has an open pull request would be greatly appreciated.

I think the variables BITBUCKET_PULL_REQUEST_ID, BITBUCKET_SOURCE_BRANCH, and BITBUCKET_TARGET_BRANCH are not standard environment variables provided by Jenkins or the Bitbucket Branch Source Plugin. :person_shrugging:

Instead, you might want to use the following environment variables:

Please note that these variables are only available when you run a multibranch pipeline build: In Jenkins pipeline some environment variables are having null value - Stack Overflow.

BITBUCKET_PULL_REQUEST_ID etc. environment variables are documented for Bitbucket Push and Pull Request Plugin: Bitbucket Push and Pull Request

I don’t have enough experience with that to recommend whether to use it or not.

1 Like

My bad, thanks for correcting. :+1: