Jenkins Job DSL Git插件-如何在构建之前合并

时间:2019-04-30 09:29:52

标签: jenkins jenkins-job-dsl

首先,这不是“构建前合并”功能的重复问题。我搜索并查看了有关此功能的所有内容。但是没有人谈论我的问题。

我们有Bitbuck Server + Jenkins,我在Bitbucket上为我们的PR撰写了一份工作:

job('pull-request-job') {
    scm {
        git {
            remote {
                name 'origin'
                credentials 'jenkins-ssh'
                url 'ssh://git@stash.example.com/my/repository.git'
                refspec '+refs/pull-requests/*/from:refs/remotes/*'
            }

            branch '**/pull-requests/**'

            extensions {
                mergeOptions {
                    remote 'origin'
                    branch 'master'
                }
            }
        }
    }

    triggers {
        scm ''
    }

    steps {
        shell './mvnw -e clean verify'
    }
}

快速说明:我们不练习gitflow或类似的方法。通常(并非总是如此),它只是开发人员在其中工作的一个master和分支。

目标:我希望有一个由Bitbucket服务器针对每个请求请求触发的作业。在工作中,我想签出my分支(我从事过的那个分支,并将其合并到master分支中)并运行测试。默认配置可以很好地完成这项工作。现在,在运行测试之前,我想获得从master分支(或my分支将被合并到的任何其他分支)到my分支的最新更改。我怎样才能做到这一点? 具有上述职位描述将无法正常工作。构建的一个示例是:

commit notification 1606dd8
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/workspace/pull-request-job
using credential jenkins-ssh
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url ssh://git@stash.example.com/my/repository.git # timeout=10
Fetching upstream changes from ssh://git@stash.example.com/my/repository.git
> git --version # timeout=10
using GIT_SSH to set credentials Jenkins User SSH Private Key
> git fetch --tags --progress ssh://git@stash.example.com/my/repository.git +refs/pull-requests/*/from:refs/remotes/*
> git rev-parse 1606dd8^{commit} # timeout=10
> git branch -a -v --no-abbrev --contains 1606dd8 # timeout=10
Merging Revision 1606dd8 (3, 2, 1) to origin/master, UserMergeOptions{mergeRemote='origin', mergeTarget='master', mergeStrategy='default', fastForwardMode='--ff'}
> git rev-parse origin/master^{commit} # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f origin/master
> git merge --ff 1606dd8 # timeout=10
> git rev-parse HEAD^{commit} # timeout=10
Seen branch in repository 1
Seen branch in repository 2
Seen branch in repository 3
Seen branch in repository origin/master
Seen 4 remote branches
> git show-ref --tags -d # timeout=10
Checking out Revision 1606dd8 (1, 2, 3, origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 1606dd8
Commit message: "Upgrade maven"
> git rev-list --no-walk 1606dd8 # timeout=10
> git rev-list --no-walk 1606dd8 # timeout=10
> git rev-list --no-walk 1606dd8 # timeout=10
[pull-request-job] $ /bin/sh -xe /tmp/jenkins3480907669634770028.sh
+ ./mvnw -e clean verify

正如所记录的,Jenkins将我的分支合并到master之后,正在再次检出origianl提交(git checkout -f 1606dd8)并继续构建。日志显示确实是代码,没有从master分支建立任何更改。

问题:
1.为什么合并后签出原始提交?为什么不继续在合并的代码上进行构建?
2.如何使branch中的mergeOptions参数动态化,使其始终成为pull-request中的 other 分支?

谢谢。

2 个答案:

答案 0 :(得分:0)

我不知道为什么,但是以某种方式可以解决问题。将localBranch添加到scm.git.extensions项:

    extensions {
      cleanBeforeCheckout()

      localBranch() // or localBranch('${PR_DESTINATION}')

      mergeOptions {
        remote('origin')
        branch('${PR_DESTINATION}')
      }
    }

日志为:

commit notification 1606dd8
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/workspace/rad-test-1-pr
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Deferred wipeout is used...
using credential jenkins-ssh
Cloning the remote Git repository
Cloning repository ssh://git@stash.example.com/my/repository.git
> git init /var/lib/jenkins/workspace/rad-test-1-pr # timeout=10
Fetching upstream changes from ssh://git@stash.example.com/my/repository.git
> git --version # timeout=10
using GIT_SSH to set credentials Jenkins User SSH Private Key
> git fetch --tags --progress ssh://git@stash.example.com/my/repository.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url ssh://git@stash.example.com/my/repository.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url ssh://git@stash.example.com/my/repository.git # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
No valid HEAD. Skipping the resetting
> git clean -fdx # timeout=10
Fetching upstream changes from ssh://git@stash.example.com/my/repository.git
using GIT_SSH to set credentials Jenkins User SSH Private Key
> git fetch --tags --progress ssh://git@stash.example.com/my/repository.git +refs/pull-requests/*/from:refs/remotes/*
> git rev-parse 1606dd8^{commit} # timeout=10
> git branch -a -v --no-abbrev --contains 1606dd8 # timeout=10
Merging Revision 1606dd8 (4, 3, 2, 1, origin/first-branch) to origin/master, UserMergeOptions{mergeRemote='origin', mergeTarget='master', mergeStrategy='default', fastForwardMode='--ff'}
> git rev-parse origin/master^{commit} # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f origin/master
> git merge --ff 1606dd8 # timeout=10
> git rev-parse HEAD^{commit} # timeout=10
Seen branch in repository 1
Seen branch in repository 2
Seen branch in repository 3
Seen branch in repository 4
Seen branch in repository origin/first-branch
Seen branch in repository origin/master
Seen 6 remote branches
> git show-ref --tags -d # timeout=10
Checking out Revision 663303d (origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 663303d
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b master 663303d
Commit message: "Merge commit '1606dd8' into HEAD"

答案 1 :(得分:0)

> git config core.sparsecheckout 
// Checked out but no local branch yet
> git checkout -f 663303d

// Only when local branch is defined
> git branch -a -v --no-abbrev 
// Checkout 663303d into local branch master
> git checkout -b master 663303d

Git插件非常冗长,但是它创建了一个新的存储库,该存储库可获取所有分支和标签,但还没有本地分支,只有远程引用。

> git checkout -f 663303d

检出提交,但没有本地分支可用。我们现在处于独立状态,因为没有本地分支机构。

> git checkout -b master 663303d

从先前检出的(分离的头)提交663303d中创建本地分支主控器。

为什么Git插件那样做是因为他们已经那样实现了。如果您想澄清一下,可以给他们留言。

Why did my Git repo enter a detached HEAD state?