首页 > 解决方案 > Jenkins 文件差异与声明性管道和多分支

问题描述

我有一个场景,我在同一个 repo 中管理两个管道,有两个不同的 Jenkinsfiles。

我已经设置了两个 Jenkins 多分支管道来处理两个不同的 Jenkinsfiles,通过路径发现并设置 github webhook 以在特定分支上创建 PR 时触发构建。

我还没有找到一种方法来更改特定 PR 的文件,所以我想通过这样做来利用 git,git diff --name-status origin/master...HEAD但它失败了,因为 Jenkins 只检查目标分支。

日志存储:

using credential github-user-token-uname-pwd
Fetching changes from the remote Git repository
Fetching without tags
 > git rev-parse --is-inside-work-tree # timeout=10
 > git config remote.origin.url https://github.com/myreponame # timeout=10
Fetching upstream changes from https://github.com/myreponame
 > git --version # timeout=10
using GIT_ASKPASS to set credentials Github token in uname-pwd form used by jenkins to register and manage webhooks
 > git fetch --no-tags --force --progress https://github.com/myreponame +refs/heads/BRANCH-X:refs/remotes/origin/BRANCH-X
Checking out Revision 440df9b08667458fa4ade4be57ecbf59a4 (BRANCH-X)
Commit message: "move post build where it belongs"
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 440df9b08667458fa4ade4be57ecbf59a4
 > git rev-list --no-walk ab28e843c0fc7807c4cbd2d6f612e5d76b # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $SECRET_ACCESS_KEY or $ACCESS_KEY_ID
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] sh
+ git diff --name-status origin/master...HEAD

和我得到的错误:

fatal: ambiguous argument 'origin/master...HEAD': unknown revision or path not in the working tree.

有没有办法在多分支管道中检索 PR 的文件更改或让 Jenkins 能够发现所述 PR 的源分支?

标签: jenkinsgithubjenkins-pipeline

解决方案


所以这就是我必须做的。我面临的错误是由于 Jenkins 使用多分支管道的默认签出行为:它签出并跟踪一个分支,并且只跟踪该分支,它不获取其他分支。我最终添加了

    options {
        skipDefaultCheckout true
    }

以及检查所有原始远程分支的阶段

stage('Checking out repo'){
            steps {
                script {
                    checkout(
                        [
                            $class: 'GitSCM', 
                            branches: [[name: 'origin/FEATUREBRANCH*']], 
                            doGenerateSubmoduleConfigurations: false, 
                            extensions: [[$class: 'LocalBranch'], 
                            [$class: 'CleanBeforeCheckout']], 
                            submoduleCfg: [], 
                            userRemoteConfigs: [[credentialsId: 'github-deploy-key', 
                            url: 'git@github.com:myawesomerepo.git']]
                        ]
                    )
                }
            }
        }

推荐阅读