代码之家  ›  专栏  ›  技术社区  ›  Viswanath Kumar Sandu

GitHub的Jenkins build for PR失败

  •  3
  • Viswanath Kumar Sandu  · 技术社区  · 6 年前

    job screenshot

    一直失败,错误如下

    Error screenshot

    如果你对此有什么建议请告诉我

    即使是答案 Git PullRequest job failed. Couldn't find any revision to build. Verify the repository and branch configuration for this job 没有解决我的问题

    1 回复  |  直到 6 年前
        1
  •  5
  •   Joerg S    6 年前

    2019/03/22更新:

    快速回答

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

    完整答案

    有一张关于这件事的公开票: https://issues.jenkins-ci.org/browse/JENKINS-52668?filter=18657

    编辑:

    我可以使用Jenkins多分支管道、github插件和对 checkout

    对于Bitbucket,我找到了几个构建PRs的选项。我现在甚至找到了一种跳过条件的方法。见下文。

    建议

    我建议尽可能使用 checkout scm 这很容易为PRs工作。

    如果你需要使用 结帐 手动执行步骤您可以尝试调整Jenkinsfile,以便手动执行操作,就像我签出bitbucket repo一样。

    最简单的方法应该是 签出配置管理


    github

    最后,我使用以下代码从github获得了我的小示例项目buildin PRs。为了我的快速测试,我在某个部门做了公关。如果你使用叉子作为公关的来源,可能需要进一步的调整。

    如所述 https://stackoverflow.com/a/36359706/4279361 您可以省略“要生成的分支”选项,以避免出现此错误。但是,根据您的公关合并策略,您仍然需要相应地配置合并:

    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中完成合并提交,在这种情况下,您不需要在Jenkins中进行合并,而是需要切换到PR分支。 可以使用条件参照规范,也可以按条件选择分支。这使得下面显示了四个选项a。到目前为止,我还没有找到一个不包含条件的选项。:(

    推荐的解决方案

    以下的解决方案对我来说似乎是最有用的。它们不涉及任何条件,并利用 BRANCH_NAME 变量。然而,我突然想到,有时我得到一个关于无效refspec的错误。在这些情况下,请使用下面所述的替代解决方案之一。

    使用Bitbucket合并

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

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

    或者你决定让詹金斯合并。您可以使用条件refspec或条件合并到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 为库配置源代码管理时的选项。但这对我来说并不管用:(

    enter image description here

    要从某个拉取请求加载共享库,必须执行两项操作:

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

      +refs/pull请求/*/merge:refs/remotes/@{remote}/PR-*
      
    2. 而不是仅仅使用 env.BRANCH_NAME 你必须使用 "origin/${env.BRANCH_NAME}" 加载该库时,例如:

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

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

    条件引用规范

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