Jenkins 将作业的参数/结果(由管道触发)返回到同一管道
詹金斯管道:我有一个管道Jenkins 将作业的参数/结果(由管道触发)返回到同一管道,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,詹金斯管道:我有一个管道p1,它触发作业j1,然后触发作业j2。我想要一些由j1设置并传递到管道p1中的j2的参数。如何使用Jenkins管道插件实现此功能?提前谢谢我也有类似的问题。我必须让作业J1、J2创建属性文件,然后使用主管道P1中的“复制工件”获取这些文件。然后将属性转换为Java属性(这可能需要Jenkins中的一些脚本批准)。如果Jenkins管道可以直接在代码中返回参数,那就太好了(也许还有其他方法可以做到这一点,但我不知道)。构建步骤的返回是一个简单的过程,它似乎无法返回我可以
p1
,它触发作业j1
,然后触发作业j2
。我想要一些由j1
设置并传递到管道p1
中的j2
的参数。如何使用Jenkins管道插件实现此功能?提前谢谢我也有类似的问题。我必须让作业J1、J2创建属性文件,然后使用主管道P1中的“复制工件”获取这些文件。然后将属性转换为Java属性(这可能需要Jenkins中的一些脚本批准)。如果Jenkins管道可以直接在代码中返回参数,那就太好了(也许还有其他方法可以做到这一点,但我不知道)。构建步骤的返回是一个简单的过程,它似乎无法返回我可以看到的自定义结果(除非我们使用一些现有属性,如构建描述)
所以我有这样的想法:
// Pipeline code in P1
// Build J1 and get result.
def j1BuildResult = build job: 'J1', parameters: [string(name: 'J1_PROP', value: 'FOO')], propagate: true, wait: true
// Get results of J1
step([$class : 'CopyArtifact', filter: 'j1-result.properties',
fingerprintArtifacts: true,
flatten : true,
projectName : 'J1',
selector : [$class : 'SpecificBuildSelector', buildNumber: buildResult.getNumber().toString()]])
// Load J1 properties (you may need to turn off sandbox or approve this in Jenkins)
Properties j1Props = new Properties()
j1Props.load(new StringReader(readFile('j1-result.properties')))
// Build J2
def j2BuildResult = build job: 'J2', parameters: [string(name: 'J2_PROP', value: j1Props.someProperty)], propagate: true, wait: true
// Get results of J2
step([$class : 'CopyArtifact', filter: 'j2-result.properties',
fingerprintArtifacts: true,
flatten : true,
projectName : 'J2',
selector : [$class : 'SpecificBuildSelector', buildNumber: buildResult.getNumber().toString()]])
// Load J2 properties (you may need to turn off sandbox or approve this in Jenkins)
Properties j2Props = new Properties()
j1Props.load(new StringReader(readFile('j2-result.properties')))
可以使用“env”来完成。如果您成功使j1
将其信息添加到构建的env
中
如果j1
是一条管道,则可以连接到env.MYKEY=MYVALUE
。
对于自由式的工作,它应该使用(没有尝试)。
在p1
中,如果超出构建结果,您将获得一个包含该信息的映射
因此,如果您在p1
中执行以下操作:
// somewhere in your pipeline, i.e. p1:
def j1BuildResult = build job: 'J1'
def j1EnvVariables = j1BuildResult.getBuildVariables();
然后,j1EnvVariables
将是一个包含您在j1
中设置的变量的映射
PS:如何将该信息传递给as parameters
p2
是一个很好的例子。您可以使用
这是一个非常棒的地图。目标参数可通过以下方式接收:
String someProperty = buildProperties.someProperty
限制:需要在“过程中脚本批准”中允许方法hudson.model.Run getEnvironment
,并在节点中调用此代码
闭包(因为rawBuild
)
我还尝试了runWrapper.rawBuild.getAction(ParametersAction.class)
,但它需要将许多导入文件
注意:
runWrapper.getBuildVariables()
没有为我返回任何信息。我的第一个答案并不完全是对你的问题的回答,但是我试图做类似的事情,但我所需要的只是对一个问题的“是/否”回答
我的第二个答案可能会帮助你或其他人,因为它会起作用
第一个答案:运行一个子作业,其中“失败”表示“否”,而“成功”表示“是”
只需确保在作业运行中设置“propagate:false”。这里有一张剪贴画:
def getos = build job: 'internal_is_host_running_linux',
parameters: [[$class: 'StringParameterValue',
name:'HOST',value: HOST]],propagate: false,wait:true
def linuxTest = getos.getResult()
if (linuxTest != "SUCCESS") {
// NOT success, meaning NOT booted Linux
我相信有100种更好的方法可以做到这一点,但这是可行的
第二个可能有效的答案是获取子作业的控制台输出,并在其中搜索所需的数据
下面是如何从subjob获取控制台输出,至少这对我来说是有效的:
def job = build job: 'internal_run_remote_command',
wait: true, propagate: false,
parameters: [[$class: 'StringParameterValue',
name: 'HOST', value: HOST],
[$class: 'StringParameterValue',
name: 'stageos', value: stageos]]
// put the standard output from the subjob into our output:
def checklog2 = job.rawBuild.log
// obviously, if you don't want the output in your console, do not println
println checklog2
显然,你可以搜索任何你想要的东西,而不是“println checklog2”。这是留给读者的练习;-)
(同样,可能有100种更好的方法可以做到这一点,但这对我很有效)
我的环境:仅使用管道。主主机必须运行管道脚本,但每隔一段时间需要在另一台主机上运行一些东西,但仍在主主机上运行,因此到目前为止,子作业似乎是我的选择。您可以通过
def myjob=build job: 'testy', propagate: true, wait: true, parameters: [string(name: 'ENV', value: 'jamshaid'), string(name: 'Repo', value: 'khalid')]
echo "${myjob.getResult()}"
这有助于:
从文件中:
getBuildVariables
public Map<String,String> getBuildVariables()
publicmap getBuildVariables()
异常异常
获取在生成中定义的环境变量。这不会报告参数化生成的生成参数,只报告生成环境。如果子作业是管道作业(WorkflowRun),则捕获并报告脚本执行期间设置的所有变量。
抛出:
AbortException在主管道中唯一对我有效的方法(在主管道中,我将触发多个构建并获取它们的变量以触发主管道中的其他测试作业)是使用管道的post步骤,如下所示: 子作业结束(在“阶段”部分之后): 在我的父母工作中:
script
{
buildResults = build job : "Build_Pipeline",
parameters : [
string( name: "BUILD_BRANCH", value: "master" )
],
wait: true
// Print the env variable set in the child job
println(buildResults.getBuildVariables()["BUILD_VARIABLE "])
}
你如何触发它们?你能分享一些代码吗?它为我生成了一个异常:org.kohsuke.stapper.NostapperConstructorException:在java.lang.String类的任何构造函数上都没有@DataBoundConstructor要修复的任何想法我尝试了“EnvInject plugin”,但它不起作用。在管道中工作得非常好。在
节点{}
块中设置env.foo=bar
,然后echo env.foo
在管道中的任何位置工作。谢谢“如果您设法使j1将其信息添加到生成的环境中”。使用声明性管道,我很难处理environment{}
部分,但它不起作用。有效的方法是在步骤中包含脚本{env.MYKEY=MYVALUE}
。这对我也有用。谢谢顺便说一下,我发现了一个开放的JIRA,因为它在环境{}
中不起作用:
post
{
success
{
script
{
env.BUILD_VARIABLE = "FOO"
}
}
}
script
{
buildResults = build job : "Build_Pipeline",
parameters : [
string( name: "BUILD_BRANCH", value: "master" )
],
wait: true
// Print the env variable set in the child job
println(buildResults.getBuildVariables()["BUILD_VARIABLE "])
}