Jenkins工作流程签出访问BRANCH_NAME和GIT_COMMIT [英] Jenkins Workflow Checkout Accessing BRANCH_NAME and GIT_COMMIT

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

问题描述

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



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



目前我尝试运行这个:

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

我想实现以下或类似的功能:

  //可以重用的指定变量
def branch = $ {BRANCH_NAME}
def commit = $ {GIT_COMMIT}

也可以工作:

  printBRANCH:$ {BRANCH_NAME},COMMIT:$ {GIT_COMMIT}
//或以下
打印BRANCH:$ {env.BRANCH_NAME},COMMIT:$ {env.GIT_COMMIT}

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



https://问题.jenkins-ci.org / browse / JENKINS-30252



任何人有任何想法如何解决这个问题,或者如果有一个变量我找不到?

      

解决方案 > def branch = $ {BRANCH_NAME}

无效Groovy,或者至少没有按照您的想法。也许你的意思是

  def branch =$ {BRANCH_NAME}

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

  def分支= BRANCH_NAME 

无论如何环境变量当前不能直接作为Pipeline中的Groovy变量访问(有一个建议允许它);你需要使用 env 全局变量:

  def branch = env .BRANCH_NAME 

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

  sh'echo $ BRANCH_NAME'

工作(注意'意味着Groovy不是 em>插入变量)。



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



无论如何,您的情况下您的结帐步骤总是检出 master 分支。如果你实际上有一个multibranch项目,那么你的 Jenkinsfile 应该使用

  checkout scm 

这将检查正确分支上的提交(总是匹配 Jenkinsfile 本身)。



至于提交散列,挂起JENKINS-26100 不会自动提供,但您可以使用类似于

  sh'git rev-parse HEAD>提交'
def commit = readFile('commit')。trim()

它。


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

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.

Currently I try to run this:

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}

Or maybe this would work too:

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

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?

解决方案

First of all,

def branch = ${BRANCH_NAME}

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

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

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

sh 'echo $BRANCH_NAME'

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

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

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

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

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()

to access it.

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

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