Jenkins build fail for PR from GitHubGit PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job“java.lang.Exception: None or multiple repos” with BitBucket Notifier in Jenkins multi-branch pipelineCan we use a single jenkins file for multibranch piepeline in jenkins using shared libraries?How to build a Pipeline basic with JenkinsHow can I remove a commit on GitHub?Git pull a certain branch from GitHubHow can I determine the URL that a local Git repository was originally cloned from?Download single files from GitHubIs there a way to cache GitHub credentials for pushing commits?How do I update a GitHub forked repository?Preferred Github workflow for updating a pull request after code reviewHow to restart Jenkins manually?Add images to README.md on GitHubHow do I clone a job in Jenkins?

Are Boeing 737-800’s grounded?

What word means to make something obsolete?

Modify locally tikzset

In gnome-terminal only 2 out of 3 zoom keys work

Python "triplet" dictionary?

What's the metal clinking sound at the end of credits in Avengers: Endgame?

Minimum value of 4 digit number divided by sum of its digits

Will tsunami waves travel forever if there was no land?

Pulling the rope with one hand is as heavy as with two hands?

Does jamais mean always or never in this context?

Why do computer-science majors learn calculus?

A non-technological, repeating, visible object in the sky, holding its position in the sky for hours

When India mathematicians did know Euclid's Elements?

Mysql fixing root password

Stateful vs non-stateful app

What is a Recurrent Neural Network?

Can my Warlock be invisible and attack with its familiar?

Why do Ichisongas hate elephants and hippos?

Bayes Nash Equilibria in Battle of Sexes

Is there a way to get a compiler for the original B programming language?

What does "rf" mean in "rfkill"?

Has any spacecraft ever had the ability to directly communicate with civilian air traffic control?

Confusion about capacitors

Help, my Death Star suffers from Kessler syndrome!



Jenkins build fail for PR from GitHub


Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job“java.lang.Exception: None or multiple repos” with BitBucket Notifier in Jenkins multi-branch pipelineCan we use a single jenkins file for multibranch piepeline in jenkins using shared libraries?How to build a Pipeline basic with JenkinsHow can I remove a commit on GitHub?Git pull a certain branch from GitHubHow can I determine the URL that a local Git repository was originally cloned from?Download single files from GitHubIs there a way to cache GitHub credentials for pushing commits?How do I update a GitHub forked repository?Preferred Github workflow for updating a pull request after code reviewHow to restart Jenkins manually?Add images to README.md on GitHubHow do I clone a job in Jenkins?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I'm using jenkins pipeline to build my github project. When I raise a pull request (PR) on GitHub, its creating a job "pr-head"



job screenshot



which fails all the time with below error



Error screenshot



Let me know if you have any suggestions for this



Even the answers in Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job did not solve my problem










share|improve this question
























  • Can you show your Jenkinsfile, please?

    – SilverNak
    Aug 18 '18 at 17:02











  • Please post the error message as text so your question may be found by possible future readers.

    – Joerg S
    Aug 19 '18 at 6:50






  • 1





    Possible duplicate of Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job

    – Martin Zeitler
    Aug 19 '18 at 7:58

















3















I'm using jenkins pipeline to build my github project. When I raise a pull request (PR) on GitHub, its creating a job "pr-head"



job screenshot



which fails all the time with below error



Error screenshot



Let me know if you have any suggestions for this



Even the answers in Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job did not solve my problem










share|improve this question
























  • Can you show your Jenkinsfile, please?

    – SilverNak
    Aug 18 '18 at 17:02











  • Please post the error message as text so your question may be found by possible future readers.

    – Joerg S
    Aug 19 '18 at 6:50






  • 1





    Possible duplicate of Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job

    – Martin Zeitler
    Aug 19 '18 at 7:58













3












3








3


1






I'm using jenkins pipeline to build my github project. When I raise a pull request (PR) on GitHub, its creating a job "pr-head"



job screenshot



which fails all the time with below error



Error screenshot



Let me know if you have any suggestions for this



