使用Jenkins管道恢复默认的github分支

时间:2018-01-27 13:21:21

标签: jenkins jenkins-pipeline

我有一个网站,它依赖于应该包含在项目根目录中的库的回购。两个repos都存储在GitHub中。我正在尝试为此项目设置Jenkins多分支管道。

我试图检查库主仓库上的同一分支作为主仓库,如果匹配的分支不存在,则恢复为主人员。

我在Jenkins文档中发现了以下内容(https://jenkins.io/doc/pipeline/steps/workflow-multibranch/#code-resolvescm-code-resolves-an-scm-from-an-scm-source-and-a-list-of-candidate-target-branch-names),这看起来很完美:

// checkout the main source
dir('main'){
    // this will checkout the source repository that is driving the multi-branch pipeline
    checkout scm
}
// now checkout the tests
dir('tests'){
    // this will check if there is a branch with the same name as the current branch in
    // https://example.com/example.git and use that for the checkout, but if there is no
    // branch with the same name it will fall back to the master branch
    checkout resolveScm(source: git('https://example.com/example.git'), targets: [BRANCH_NAME,'master']
}
// rest of pipeline

然而,这只会引发Sytnax错误(我在上面的示例中也注意到了一个未闭合的括号,因此尝试使用末端的右括号,没有任何乐趣)。

这是我一直在努力的Jenkins文件:

pipeline {
    agent any

stages {
    stage ('Checkout') {
        steps {
            dir ('Libraries') {
                resolveScm source: github(credentialsId: '****', id: '_', repoOwner: '***', repository: 'Libraries', traits: [[$class: 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait', strategyId: 1]]), targets: ['does_not_exist', 'master']
            }
        }
    }
}

如果找不到上面的分支名称,我可以看到开发是,但是没有检出。相反,最后找到的分支仍然是工作区(即在之前的运行中找到的分支)。

有没有人设法成功运作?

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加'结帐'在resolveScm之前? F.e:

    dir('Libraries') {
        checkout resolveScm(source: github(credentialsId: '****',
                id: '_',
                repoOwner: '***',
                repository: 'Libraries',
                traits: [[$class: 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait', strategyId: 1], [$class: 'WipeWorkspaceTrait']]),
                targets: ['does_not_exist', 'master'])
    }