如何从管道作业访问git分支名称? [英] How to access git branch name from pipeline job?

查看:131
本文介绍了如何从管道作业访问git分支名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jenkins Pipeline作业,该作业配置为检出git repo和特定的本地分支.

I have a Jenkins Pipeline job which is configured to checkout a git repo and a specific local branch.

如何在我的Jenkinsfile中获取本地分支的名称?

我尝试加载git jenkins插件 env属性,但具有没有运气.

I tried to load the git jenkins plugin env properties but had no luck.

node {
  checkout scm
  echo "1 "+ env.GIT_LOCAL_BRANCH
  echo "2 "+ env.GIT_BRANCH
}

两个值均为"null"

Both values are "null"

推荐答案

我发现我可以捕获checkout scm的返回值,并使用它来获取分支名称(和其他值)

I found that I can capture the return value from checkout scm and use that to get the branch name (and other values)

  def scmVars

  node('api-sample-build') {
    stage('Clone source code') {
        scmVars = checkout scm
        // scmVars contains the following values
        // GIT_BRANCH=origin/mybranch
        // GIT_COMMIT=fc8279a107ebaf806f2e310fce15a7a54238eb71
        // GIT_PREVIOUS_COMMIT=6f2e319a1fc82707ebaf800fce15a7a54238eb71
        // GIT_PREVIOUS_SUCCESSFUL_COMMIT=310fce159a1fc82707ebaf806f2ea7a54238eb71
        // GIT_URL=https://stash.someworkplace.com/scm/poc/api-sample.git
    }
    stage('test scope') {
      echo scmVars.GIT_BRANCH
    }
  }

通过在节点外部定义变量,可以在结帐后分阶段使用它.

By defining the variable outside the node it is available in stages after the checkout.

这篇关于如何从管道作业访问git分支名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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