github - 使用管道作为代码时,在 Jenkins 声明性管道中从 Git 中提取分支名称
问题描述
我正在开发一个 Jenkins 声明式管道,只要发布分支发生变化,管道就会触发。发布分支以这种格式命名:release/v1.0、release/v2.0 等。
每当创建新的发布分支时,我们都使用 Git PollSCM 阶段来检查。
stage ('git checkout')
{
steps
{
checkout poll:true, scm: ([$class: 'GitSCM',
branches: [[name: 'origin/release/*']],
userRemoteConfigs: [[credentialsId: "${GIT_CREDENTIALS_ID}", url: "${GIT_URL}"]]
])
}
}
我面临从发布分支获取版本号的问题,即如果我当前的发布分支是“release/v1.0”,我想从发布名称中提取“v1.0”。
当我使用 echo "${env.BRANCH_NAME}" 时,它返回我的“管道即代码”存储库的分支名称,而不是我在“git checkout”阶段提到的那个。
解决方案
我可以使用以下代码在声明性管道中获取当前结帐分支名称。
阶段('git结帐')
{
steps
{
script
{
def scmVars = checkout poll:true, scm: ([$class: 'GitSCM',
branches: [[name: 'origin/release/*']],
userRemoteConfigs: [[credentialsId: "${GIT_CREDENTIALS_ID}", url: "${GIT_URL}"]]
])
def releaseName=scmVars.GIT_BRANCH
}
}
}
推荐阅读
- r - 在大型数据集上运行 R 脚本时如何防止计算机崩溃
- javascript - 使用 javascript 平滑滚动到某些内容的最佳方法是什么?
- c# - 如何在 c# 中使用实体框架 6.1 在遗留模式下添加存储过程?
- python - 如何在 pycharm 上创建多个 django 项目而不会与其他端口冲突
- google-apps-script - 在谷歌应用脚本中迭代过滤数据透视表
- angularjs - 如何使用 api 前缀在 NGINX 中配置 Laravel API 和 Angular 前端
- python - Python 错误 ModuleNotFoundError:没有名为“pypresence”的模块
- c# - 将多个标签添加到 div (C#)
- excel - 在 Excel 中根据 2 个分隔符拆分单元格
- javascript - 了解我需要在何处/何时使用 onAuthStateChanged() - Firebase / Next.js