Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 410
ESG Avatar asked Dec 19 '16 19:12

ESG


People also ask

How do you trigger a pipeline on a pull request?

You can set up pull request triggers for both Azure Repos or GitHub repositories. From within your project, Select Pipelines > Releases, and then select your release pipeline. Under the Pull request trigger section, select the toggle button to enable it.

How can we make Jenkins pipeline jobs triggered by pull requests?

Configure your pipelineOn the Datalog Tagging tab, check “GitHub project”, and put your project URL in the field. On the Build Triggers tab, check “GitHub Pull Request Builder”. Then the GitHub API credentials is automatically filled in. Make sure the Admin list is filled in with at least one admin name.

What is pipeline pull request?

Pull request pipeline runs set compliance status checks on a pull request for the specified application repository. Attempts to merge a pull request into the master branch might be blocked because of failed compliance status checks.


1 Answers

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/

like image 102
mpavlov Avatar answered Oct 21 '22 16:10

mpavlov