Even the answers in Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job did not solve my problem










share|improve this question
















I'm using jenkins pipeline to build my github project. When I raise a pull request (PR) on GitHub, its creating a job "pr-head"



job screenshot



which fails all the time with below error



Error screenshot



Let me know if you have any suggestions for this



Even the answers in Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job did not solve my problem







jenkins github jenkins-pipeline jenkins-github-plugin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 2 '18 at 10:50







Viswanath Kumar Sandu

















asked Aug 18 '18 at 6:43









Viswanath Kumar SanduViswanath Kumar Sandu

1,2131718




1,2131718












  • Can you show your Jenkinsfile, please?

    – SilverNak
    Aug 18 '18 at 17:02











  • Please post the error message as text so your question may be found by possible future readers.

    – Joerg S
    Aug 19 '18 at 6:50






  • 1





    Possible duplicate of Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job

    – Martin Zeitler
    Aug 19 '18 at 7:58

















  • Can you show your Jenkinsfile, please?

    – SilverNak
    Aug 18 '18 at 17:02











  • Please post the error message as text so your question may be found by possible future readers.

    – Joerg S
    Aug 19 '18 at 6:50






  • 1





    Possible duplicate of Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job

    – Martin Zeitler
    Aug 19 '18 at 7:58
















Can you show your Jenkinsfile, please?

– SilverNak
Aug 18 '18 at 17:02





Can you show your Jenkinsfile, please?

– SilverNak
Aug 18 '18 at 17:02













Please post the error message as text so your question may be found by possible future readers.

– Joerg S
Aug 19 '18 at 6:50





Please post the error message as text so your question may be found by possible future readers.

– Joerg S
Aug 19 '18 at 6:50




1




1





Possible duplicate of Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job

– Martin Zeitler
Aug 19 '18 at 7:58





Possible duplicate of Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job

– Martin Zeitler
Aug 19 '18 at 7:58












1 Answer
1






active

oldest

votes


















4














Update 2019/03/22:



Quick anser



Use a refspec which fits your git server's PR handling. E.g. for Bitbucket that could be:



+refs/pull-requests/*/merge:refs/remotes/@remote/PR-*


Full Answer



There is an open ticket about this:
https://issues.jenkins-ci.org/browse/JENKINS-52668?filter=18657



EDIT:



I was able to reproduce this issue using a Jenkins multibranch pipeline together with the github plugin and a manual call to the checkout step.



For Bitbucket I found several options how to build PRs. I now even found a way to skip the conditional there. See below.



Recommendation



Whenever possible I'd recommend using checkout scm which works for PRs easily.



If you need to use the checkout step manually you could try to adjust your Jenkinsfile in order to do things manually as I did for checking out a bitbucket repo.



Easiest way should be to do a checkout scm at least one time to see how it should be done and use that values accordingly in the manual checkout steps. You'll need some if condition for the case that you're not building a PR.




Github



I finally got my small sample project buildin PRs from github using the following code. For my quick test I did a PR from some branch. In case you're using a fork as source for the PR it may need some further adjustment.



As stated in https://stackoverflow.com/a/36359706/4279361 you can leave out 'branches to build' option to not get this error. However depending on your PR merge strategy you'd still need to configure the merge accordingly:



def isPr() 
env.CHANGE_ID != null


// github-specific refspec
def refspec = "+refs/pull/$env.CHANGE_ID/head:refs/remotes/origin/PR-$env.CHANGE_ID +refs/heads/master:refs/remotes/origin/master"
def url = 'https://github.com/orgi/workflow-durable-task-step-plugin.git'

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: "PR-$env.CHANGE_ID"]]]


checkout([
$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
credentialsId: '<your credentials>',
url: url
]]
])


Bitbucket



For bitbucket you have to do the almost the same. However you'll have the option to the merge commit done in bitbucket directly in which case you do not need to do the merge in Jenkins but need to switch to the PR branch instead.
You may either work with a conditional refspec or choose the branch conditionally. That makes four options a shown below. So far I didn't find an option which does not involve a conditional. :(



Recommended solutions



Following solutions to me seems most usable. They do not involve any conditionals and make use of the BRANCH_NAME variable. However it occurred to me that sometimes I got an error about an invalid refspec. In those cases please use one of the alternative solutions as written below.



Using Bitbucket Merge



Use the merge which as prepared by the Bitbucket server



def respec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Using Jenkins Merge



Or you decide to let Jenkins do the merge. You may either use a conditional refspec or a conditional merge into the PR.



def refspec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Shared library



To use PR branches alongside a global shared library it would seem like the most straight-forward way would be to use the Discover other refs option when configuring the source control for your library. However that didn't work for me :(



enter image description here



To load a shared library from some pull request you have to do two things:




  1. Add the following refspec to your shared library's Jenkins global configuration:



    +refs/pull-requests/*/merge:refs/remotes/@remote/PR-*



  2. Instead of just using env.BRANCH_NAME you have to use"origin/$env.BRANCH_NAME" when loading that library, like:



    libBranch = env.BRANCH_NAME
    libId= "myLib@origin/$libBranch"
    lib = library(libId)


