无法使用 Groovy Jenkinsfile 读取 Git 环境变量 Jenkins

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38044846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:04:44  来源:igfitidea点击:

Not able to read Git Environment variables Jenkins using Groovy Jenkinsfile

gitjenkinsgroovyjenkins-pipelinejenkinsfile

提问by NateW

The Git Plugin is installed (by default) in my Jenkins but I'm unable to get the env variables that are supposed to be passed in by the Git Plugin. I'm looking for:

Git 插件(默认情况下)安装在我的 Jenkins 中,但我无法获取应该由 Git 插件传入的 env 变量。我在找:

GIT_COMMIT
GIT_BRANCH
GIT_PREVIOUS_COMMIT 
GIT_PREVIOUS_SUCCESSFUL_COMMIT
GIT_URL

etc. I'm using the Pipeline Job Item that's pointing at a Github repo with the Jenkinsfile with the following code

等等。我正在使用指向 Github 存储库的流水线作业项,其中包含 Jenkinsfile 和以下代码

stage 'PushToProd'
node {
    git url: "https://github.com/username/fakeurl.git"
    echo "Starting PushToProd"
    sh 'printenv'
    sh 'env'
    sh 'echo $BRANCH_NAME' 
    sh 'echo $GIT_COMMIT'
}

I'm getting plenty of environment variables when I use envor printenvjust not the Github plugin ones.
Any tips on how I can get the Git env variables passed in to the job?

当我使用envprintenv不使用 Github 插件时,我得到了很多环境变量。
关于如何将 Git 环境变量传递给作业的任何提示?

Update: I'm able to easily get the Git env variables when I use a Freestyle Project and have a shell step use echo $GIT_COMMIT. Still want to know though how to get it to work using Jenkinsfile + Pipeline job item.

更新:当我使用 Freestyle 项目并有一个 shell 步骤 use 时,我能够轻松获取 Git 环境变量echo $GIT_COMMIT。仍然想知道如何使用 Jenkinsfile + Pipeline 作业项让它工作。

回答by Konrad Kleine

This won't work due to lack of double quotes, missing curly braces, and missing env.:

由于缺少双引号、缺少大括号和缺少env.

sh 'echo $BRANCH_NAME' 

This works as expected in a Jenkinsfile:

这在 Jenkinsfile 中按预期工作:

node {
    sh "echo ${env.BRANCH_NAME}"
}

回答by Breedly

So, anyone else who has stumbled onto this Stackoverflow issue should be aware that this is a bugwith the current pipeline situation.

因此,任何偶然发现此 Stackoverflow 问题的人都应该意识到这是当前管道情况的一个错误

Lots of discussion here: https://issues.jenkins-ci.org/browse/JENKINS-35230

这里有很多讨论:https: //issues.jenkins-ci.org/browse/JENKINS-35230

Essentially the plugin is not able to correctly add environment variables due to some incompatibilities with pipeline.

本质上,由于与管道的某些不兼容,插件无法正确添加环境变量。

回答by Morgan Christiansson

This is fixed in Git plugin 3.3.1

这在Git 插件 3.3.1 中已修复

Version 3.3.1 (Jun 23, 2017)

版本 3.3.1(2017 年 6 月 23 日)

  • Print first line of commit message in console log (JENKINS-38241)
  • Allow scm steps to return revision (JENKINS-26100)
  • Don't require crumb for POST to /git/notifyCommit even when CSRF is enabled (JENKINS-34350)
  • Fix credentials tracking null pointer exception in pipeline library use (JENKINS-44640)
  • Fix credentials tracking null pointer exception in git parameters use (JENKINS-44087)
  • 在控制台日志中打印提交消息的第一行 (JENKINS-38241)
  • 允许 scm 步骤返回修订版 ( JENKINS-26100)
  • 即使启用了 CSRF,也不需要 POST 到 /git/notifyCommit 的 crumb (JENKINS-34350)
  • 在管道库使用中修复凭据跟踪空指针异常 (JENKINS-44640)
  • 在 git 参数使用中修复凭据跟踪空指针异常 (JENKINS-44087)

回答by Noah Bar-Shain

Most of the pipeline examples I've found wrap the code in a "node" closure, which for some reason doesn't allow the Jenkins Git plugin to populate the environment variables.

我发现的大多数管道示例都将代码包装在“节点”闭包中,出于某种原因,这不允许 Jenkins Git 插件填充环境变量。

However, if instead you wrap it like this, the environment variables are set properly:

但是,如果您像这样包装它,则环境变量设置正确:

pipeline {
  agent {
    label ('<AGENT>')
  }
  stages {
    stage('<STAGE>') {
      steps {
        <CODE>
      }
    }
  }
}

I'm not sure which closures are crucial here, but this formatting allowed me to access env variables such as env.GIT_COMMIT

我不确定哪些闭包在这里至关重要,但是这种格式允许我访问环境变量,例如 env.GIT_COMMIT

回答by Oscar Romero

You may need to do something like this.

你可能需要做这样的事情。

node {
   def branch = env.BRANCH_NAME
   sh "My branch name: ${branch}"
}