在 Jenkins 作业的构建中,是否可以中途更改构建参数?

发布于 2024-11-28 22:53:52 字数 394 浏览 1 评论 0原文

我们正在使用 Jenkins 来自动化我们的多个构建和测试流程。对于我们的某些流程,开始构建的工程师需要指定一个参数。但该参数的可能值和最佳值的范围在一天中不断变化。

我想做的是让工程师指定一个值 - 如果他们知道最佳值 - 或者将其留空并通过早期构建步骤计算一个值。如果计算了该值,我希望计算构建步骤更新作业的参数值。这样,所有后续构建步骤都不必担心使用该参数或计算它,它们只是使用该参数。

看起来 Groovy Script Plugin 可能能够做到这一点,但我不知道如何设置构建参数,只需获取它们。

We are using Jenkins to automate several of our build and test processes. For some of our process, the engineer starting the build needs to specify a parameter. But the range of possible and optimal values for that parameter change throughout the course of the day.

What I would like to do is let the engineer specify a value - if they know an optimal value - or leave it blank and have a value be calculated by an early build step. If the value is calculated, I would like the calculating build step to update the parameter value of the job. That way, all subsequent build steps don't have to worry about using the parameter or calculating it, they just use the parameter regardless.

It looks like the Groovy Script Plugin might be able to do this, but I can't see how I can SET the build parameters, just GET them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

打小就很酷 2024-12-05 22:53:53

找到答案:使用 EnvInject 插件。其中一项功能是构建步骤,允许您从设置文件将参数“注入”到构建作业中。我使用一个构建步骤来创建设置文件,然后使用另一个构建步骤来注入新值。然后,所有后续构建步骤和构建后操作都使用新值。

更新示例:

在此处输入图像描述

要添加新参数 (REPORT_FILE),请基于现有参数 (JOB_NAME),在Groovy 脚本框:

// Setting a map for new build parameters
def paramsMap = [:]

// Set REPORT_FILE based on JOB_NAME
def filename = JOB_NAME.replace(' ','_') + ".html"
paramsMap.put("REPORT_FILE", filename)

// Add or modify other parameters...

return paramsMap

Found the answer: use the EnvInject Plugin. One of the features is a build step that allows you to "inject" parameters into the build job from a settings file. I used one build step to create the settings file, then another build step to inject the new values. Then, all subsequent build steps and post-build operations used the new value.

Update with an example:

enter image description here

To add a new parameter (REPORT_FILE), based on existing one (JOB_NAME), inject a map with new or modified parameters in the Groovy Script box:

// Setting a map for new build parameters
def paramsMap = [:]

// Set REPORT_FILE based on JOB_NAME
def filename = JOB_NAME.replace(' ','_') + ".html"
paramsMap.put("REPORT_FILE", filename)

// Add or modify other parameters...

return paramsMap
居里长安 2024-12-05 22:53:53

Jenkins 确实有能力参数化构建。对于字符串参数,开发人员可以将该字段留空,然后您的构建脚本可以检查 env.设置参数的变量。如果环境。变种未设置,脚本可以执行所需的任何计算(我不认为 Jenkins 有“预构建步骤”)并将其传递。对于选择参数,第一行可以类似于(Default),构建脚本可以再次测试其值并采取相应的操作。

注意(默认)

我尝试将选择框的第一行留空,Jenkins 第一次正确保存了它;但是当我回来重新配置构建时,Jenkins 对选项进行了某种修剪,并且删除了前导空白行,因此我选择了 (Default)

我希望这有帮助,
扎卡里

Jenkins does have the ability to parameterize builds. For a string parameter, the developer can leave the field blank and then your build scripts can check to see if the env. variable for the parameter is set. If the env. var. is not set, the script can perform whatever calculation is needed (I don't think Jenkins has "pre-build steps") and pass it along. For a choice parameter the first line can be something like (Default), and again the build script can test its value and act accordingly.

Note on (Default)

I tried leaving the first line of the choice box blank, and Jenkins saved it correctly the first time; but when I came back to reconfigure the build Jenkins ran some kind of trim on options and the leading blank line was removed so I settled on (Default).

I hope this helps,
Zachary

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文