Alternative Solution using Bitbucket Merge



Conditional Refspec



Just as a fallback; I can remember that the refspec including the PRs sometimes didn't work for me. In that case you could use:



def isPr() 
env.CHANGE_ID != null


def respec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
respec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Conditional Branch Name



def isPr() 
env.CHANGE_ID != null

def branch
if (isPr())
branch = "refs/remotes/origin/pull-requests/$env.CHANGE_ID/merge"
else
branch = "*/master"


checkout([$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])


Alternative Solution using Merge in Jenkins



Conditional refspec



def isPr() 
env.CHANGE_ID != null

def refspec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
refspec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Conditional merge



def isPr() 
env.CHANGE_ID != null

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin/pull-requests", mergeTarget: "$env.CHANGE_ID/from"]]]

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])





share|improve this answer

























  • Thanks dude. It worked :)

    – Viswanath Kumar Sandu
    Sep 3 '18 at 14:24











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51906017%2fjenkins-build-fail-for-pr-from-github%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Update 2019/03/22:



Quick anser



Use a refspec which fits your git server's PR handling. E.g. for Bitbucket that could be:



+refs/pull-requests/*/merge:refs/remotes/@remote/PR-*


Full Answer



There is an open ticket about this:
https://issues.jenkins-ci.org/browse/JENKINS-52668?filter=18657



EDIT:



I was able to reproduce this issue using a Jenkins multibranch pipeline together with the github plugin and a manual call to the checkout step.



For Bitbucket I found several options how to build PRs. I now even found a way to skip the conditional there. See below.



Recommendation



Whenever possible I'd recommend using checkout scm which works for PRs easily.



If you need to use the checkout step manually you could try to adjust your Jenkinsfile in order to do things manually as I did for checking out a bitbucket repo.



Easiest way should be to do a checkout scm at least one time to see how it should be done and use that values accordingly in the manual checkout steps. You'll need some if condition for the case that you're not building a PR.




Github



I finally got my small sample project buildin PRs from github using the following code. For my quick test I did a PR from some branch. In case you're using a fork as source for the PR it may need some further adjustment.



As stated in https://stackoverflow.com/a/36359706/4279361 you can leave out 'branches to build' option to not get this error. However depending on your PR merge strategy you'd still need to configure the merge accordingly:



def isPr() 
env.CHANGE_ID != null


// github-specific refspec
def refspec = "+refs/pull/$env.CHANGE_ID/head:refs/remotes/origin/PR-$env.CHANGE_ID +refs/heads/master:refs/remotes/origin/master"
def url = 'https://github.com/orgi/workflow-durable-task-step-plugin.git'

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: "PR-$env.CHANGE_ID"]]]


checkout([
$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
credentialsId: '<your credentials>',
url: url
]]
])


Bitbucket



For bitbucket you have to do the almost the same. However you'll have the option to the merge commit done in bitbucket directly in which case you do not need to do the merge in Jenkins but need to switch to the PR branch instead.
You may either work with a conditional refspec or choose the branch conditionally. That makes four options a shown below. So far I didn't find an option which does not involve a conditional. :(



Recommended solutions



Following solutions to me seems most usable. They do not involve any conditionals and make use of the BRANCH_NAME variable. However it occurred to me that sometimes I got an error about an invalid refspec. In those cases please use one of the alternative solutions as written below.



Using Bitbucket Merge



Use the merge which as prepared by the Bitbucket server



def respec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Using Jenkins Merge



Or you decide to let Jenkins do the merge. You may either use a conditional refspec or a conditional merge into the PR.



def refspec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Shared library



To use PR branches alongside a global shared library it would seem like the most straight-forward way would be to use the Discover other refs option when configuring the source control for your library. However that didn't work for me :(



enter image description here



To load a shared library from some pull request you have to do two things:




  1. Add the following refspec to your shared library's Jenkins global configuration:



    +refs/pull-requests/*/merge:refs/remotes/@remote/PR-*



  2. Instead of just using env.BRANCH_NAME you have to use"origin/$env.BRANCH_NAME" when loading that library, like:



    libBranch = env.BRANCH_NAME
    libId= "myLib@origin/$libBranch"
    lib = library(libId)


