Jenkins使用声明式管道和多分支的文件差异

时间:2019-09-11 16:32:23

标签: jenkins github jenkins-pipeline

我有一个场景,我在同一个回购中使用两个不同的Jenkinsfiles管理两个管道。

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

我还没有找到一种方法来获取特定PR的文件更改,所以我想到通过执行git diff --name-status origin/master...HEAD来利用git,但由于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的源分支?

2 个答案:

答案 0 :(得分:0)

您可以尝试获取原点,然后使用FETCH_HEAD进行比较 {git fetch origin master
git diff —名称状态FETCH_HEAD ... HEAD }

答案 1 :(得分:0)

这就是我要做的。 我遇到的错误是由于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']]
                        ]
                    )
                }
            }
        }