Search code examples
jenkinsjenkins-pipeline

How to check if a pipeline is triggered from a pull request


I'm using Jenkins pipeline to build Pull Requests branches using the GitHub Organization plugin.

I can build them fine, but I want to avoid some steps (such as publishing the artifacts). Checking git's current branch gives me the PR's target since the PR branch is being merged into the target before the build is attempted.

How can I check if the build is being initiated from a PR vs than a regular branch build?


Solution

  • At least on Jenkins 2.16 env.BRANCH_NAME gives the source branch not the target branch. You could do something like:

    if (env.BRANCH_NAME == "master") {
      sh "./publish.sh"
    }
    

    Other env vars that could be useful are the CHANGE_* variables. E.g.,

    CHANGE_AUTHOR='me'
    CHANGE_ID='6'
    CHANGE_TARGET='master'
    CHANGE_TITLE='Update README.md'
    CHANGE_URL='https://github.com/test-org/test-repo/pull/6'
    

    For documentation on these and more: https://ci.eclipse.org/webtools/env-vars.html/