Alternative Solution using Bitbucket Merge



Conditional Refspec



Just as a fallback; I can remember that the refspec including the PRs sometimes didn't work for me. In that case you could use:



def isPr() 
env.CHANGE_ID != null


def respec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
respec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Conditional Branch Name



def isPr() 
env.CHANGE_ID != null

def branch
if (isPr())
branch = "refs/remotes/origin/pull-requests/$env.CHANGE_ID/merge"
else
branch = "*/master"


checkout([$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])


Alternative Solution using Merge in Jenkins



Conditional refspec



def isPr() 
env.CHANGE_ID != null

def refspec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
refspec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Conditional merge



def isPr() 
env.CHANGE_ID != null

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin/pull-requests", mergeTarget: "$env.CHANGE_ID/from"]]]

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])





share|improve this answer

























  • Thanks dude. It worked :)

    – Viswanath Kumar Sandu
    Sep 3 '18 at 14:24















4














Update 2019/03/22:



Quick anser



Use a refspec which fits your git server's PR handling. E.g. for Bitbucket that could be:



+refs/pull-requests/*/merge:refs/remotes/@remote/PR-*


Full Answer



There is an open ticket about this:
https://issues.jenkins-ci.org/browse/JENKINS-52668?filter=18657



EDIT:



I was able to reproduce this issue using a Jenkins multibranch pipeline together with the github plugin and a manual call to the checkout step.



For Bitbucket I found several options how to build PRs. I now even found a way to skip the conditional there. See below.



Recommendation



Whenever possible I'd recommend using checkout scm which works for PRs easily.



If you need to use the checkout step manually you could try to adjust your Jenkinsfile in order to do things manually as I did for checking out a bitbucket repo.



Easiest way should be to do a checkout scm at least one time to see how it should be done and use that values accordingly in the manual checkout steps. You'll need some if condition for the case that you're not building a PR.




Github



I finally got my small sample project buildin PRs from github using the following code. For my quick test I did a PR from some branch. In case you're using a fork as source for the PR it may need some further adjustment.



As stated in https://stackoverflow.com/a/36359706/4279361 you can leave out 'branches to build' option to not get this error. However depending on your PR merge strategy you'd still need to configure the merge accordingly:



def isPr() 
env.CHANGE_ID != null


// github-specific refspec
def refspec = "+refs/pull/$env.CHANGE_ID/head:refs/remotes/origin/PR-$env.CHANGE_ID +refs/heads/master:refs/remotes/origin/master"
def url = 'https://github.com/orgi/workflow-durable-task-step-plugin.git'

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: "PR-$env.CHANGE_ID"]]]


checkout([
$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
credentialsId: '<your credentials>',
url: url
]]
])


Bitbucket



For bitbucket you have to do the almost the same. However you'll have the option to the merge commit done in bitbucket directly in which case you do not need to do the merge in Jenkins but need to switch to the PR branch instead.
You may either work with a conditional refspec or choose the branch conditionally. That makes four options a shown below. So far I didn't find an option which does not involve a conditional. :(



Recommended solutions



Following solutions to me seems most usable. They do not involve any conditionals and make use of the BRANCH_NAME variable. However it occurred to me that sometimes I got an error about an invalid refspec. In those cases please use one of the alternative solutions as written below.



Using Bitbucket Merge



Use the merge which as prepared by the Bitbucket server



def respec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Using Jenkins Merge



Or you decide to let Jenkins do the merge. You may either use a conditional refspec or a conditional merge into the PR.



def refspec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Shared library



To use PR branches alongside a global shared library it would seem like the most straight-forward way would be to use the Discover other refs option when configuring the source control for your library. However that didn't work for me :(



enter image description here



To load a shared library from some pull request you have to do two things:




  1. Add the following refspec to your shared library's Jenkins global configuration:



    +refs/pull-requests/*/merge:refs/remotes/@remote/PR-*



  2. Instead of just using env.BRANCH_NAME you have to use"origin/$env.BRANCH_NAME" when loading that library, like:



    libBranch = env.BRANCH_NAME
    libId= "myLib@origin/$libBranch"
    lib = library(libId)


