当使用管道作为代码时,在Jenkins声明性管道中从Git提取分支名称



我正在开发一个Jenkins声明性管道,每当发布分支发生变化时,该管道就会触发。发布分支的命名格式如下:Release/v.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}"]]
])
}    
}

我面临着从发布分支获取版本号的问题,即如果我当前的发布分支是";版本/v.0";"我想提取";v1.0";来自发布名称。

当我使用echo"时${env.BRANCH_NAME}";它返回我的"的分支名称;流水线作为代码";不是我在";git结账";阶段

我可以使用以下代码在声明性管道中获取当前签出分支名称。

阶段("it checkout"(

{
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

}
} 
}

最新更新