Jenkins-有没有办法解析控制台输出并设置环境变量 [英] Jenkins - Is there a way to parse console output and set environment variable

查看:343
本文介绍了Jenkins-有没有办法解析控制台输出并设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对詹金斯来说还比较陌生.我陷入一种需要读取控制台输出并找到特定字符串并将其设置为环境变量的情况.我将在一些下游作业中使用此变量.

I am relatively new to Jenkins. I am into a scenario where I need to read the console output and find a specific string and set this as environment variable. This variable I would be using in some downstream jobs.

例如:我的詹金斯工作的控制台将包含类似

for example: my jenkins job's console would contain something like

product_build_number: 123456

我已经研究过FindText,类似于插件的日志解析器,但是它们并不能帮助我将此值设置为环境变量

I have looked into FindText, log parser like plugins but they don't help me in setting this value as environment variable

有人可以帮助找到这个数字并将其传递给下游工作吗?

Can somebody help in finding this number and pass it to downstream jobs?

**

更新为答案:

Updated with Answer:

**

def matcher = manager.getLogMatcher(".*product_build_number=(\\d+.*)")
if(matcher.matches()) {
    pbn= matcher.group(1).substring(0)
    manager.build.setDescription(pbn) // you can do anything with this here
}

推荐答案

考虑使用示例:

下面的脚本放置一个警告标志,如果它检测到使用了不赞成使用的方法,则将其标记为不稳定.

The script below puts a warning badge and mark the build as unstable if it detects that deprecated methods were used.

if(manager.logContains(".*uses or overrides a deprecated API.*")) {
    manager.addWarningBadge("Thou shalt not use deprecated methods.")
    manager.createSummary("warning.gif").appendText("<h1>You have been warned!</h1>", false, false, false, "red")
    manager.buildUnstable()
}

解决方案

完整的解决方案不仅使用 Groovy Postbuild插件进行解析控制台输出并设置变量BUILD_MSG,但还需要参数化触发器插件触发带有预定义参数MSG=${BUILD_MSG}的下游作业.下游作业会自动访问MSG参数.

Solution

Complete solutions uses not only Groovy Postbuild Plugin to parse console output and to set variable BUILD_MSG, but also needs Parameterized Trigger Plugin to trigger downstream job with predefined parameter MSG=${BUILD_MSG}. Downstream job automatically has access to MSG parameter.

Groovy Postbuild脚本:

Groovy Postbuild script:

import hudson.model.*


matcher = manager.getLogMatcher('^build msg: (.*)$')

if (matcher.matches()) {
    buildMsg=matcher.group(1)

    manager.build.addAction(
        new ParametersAction([
            new StringParameterValue("BUILD_MSG", "${buildMsg}")
        ])
    )
}

这篇关于Jenkins-有没有办法解析控制台输出并设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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