Alternative Solution using Bitbucket Merge



Conditional Refspec



Just as a fallback; I can remember that the refspec including the PRs sometimes didn't work for me. In that case you could use:



def isPr() 
env.CHANGE_ID != null


def respec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
respec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Conditional Branch Name



def isPr() 
env.CHANGE_ID != null

def branch
if (isPr())
branch = "refs/remotes/origin/pull-requests/$env.CHANGE_ID/merge"
else
branch = "*/master"


checkout([$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])


Alternative Solution using Merge in Jenkins



Conditional refspec



def isPr() 
env.CHANGE_ID != null

def refspec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
refspec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Conditional merge



def isPr() 
env.CHANGE_ID != null

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin/pull-requests", mergeTarget: "$env.CHANGE_ID/from"]]]

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])





share|improve this answer

























  • Thanks dude. It worked :)

    – Viswanath Kumar Sandu
    Sep 3 '18 at 14:24













4












4








4







Update 2019/03/22:



Quick anser



Use a refspec which fits your git server's PR handling. E.g. for Bitbucket that could be:



+refs/pull-requests/*/merge:refs/remotes/@remote/PR-*


Full Answer



There is an open ticket about this:
https://issues.jenkins-ci.org/browse/JENKINS-52668?filter=18657



EDIT:



I was able to reproduce this issue using a Jenkins multibranch pipeline together with the github plugin and a manual call to the checkout step.



For Bitbucket I found several options how to build PRs. I now even found a way to skip the conditional there. See below.



Recommendation



Whenever possible I'd recommend using checkout scm which works for PRs easily.



If you need to use the checkout step manually you could try to adjust your Jenkinsfile in order to do things manually as I did for checking out a bitbucket repo.



Easiest way should be to do a checkout scm at least one time to see how it should be done and use that values accordingly in the manual checkout steps. You'll need some if condition for the case that you're not building a PR.




Github



I finally got my small sample project buildin PRs from github using the following code. For my quick test I did a PR from some branch. In case you're using a fork as source for the PR it may need some further adjustment.



As stated in https://stackoverflow.com/a/36359706/4279361 you can leave out 'branches to build' option to not get this error. However depending on your PR merge strategy you'd still need to configure the merge accordingly:



def isPr() 
env.CHANGE_ID != null


// github-specific refspec
def refspec = "+refs/pull/$env.CHANGE_ID/head:refs/remotes/origin/PR-$env.CHANGE_ID +refs/heads/master:refs/remotes/origin/master"
def url = 'https://github.com/orgi/workflow-durable-task-step-plugin.git'

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: "PR-$env.CHANGE_ID"]]]


checkout([
$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
credentialsId: '<your credentials>',
url: url
]]
])


Bitbucket



For bitbucket you have to do the almost the same. However you'll have the option to the merge commit done in bitbucket directly in which case you do not need to do the merge in Jenkins but need to switch to the PR branch instead.
You may either work with a conditional refspec or choose the branch conditionally. That makes four options a shown below. So far I didn't find an option which does not involve a conditional. :(



Recommended solutions



Following solutions to me seems most usable. They do not involve any conditionals and make use of the BRANCH_NAME variable. However it occurred to me that sometimes I got an error about an invalid refspec. In those cases please use one of the alternative solutions as written below.



Using Bitbucket Merge



Use the merge which as prepared by the Bitbucket server



def respec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Using Jenkins Merge



Or you decide to let Jenkins do the merge. You may either use a conditional refspec or a conditional merge into the PR.



def refspec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Shared library



To use PR branches alongside a global shared library it would seem like the most straight-forward way would be to use the Discover other refs option when configuring the source control for your library. However that didn't work for me :(



enter image description here



To load a shared library from some pull request you have to do two things:




  1. Add the following refspec to your shared library's Jenkins global configuration:



    +refs/pull-requests/*/merge:refs/remotes/@remote/PR-*



  2. Instead of just using env.BRANCH_NAME you have to use"origin/$env.BRANCH_NAME" when loading that library, like:



    libBranch = env.BRANCH_NAME
    libId= "myLib@origin/$libBranch"
    lib = library(libId)


