Jenkins Workflow Checkout 访问 BRANCH_NAME 和 GIT_COMMIT [英] Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT

查看:25
本文介绍了Jenkins Workflow Checkout 访问 BRANCH_NAME 和 GIT_COMMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法从 Jenkins Workflow Checkout 步骤中提取 $GIT_COMMIT 和 $BRANCH_NAME.

I cannot seem to extract $GIT_COMMIT and $BRANCH_NAME from a Jenkins Workflow Checkout step.

我希望能够将此信息发送到我的 Gradle 脚本,以便将其传递到静态分析等外部来源.

I would like to be able to send this information through to my Gradle scripts in order to pass it onto external sources such as Static analysis etc.

目前我尝试运行这个:

checkout([$class: 'GitSCM', branches: [[name: '*/master']], userRemoteConfigs: [[credentialsId: '2b74a351-67d5-4d00-abd3-49842a984201', url: 'ssh://git@corporate.com:repo.git']]])

我想实现以下或类似的目标:

And I would like to achieve the following or something similar:

// Specified variables that can be reused
def branch = ${BRANCH_NAME}
def commit = ${GIT_COMMIT}

或者这也可以:

print "BRANCH: ${BRANCH_NAME}, COMMIT: ${GIT_COMMIT}"
// or the following
print "BRANCH: ${env.BRANCH_NAME}, COMMIT: ${env.GIT_COMMIT}"

我确实发现了以下似乎已解决但在 1.15 版中不起作用的问题:

I did find the following issue which seems to be resolved but it doesn't work in version 1.15:

https://issues.jenkins-ci.org/browse/JENKINS-30252

任何人都知道如何解决这个问题,或者如果有一个我找不到的变量?

Anyone have any ideas how to workaround this or if there's a variable I cannot find?

推荐答案

首先,

def branch = ${BRANCH_NAME}

不是有效的 Groovy,或者至少没有按照你的想法去做.也许你的意思是

is not valid Groovy, or at least not doing what you think. Perhaps you meant

def branch = "${BRANCH_NAME}"

这只是一种愚蠢的写作方式

which would just be a silly way of writing

def branch = BRANCH_NAME

无论如何,环境变量目前不能作为管道中的 Groovy 变量直接访问(有一个提议允许这样做);你需要使用 env 全局变量:

Anyway environment variables are not currently accessible directly as Groovy variables in Pipeline (there is a proposal to allow it); you need to use the env global variable:

def branch = env.BRANCH_NAME

从外部进程中,例如 sh 步骤,它是一个实际的环境变量,所以

From within an external process, such as a sh step, it is an actual environment variable, so

sh 'echo $BRANCH_NAME'

有效(注意 ' 表示 Groovy 对变量进行插值).

works (note that ' means Groovy is not interpolating the variable).

现在,JENKINS-30252 指的是多分支项目.如果您创建了独立的 Pipeline 作业,则不会设置此变量.

Now, JENKINS-30252 was referring to multibranch projects. If you created a standalone Pipeline job, this variable will not be set.

无论如何,在您的情况下,您的 checkout 步骤始终是检查 master 分支.如果您实际上有一个多分支项目,那么您的 Jenkinsfile 应该使用

Anyway in your case your checkout step is always checking out the master branch. If you actually have a multibranch project, then your Jenkinsfile should be using

checkout scm

这将检查正确分支上的提交(始终匹配 Jenkinsfile 本身的修订版).

which will check out a commit on the correct branch (always matching the revision of Jenkinsfile itself).

关于提交哈希,待处理的 JENKINS-26100 这不是自动可用的,但您可以使用类似的东西

As to the commit hash, pending JENKINS-26100 this is not available automatically, but you can use something like

sh 'git rev-parse HEAD > commit'
def commit = readFile('commit').trim()

访问它.

这篇关于Jenkins Workflow Checkout 访问 BRANCH_NAME 和 GIT_COMMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