如何在 Windows 2008 上运行的 Jenkins 中将 git short hash 输入到变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16943665/
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 08:35:41  来源:igfitidea点击:

how to get git short hash in to a variable in Jenkins running on windows 2008

gitjenkins

提问by user2367078

I am trying to get the git short hash in a variable. I tried to set GIT_COMMIT_SHORT variable to run 'git rev-parse --short HEAD' but it didn't work. I need this variable to pass to ant build script so the package name include this short hash.

我试图在变量中获取 git short hash。我试图设置 GIT_COMMIT_SHORT 变量来运行 'git rev-parse --short HEAD' 但它没有用。我需要将此变量传递给 ant 构建脚本,以便包名称包含此短哈希。

I am running Jenkins on windows 2008 server.

我在 Windows 2008 服务器上运行 Jenkins。

Thanks

谢谢

采纳答案by Eldad Assis

I suggest you use the EnvInjectplugin to load the parameter from a properties file you create that holds the hash.

我建议您使用EnvInject插件从您创建的包含哈希的属性文件中加载参数。

Echo the result of your gitcommand to a formed key=valuefile in your job's workspace and load it with the plugin I mentioned.

git命令的结果回显到key=value作业工作区中的已形成文件,并使用我提到的插件加载它。

I used it for such needs and it's very simple.

我将它用于此类需求,而且非常简单。

I hope this helps.

我希望这有帮助。

EDIT:How to write a properties file in windows:

编辑:如何在 Windows 中编写属性文件:

@for /f "delims=" %l in ('git command') do @echo hash=%l >> hash.properties

@for /f "delims=" %l in ('git command') do @echo hash=%l >> hash.properties

Then, you can load hash.propertieswith the plugin.

然后,您可以加载hash.properties插件。

回答by Jan Molak

Probably the simplest way to achieve the result you want would be to use the GIT_REVISION token makro, like this:

可能实现您想要的结果的最简单方法是使用 GIT_REVISION 令牌 makro,如下所示:

${GIT_REVISION,length=6}

Have a look at https://wiki.jenkins-ci.org/display/JENKINS/Token+Macro+Pluginfor more details.

有关更多详细信息,查看https://wiki.jenkins-ci.org/display/JENKINS/Token+Macro+Plugin

Hope this helps,
Jan

希望这会有所帮助,

回答by theGecko

This worked for me in a jenkins pipeline using the git plugin:

这在使用 git 插件的 jenkins 管道中对我有用:

SHORT_COMMIT = "${GIT_COMMIT[0..7]}"

回答by Dmytro Titov

Another possible option for the Blue Ocean Pipeline:

蓝海管道的另一个可能选择:

pipeline { 
    ...
    environment {
        GIT_COMMIT_SHORT = sh(
                script: "printf $(git rev-parse --short ${GIT_COMMIT})",
                returnStdout: true
        )
    }
    ...

回答by Ayub Malik

On jenkins 2.73 with git plugin this placeholder template works

在带有 git 插件的 jenkins 2.73 上,这个占位符模板有效

${GIT_REVISION:0:7}

I used it with the Delivery pipeline plugin to set the task name.

我将它与交付管道插件一起使用来设置任务名称。

E.g Building GIT rev. ${GIT_REVISION:0:7}

例如构建 GIT 修订版。 ${GIT_REVISION:0:7}

回答by pjanssen

Not sure if it applies but I was searching for a way to do this in a jenkins pipeline. I ended up using this ${GIT_REVISION[0..7]}

不确定它是否适用,但我正在寻找一种在 jenkins 管道中执行此操作的方法。我最终使用了这个${GIT_REVISION[0..7]}

回答by Maxim Suslov

Without any additional plugins you can get commit using the following command in Jenkins script ('Execute Windows batch command' build step):

无需任何其他插件,您可以在 Jenkins 脚本中使用以下命令进行提交(“执行 Windows 批处理命令”构建步骤):

for /f %%i in ('git rev-parse HEAD') do set GITHASH=%%i

In command line you should use single %instead of %%:

在命令行中,您应该使用 single%而不是%%

for /f %%i in ('git rev-parse HEAD') do set GITHASH=%%i

回答by howie

Only this work for me

只有这对我有用

GIT_SHA_SHORT=`git rev-parse --short=8 ${GIT_COMMIT}`

回答by yasin lachini

  return sh(
            script: "printf $(git rev-parse --short ${env.GIT_COMMIT})",
            returnStdout: true
        ).trim()
        }

        pipeline {
            agent any
            environment {
                SHORT_COMMIT = git_short_commit()

            }
            stages {
                stage('Build') {
                    steps {
                        echo "${SHORT_COMMIT}"
                   }
            }
        }
    }

回答by stricq

I didn't see this answer listed here so thought I'd add it:

我没有看到这里列出的这个答案,所以我想我会添加它:

environment {
  GIT_HASH = GIT_COMMIT.take(7)
}

Source: https://issues.jenkins-ci.org/browse/JENKINS-44449?focusedCommentId=361307&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-361307

资料来源:https: //issues.jenkins-ci.org/browse/JENKINS-44449?focusedCommentId=361307&page=com.atlassian.jira.plugin.system.issuetabpanels: comment-tabpanel#comment-361307