Alternative Solution using Bitbucket Merge



Conditional Refspec



Just as a fallback; I can remember that the refspec including the PRs sometimes didn't work for me. In that case you could use:



def isPr() 
env.CHANGE_ID != null


def respec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
respec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Conditional Branch Name



def isPr() 
env.CHANGE_ID != null

def branch
if (isPr())
branch = "refs/remotes/origin/pull-requests/$env.CHANGE_ID/merge"
else
branch = "*/master"


checkout([$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])


Alternative Solution using Merge in Jenkins



Conditional refspec



def isPr() 
env.CHANGE_ID != null

def refspec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
refspec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Conditional merge



def isPr() 
env.CHANGE_ID != null

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin/pull-requests", mergeTarget: "$env.CHANGE_ID/from"]]]

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])





share|improve this answer















Update 2019/03/22:



Quick anser



Use a refspec which fits your git server's PR handling. E.g. for Bitbucket that could be:



+refs/pull-requests/*/merge:refs/remotes/@remote/PR-*


Full Answer



There is an open ticket about this:
https://issues.jenkins-ci.org/browse/JENKINS-52668?filter=18657



EDIT:



I was able to reproduce this issue using a Jenkins multibranch pipeline together with the github plugin and a manual call to the checkout step.



For Bitbucket I found several options how to build PRs. I now even found a way to skip the conditional there. See below.



Recommendation



Whenever possible I'd recommend using checkout scm which works for PRs easily.



If you need to use the checkout step manually you could try to adjust your Jenkinsfile in order to do things manually as I did for checking out a bitbucket repo.



Easiest way should be to do a checkout scm at least one time to see how it should be done and use that values accordingly in the manual checkout steps. You'll need some if condition for the case that you're not building a PR.




Github



I finally got my small sample project buildin PRs from github using the following code. For my quick test I did a PR from some branch. In case you're using a fork as source for the PR it may need some further adjustment.



As stated in https://stackoverflow.com/a/36359706/4279361 you can leave out 'branches to build' option to not get this error. However depending on your PR merge strategy you'd still need to configure the merge accordingly:



def isPr() 
env.CHANGE_ID != null


// github-specific refspec
def refspec = "+refs/pull/$env.CHANGE_ID/head:refs/remotes/origin/PR-$env.CHANGE_ID +refs/heads/master:refs/remotes/origin/master"
def url = 'https://github.com/orgi/workflow-durable-task-step-plugin.git'

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: "PR-$env.CHANGE_ID"]]]


checkout([
$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
credentialsId: '<your credentials>',
url: url
]]
])


Bitbucket



For bitbucket you have to do the almost the same. However you'll have the option to the merge commit done in bitbucket directly in which case you do not need to do the merge in Jenkins but need to switch to the PR branch instead.
You may either work with a conditional refspec or choose the branch conditionally. That makes four options a shown below. So far I didn't find an option which does not involve a conditional. :(



Recommended solutions



Following solutions to me seems most usable. They do not involve any conditionals and make use of the BRANCH_NAME variable. However it occurred to me that sometimes I got an error about an invalid refspec. In those cases please use one of the alternative solutions as written below.



Using Bitbucket Merge



Use the merge which as prepared by the Bitbucket server



def respec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Using Jenkins Merge



Or you decide to let Jenkins do the merge. You may either use a conditional refspec or a conditional merge into the PR.



def refspec = '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'
checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Shared library



To use PR branches alongside a global shared library it would seem like the most straight-forward way would be to use the Discover other refs option when configuring the source control for your library. However that didn't work for me :(



enter image description here



To load a shared library from some pull request you have to do two things:




  1. Add the following refspec to your shared library's Jenkins global configuration:



    +refs/pull-requests/*/merge:refs/remotes/@remote/PR-*



  2. Instead of just using env.BRANCH_NAME you have to use"origin/$env.BRANCH_NAME" when loading that library, like:



    libBranch = env.BRANCH_NAME
    libId= "myLib@origin/$libBranch"
    lib = library(libId)


