Jenkins无法从GitHub建立PR [英] Jenkins build fail for PR from GitHub

查看:351
本文介绍了Jenkins无法从GitHub建立PR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jenkins管道来构建我的github项目.当我在GitHub上提出拉取请求(PR)时,它会创建一个工作"pr-head"

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"

始终失败并显示以下错误

which fails all the time with below error

如果您对此有任何建议,请告诉我

Let me know if you have any suggestions for this

甚至 Git PullRequest作业失败.找不到要构建的任何修订.验证此工作的存储库和分支配置不能解决我的问题

推荐答案

更新2019/03/22:

快速分析助手

使用适合您git服务器PR处理的refspec.例如.对于Bitbucket可能是:

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-*

完整答案

有关于此的公开票: https://issues.jenkins-ci.org/browse/JENKINS- 52668?filter = 18657

Full Answer

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

我能够使用Jenkins多分支管道以及github插件和对checkout步骤的手动调用来重现此问题.

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.

对于Bitbucket,我发现了几种构建PR的选项.我现在甚至找到了一种跳过此处条件的方法.见下文.

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

只要有可能,我建议您使用checkout scm,它很容易用于PR.

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

如果您需要手动使用checkout步骤,则可以尝试调整Jenkinsfile,以便像我检查位桶存储库那样手动执行操作.

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.

最简单的方法应该是至少执行一次checkout scm来查看应该如何做,并在手动结帐步骤中相应地使用该值.对于未建立PR的情况,您将需要一些前提条件.

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上获得了小样本项目PR.为了进行快速测试,我从某个分支机构进行了PR.如果您使用叉子作为PR的来源,则可能需要进一步调整.

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.

https://stackoverflow.com/a/36359706/4279361 中所述,您可以将生成"选项以不出现此错误.但是,根据您的PR合并策略,您仍然需要相应地配置合并:

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

对于bitbucket,您必须执行几乎相同的操作.但是,您可以选择直接在bitbucket中完成合并提交,在这种情况下,您无需在Jenkins中进行合并,而需要切换到PR分支. 您可以使用条件refspec或有条件地选择分支.这使得四个选项如下所示.到目前为止,我还没有找到不涉及条件的选项. :(

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. :(

按照我的解决方案似乎最有用.它们不涉及任何条件,并使用BRANCH_NAME变量.但是我想到有时我会收到关于无效refspec的错误.在这种情况下,请使用以下替代方法之一.

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.

使用由Bitbucket服务器准备的合并

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>'
    ]]
])

使用Jenkins合并

或者您决定让Jenkins进行合并.您可以使用条件refspec或有条件地合并到PR中.

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>'
    ]]
])

共享库

要将PR分支与全局共享库一起使用,最直接的方法似乎是在为库配置源控件时使用Discover other refs选项.但是,这对我不起作用:(

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 :(

要从某些请求中加载共享库,您必须做两件事:

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

  1. 将以下refspec添加到共享库的Jenkins全局配置中:

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

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

  • 加载该库时,不仅要使用env.BRANCH_NAME,还必须使用"origin/${env.BRANCH_NAME}",例如:

  • 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)
    

  • 使用Bitbucket合并的替代解决方案

    有条件的参考规格

    只是一个后备;我可以记住,包括PR在内的refspec有时对我不起作用.在这种情况下,您可以使用:

    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>'
        ]]
    ])
    

    条件分支名称

    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>'
        ]]
    ])
    

    在Jenkins中使用Merge的替代解决方案

    有条件的参考规范

    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>'
        ]]
    ])
    

    有条件的合并

    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>'
        ]]
    ])
    

    这篇关于Jenkins无法从GitHub建立PR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