首先,这不是“构建前合并”功能的重复问题。我用谷歌搜索并审查了我发现的有关此功能的任何内容。但没有人谈论我的问题。
我们有Bitbucket Server + Jenkins,我为Bitbucket的PR写了一份工作:
job('pull-request-job') {
scm {
git {
remote {
name 'origin'
credentials 'jenkins-ssh'
url 'ssh://[email protected]/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服务器为每个pull-request触发的工作。在工作中,我想检查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://[email protected]/my/repository.git # timeout=10
Fetching upstream changes from ssh://[email protected]/my/repository.git
> git --version # timeout=10
using GIT_SSH to set credentials Jenkins User SSH Private Key
> git fetch --tags --progress ssh://[email protected]/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中的另一个分支?
谢谢。
我不知道为什么,但不知怎的,它解决了这个问题。将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://[email protected]/my/repository.git
> git init /var/lib/jenkins/workspace/rad-test-1-pr # timeout=10
Fetching upstream changes from ssh://[email protected]/my/repository.git
> git --version # timeout=10
using GIT_SSH to set credentials Jenkins User SSH Private Key
> git fetch --tags --progress ssh://[email protected]/my/repository.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url ssh://[email protected]/my/repository.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url ssh://[email protected]/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://[email protected]/my/repository.git
using GIT_SSH to set credentials Jenkins User SSH Private Key
> git fetch --tags --progress ssh://[email protected]/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"
> 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 Plugin这样做是因为他们以这种方式实现了它。如果您想要澄清,可以给他们留言。