Alternative Solution using Bitbucket Merge



Conditional Refspec



Just as a fallback; I can remember that the refspec including the PRs sometimes didn't work for me. In that case you could use:



def isPr() 
env.CHANGE_ID != null


def respec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
respec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH_NAME]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: respec,
url: '<repo URL>'
]]
])


Conditional Branch Name



def isPr() 
env.CHANGE_ID != null

def branch
if (isPr())
branch = "refs/remotes/origin/pull-requests/$env.CHANGE_ID/merge"
else
branch = "*/master"


checkout([$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])


Alternative Solution using Merge in Jenkins



Conditional refspec



def isPr() 
env.CHANGE_ID != null

def refspec = '+refs/heads/*:refs/remotes/origin/*'
if (isPr())
refspec += ' +refs/pull-requests/*/merge:refs/remotes/origin/PR-*'

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin", mergeTarget: env.BRANCH_NAME]]],
submoduleCfg: [],
userRemoteConfigs: [[
refspec: refspec,
url: '<repo URL>'
]]
])


Conditional merge



def isPr() 
env.CHANGE_ID != null

def extensions = []
if (isPr())
extensions = [[$class: 'PreBuildMerge', options: [mergeRemote: "refs/remotes/origin/pull-requests", mergeTarget: "$env.CHANGE_ID/from"]]]

checkout([$class: 'GitSCM',
doGenerateSubmoduleConfigurations: false,
extensions: extensions,
submoduleCfg: [],
userRemoteConfigs: [[
refspec: '+refs/heads/*:refs/remotes/origin/* +refs/pull-requests/*:refs/remotes/origin/pull-requests/*',
url: '<repo URL>'
]]
])






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 17:33

























answered Aug 19 '18 at 7:10









Joerg SJoerg S

1,6001822




1,6001822












  • Thanks dude. It worked :)

    – Viswanath Kumar Sandu
    Sep 3 '18 at 14:24

















  • Thanks dude. It worked :)

    – Viswanath Kumar Sandu
    Sep 3 '18 at 14:24
















Thanks dude. It worked :)

– Viswanath Kumar Sandu
Sep 3 '18 at 14:24





Thanks dude. It worked :)

– Viswanath Kumar Sandu
Sep 3 '18 at 14:24



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51906017%2fjenkins-build-fail-for-pr-from-github%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?