使用前操纵Jenkins作业参数 [英] Manipulate Jenkins Job Parameter before using

查看:83
本文介绍了使用前操纵Jenkins作业参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望定义一个Jenkins构建参数,通常由用户以特定方式输入.为了使用该参数,必须通过以下简单函数(在shell脚本中)对其进行转换

I wish to define a Jenkins build parameter, that would typically be entered by a user in a particular way. In order to be used this parameter must be transformed by a simple function as follows (in shell script)

if [ "" != "${SUBPROJ}" ]
then 
    CLEAN=`echo ":${SUBPROJ}:${CLEAN}" | sed 's|/|:|g' `
    END_TARGET=`echo ":${SUBPROJ}:${END_TARGET}" | sed 's|/|:|g' `
fi  

这会将a/b/c之类的内容转换为:a:b:c:

This will convert something like a/b/c to :a:b:c:

在使用参数之前,我如何在Jenkins中进行此转换.运行Shell脚本步骤不会更改原始参数.

How in Jenkins can I do this conversion before the parameter is used. Running a shell script step does not change the original parameter.

我查看了动态参数插件但这似乎不允许从提供的参数中输入

I looked at the dynamic parameter plugin but that doesn't appear to allow for input from the supplied parameters

推荐答案

在特定构建步骤中创建和更改的变量仅适用于该构建步骤.如果要在脚本步骤之后公开在构建步骤中编辑过的变量,则需要获取该变量并将新版本放回环境中.我通常通过将值作为键值对写入文件并使用 EnvInject插件.

Variables you create and alter in a particular buildstep are scoped just to that build step. If you want to expose a variable you edited to the build steps after your script step you need to take the variable and put the new version back into the environment. I normally do this by writing the value to a file as a key value pair and using the EnvInject Plugin.

以您的示例为例,"CLEAN"变量现在包含:a:b:c:作为脚本的下一步,您需要将其写入文件(我将其称为newEnvironmentVariables.txt),如果要替换的话SUBPROJ字符串,那么您应该执行以下操作:

Taking your example the 'CLEAN' variable now holds :a:b:c: As the next step in your script you need to write that to a file (which I will call newEnvironmentVariables.txt), if you want to replace the SUBPROJ string then you should do something like:

if [ "" != "${SUBPROJ}" ]
then 
    CLEAN=`echo ":${SUBPROJ}:${CLEAN}" | sed 's|/|:|g' `
    echo "CLEAN=${CLEAN}" > ${WORKSPACE}\newEnvironmentVariables.txt 
    END_TARGET=`echo ":${SUBPROJ}:${END_TARGET}" | sed 's|/|:|g' `
    echo "END_TARGET=${END_TARGET}" >> ${WORKSPACE}\newEnvironmentVariables.txt 
fi  

这应该产生一个包含两行的文本文件(我假设CLEAN和END_TARGET首先是空的.)

This should produce a text file that contains two lines (I've assumed CLEAN and END_TARGET were empty to begin with).

CLEAN =:a:b:c:
END_TARGET =:a:b:c:

CLEAN=:a:b:c:
END_TARGET=:a:b:c:

在脚本之后立即添加一个Envinject构建步骤,并将该文件的路径放在属性文件路径"字段中.如果脚本在工作空间的根目录下运行,则可以使用${WORKSPACE}\newEnvironmentVariables.txt作为路径.

Add an Envinject build step immediately after your script and place the path to the file in the Properties File Path field. If your script was running at the root of your workspace you can use ${WORKSPACE}\newEnvironmentVariables.txt as your path.

这将在jenkins构建作业的环境中创建两个名为CLEAN和END_TARGET的新变量,您可以在后续构建步骤中使用它们,就像其他任何变量一样.如果要更改SUBPROJ的值,则可以将其用作键值对中的键之一.

This will create two new variables in your jenkins build job's environment called CLEAN and END_TARGET that you can use in subsequent build steps like any other variable. If you want change the value of SUBPROJ then use that as one of the keys in your key value pairs.

这篇关于使用前操纵Jenkins作业参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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