blue-sky
blue-sky

Reputation: 53826

Jenkins environment variables - detecting a pull request

I'm attempting to detect if a pull request is created on a branch.

From reading https://ci.eclipse.org/webtools/env-vars.html/

CHANGE_TARGET is :

For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset.

def isPullRequest = env.CHANGE_TARGET == 'master'  

isPullRequest should resolve to true when the pull request is created and merged with master or isPullRequest is true when the pull request is created ?

Upvotes: 2

Views: 2245

Answers (2)

pataluc
pataluc

Reputation: 589

I am very late after MorganGeek's answer, but I had the same question and it seems (I don't know if it's available in every Jenkins version) that we can test for the existence of the variable CHANGE_ID.

Which could result in something very similar, but maybe a little more elegant:

stages {
    stage('Build') {
        steps {
            script {
                if (CHANGE_ID) {
                    ...
                }
            }
        }
    }
}

(not sure though if it needs env.CHANGE_ID instead of CHANGE_ID

Upvotes: 0

PixelPint
PixelPint

Reputation: 191

In our case, for a multibranch pipeline project triggered by a commit in Bitbucket, we simply test the branch name :

stages {
    stage('Build') {
        steps {
            script {
                if (BRANCH_NAME ==~ /PR-.*/)) {
                    ...
                }
            }
        }
    }
}

Upvotes: 4

Related Questions