我已经使用Git webhook为分支和标记设置了一个多分支管道。它在创建新标记时发现。
但是,我想确定当前执行是基于分支还是标记来确定操作,例如发布神器。
E.g。如果构建是用于标记,并且环境是SIT,我将在构建和测试成功时发布到Artifactory。
如果当前执行是针对分支或标记,那么任何想法如何检入multibranch管道?
提前致谢〜
答案 0 :(得分:1)
您可以在Jenkins文件中使用变量GIT_BRANCH。或者,在你的舞台上,这样做:
...
stage("Publish to Artifactory") {
when {
branch("master")
}
...
}
...
关于标签,我建议让Jenkins在成功发布结束时进行标记。因为如果你自己做标记并触发Jenkins,你会认为你的构建会成功。您的标记构建可能会失败,即使之前的构建成功相同,例如当你的Artifactory失败了。然后你将不得不移动不好的标签。
尝试实现发布管道,在发布工件的管道末尾,标记成功的构建(免责声明:这是未经测试的):
success {
script {
if (env.BRANCH_NAME ==~ /release\/.+/) {
def tag = ...
sh "git tag ${tag}"
sh "git push --tags"
}
}
}
答案 1 :(得分:0)
我的Jenkins文件中有一部分用于标记的不同分支:
environment {
GIT_BRANCH_OR_TAG = sh(returnStdout: true, script:
'''\
#!/bin/bash -e
GIT_REMOTE=$(git remote)
if git rev-parse --verify -q refs/remotes/${GIT_REMOTE}/${GIT_BRANCH}^{} | grep -q ${GIT_COMMIT}
then echo branch
exit 0
elif git rev-parse --verify -q refs/tags/${GIT_BRANCH}^{} | grep -q ${GIT_COMMIT}
then echo tag
exit 0
else echo unknown
exit 0
fi
'''.stripIndent()).trim()
}
如果您尝试使用git show-ref
代替git rev-parse
^{}
,您可以了解情况,当签出时,提交哈希值与标记对象不同。
例如:
[17:20:17] user@host:/tmp/dummy-git/
>ѳ> git init
Initialized empty Git repository in /tmp/dummy-git/.git/
[17:20:21] user@host:/tmp/dummy-git/ (master|✔)
>±> touch file
[17:20:31] user@host:/tmp/dummy-git/ (master|…)
>±> git add file
[17:20:53] user@host:/tmp/dummy-git/ (master|●1)
>±> git commit -m 'dummy commit'
[master (root-commit) 0835e70] dummy commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file
[17:21:04] user@host:/tmp/dummy-git/ (master|✔)
>±> git tag -m 'dummy annotation' with-annotation
[17:21:40] user@host:/tmp/dummy-git/ (master|✔)
>±> git tag without-annotation
[17:21:51] user@host:/tmp/dummy-git/ (master|✔)
>±> git show-ref
0835e706a64f8fd7f7cc8eec904eb527925ebf22 refs/heads/master
b75da5a999f358a390b403136904f4a974787da1 refs/tags/with-annotation
0835e706a64f8fd7f7cc8eec904eb527925ebf22 refs/tags/without-annotation
[17:21:59] user@host:/tmp/dummy-git/ (master|✔)
>±> git show with-annotation
tag with-annotation
Tagger: user <user@gmail.com>
Date: Tue Feb 13 17:21:36 2018 +0100
dummy annotation
commit 0835e706a64f8fd7f7cc8eec904eb527925ebf22 (HEAD -> master, tag: without-annotation, tag: with-annotation)
Author: user <user@gmail.com>
Date: Tue Feb 13 17:21:04 2018 +0100
dummy commit
diff --git a/file b/file
new file mode 100644
index 0000000..e69de29
[17:22:21] user@host:/tmp/dummy-git/ (master|✔)
>±> git show without-annotation
commit 0835e706a64f8fd7f7cc8eec904eb527925ebf22 (HEAD -> master, tag: without-annotation, tag: with-annotation)
Author: user <user@gmail.com>
Date: Tue Feb 13 17:21:04 2018 +0100
dummy commit
diff --git a/file b/file
new file mode 100644
index 0000000..e69de29
[17:22:23] user@host:/tmp/dummy-git/ (master|✔)
>±> git rev-parse refs/tags/with-annotation^{}
0835e706a64f8fd7f7cc8eec904eb527925ebf22
[17:22:56] user@host:/tmp/dummy-git/ (master|✔)
>±> git rev-parse refs/tags/with-annotation
b75da5a999f358a390b403136904f4a974787da1