简体   繁体   English

Jenkins + Build Flow,如何将变量从一个作业传递到另一个作业

[英]Jenkins + Build Flow, how to pass a variable from one job to another

I have a build flow scenario similar to the documentation example : two jobs, one running after the other. 我有一个类似于文档示例的构建流程场景:两个作业,一个在另一个之后运行。

b = build("job1")
build("job2", param1: b.????)

My job1 is a shell script that builds a package out of a checked out git repositoy and prints out the version of the built package. 我的job1是一个shell脚本,它从签出的git repositoy构建一个包,并打印出构建包的版本。

I need to extract the version from job1 (parse output??) and make it available somehow as a parameter to job2 . 我需要从job1提取版本(解析输出??)并使其以某种方式作为job2的参数。 How can this be achieved? 怎么能实现这一目标? Please note that I can't know the version before running job1 . 请注意,在运行job1之前我无法知道该版本。

The problem with simply using export in a shell script build step is that the exported variables disappear when the shell script exits, they are not propagated up to the job. 在shell脚本构建步骤中简单地使用export的问题是,当shell脚本退出时,导出的变量会消失,它们不会传播到作业。

Use the EnvInject plugin to create environment variables in your build. 使用EnvInject插件在构建中创建环境变量。 If you write out a properties file as part of your build, EnvInject can read the file and inject variables as a build step . 如果您在构建过程中写出属性文件,EnvInject可以读取文件并将变量注入构建步骤 A properties file has a simple KEY=VALUE format: 属性文件具有简单的KEY=VALUE格式:

MY_BUILD_VERSION=some_parsed_value

Once you have an environment variable set in your job, in the Build Flow plugin, you can extract the variable's value and use it in subsequent jobs: 在作业中设置环境变量后,在Build Flow插件中,您可以提取变量的值并在后续作业中使用它:

def version = build.environment.get( "MY_BUILD_VERSION" )
out.println String.format("Parameters: version: %s", version)
build( "My Second Build", MY_BUILD_VERSION: version )

When you run job1 export the version with name as system property. 运行job1将名称作为系统属性的版本导出。

export appVersion="stringOfVersion-123"

Then it depend if you know how long is version (count of numbers or others characters). 然后,它取决于您是否知道版本的长度(数字或其他字符的数量)。 If you know it you can parse variable from end in second build as new variable and use it. 如果您知道它,您可以将第二个构建中的变量解析为新变量并使用它。

How parse string you can find in this question with nice examples . 你可以在这个问题中找到解析字符串的好例子

If job2 always should get some information from job1 you could use approach without parameters. 如果job2总是应该从job1获取一些信息,你可以使用没有参数的方法。 job1 could publish artifact with version and job2 will use that artifact (with Copy Artifact Plugin for example). job1可以使用版本发布工件,job2将使用该工件(例如,使用复制工件插件 )。 With that approach job2 could be executed also as standalone job. 通过这种方法,job2也可以作为独立的工作执行。

For anyone else coming upon this, another solution is to use a scriptler script, where you pass in the .properties file path, and the script will add the properties to the list of job variables: 对于其他任何人来说,另一个解决方案是使用脚本编写器脚本,在其中传入.properties文件路径,脚本将属性添加到作业变量列表中:

Properties properties = new Properties()

FilePath workspace = build.getWorkspace()
FilePath sourceFile = workspace.child(path)

properties.load(sourceFile.read())

properties.each { key, value ->
  key = key.replace(".", "_").toUpperCase()
  Job.setVariable(build, key, value)

  println "Created Variable: " + key + "=" + value
}

This will convert any periods to underscores, and capitalize all letters. 这会将任何句点转换为下划线,并将所有字母大写。 Using a scriptler script ensures that you have a method that works independent of the "plugin soup" you are using. 使用脚本编写器脚本可确保您拥有一个独立于您正在使用的“插件汤”的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM