git - 在 Jenkinsfile 中隔离存储库分支的名称
问题描述
Jenkinsfile
为了将提交的分支名称隔离到可以在日志中打印出来的变量中,需要对以下内容进行哪些具体更改? 在下面的例子中,分支的名称是GWS-43-getissueforcommit
。
以下是具体情况:
詹金斯文件:
以下代码Jenkinsfile
在运行命令产生的输出中间打印出分支名称checkout(scm).GIT_ASKPASS
,但是当后续代码行 ( sh "echo 'The repo Ask Pass is: ${repoAskPass}'"
) 尝试打印出命令的封装结果时,此信息会丢失:
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
def repoAskPass = checkout(scm).GIT_ASKPASS
sh "echo 'The repo Ask Pass is: ${repoAskPass}'"
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
结果日志:
结果日志输出为:
General SCM<1s
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
> git init /var/jenkins_home/workspace/ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA # timeout=10
Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials Bitbucket server credentials
> git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
> git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit # timeout=10
> git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
No valid HEAD. Skipping the resetting
> git clean -fdx # timeout=10
Fetching without tags
Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git
using GIT_ASKPASS to set credentials Bitbucket server credentials
> git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/GWS-43-getissueforcommit:refs/remotes/origin/GWS-43-getissueforcommit
Checking out Revision 375b17c4e7453d802d94659836db436553cc7f0c (GWS-43-getissueforcommit)
> git config core.sparsecheckout # timeout=10
> git checkout -f 375b17c4e7453d802d94659836db436553cc7f0c
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b GWS-43-getissueforcommit 375b17c4e7453d802d94659836db436553cc7f0c
Commit message: "isolate ASKPASS"
> git rev-list --no-walk 268c468a96de0fb27b6f205658a169b38871b581 # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
[Bitbucket] Notifying commit build result
echo 'The repo Ask Pass is: null'— Shell Script<1s
[ne_GWS-43-getissueforcommit-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
+ echo The repo Ask Pass is: null
The repo Ask Pass is: null
重述问题:
上面的 Jenkinsfile 需要如何修改才能在日志中输出以下行:
The name of the branch containing the commit for this build is:
GWS-43-getissueforcommit
解决方案
GIT_ASKPASS
用于获取用户凭据 - 它与分支无关。
这是插件集的变量列表,您可以使用它们。从这里您可以看到它GIT_BRANCH
为您提供了远程分支并GIT_LOCAL_BRANCH
为您提供了已签出的本地分支。
def branch = checkout(scm).GIT_BRANCH
sh "echo 'The name of the branch containing the commit for this build is: ${branch}'"
这将打印远程分支名称以及原始前缀(通常是可取的)。但是如果你想要它没有前缀:
def branch = checkout(scm).GIT_BRANCH
branch = branch.substring(branch.indexOf('/') + 1)
sh "echo 'The name of the branch containing the commit for this build is: ${branch}'"
推荐阅读
- python - 这种多层感知器反向传播实现有什么问题?
- android - 如何在kotlin android中使用带有背景按钮的If?
- ios - 更改部署目标后出现错误
- r - 如何从 R 中的非平稳广义帕累托(GP)模型中提取参数?
- azure-web-app-service - 我的 Azure Web 应用程序报告了与内存相关的自动修复。Proactive Auto Heal 如何知道何时重新启动我的 Web 应用程序?
- python - ckeditor_uploader 动态图片上传路径
- python - TypeError 与功能,但工作正常,否则
- haskell - 为什么这个 Functor 的实例化不起作用?
- python - 用jinja2或python中的正则表达式替换字符串中的相同字符序列
- r - ggplot2:数字 x 轴上的自定义分类标签