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
. How can this be achieved? Please note that I can't know the version before running job1
.
我需要从job1中提取版本(解析输出??)并使其以某种方式作为job2的参数。怎么能实现这一目标?请注意,在运行job1之前我无法知道版本。
4 个解决方案
#1
9
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脚本构建步骤中简单地使用导出的问题是,当shell脚本退出时,导出的变量会消失,它们不会传播到作业。
Use the EnvInject plugin to create environment variables in your build. If you write out a properties file as part of your build, EnvInject can read the file and inject variables as a build step. A properties file has a simple KEY=VALUE
format:
使用EnvInject插件在构建中创建环境变量。如果您在构建过程中写出属性文件,EnvInject可以读取文件并将变量注入构建步骤。属性文件具有简单的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 )
#2
0
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.
你可以在这个问题中找到解析字符串的好例子。
#3
0
If job2 always should get some information from job1 you could use approach without parameters. job1 could publish artifact with version and job2 will use that artifact (with Copy Artifact Plugin for example). With that approach job2 could be executed also as standalone job.
如果job2总是应该从job1获取一些信息,你可以使用没有参数的方法。 job1可以使用版本发布工件,job2将使用该工件(例如,使用复制工件插件)。通过这种方法,job2也可以作为独立的工作执行。
#4
0
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.
这会将任何句点转换为下划线,并将所有字母大写。使用脚本编写器脚本可确保您拥有一个独立于您正在使用的“插件汤”的方法。
#1
9
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脚本构建步骤中简单地使用导出的问题是,当shell脚本退出时,导出的变量会消失,它们不会传播到作业。
Use the EnvInject plugin to create environment variables in your build. If you write out a properties file as part of your build, EnvInject can read the file and inject variables as a build step. A properties file has a simple KEY=VALUE
format:
使用EnvInject插件在构建中创建环境变量。如果您在构建过程中写出属性文件,EnvInject可以读取文件并将变量注入构建步骤。属性文件具有简单的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 )
#2
0
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.
你可以在这个问题中找到解析字符串的好例子。
#3
0
If job2 always should get some information from job1 you could use approach without parameters. job1 could publish artifact with version and job2 will use that artifact (with Copy Artifact Plugin for example). With that approach job2 could be executed also as standalone job.
如果job2总是应该从job1获取一些信息,你可以使用没有参数的方法。 job1可以使用版本发布工件,job2将使用该工件(例如,使用复制工件插件)。通过这种方法,job2也可以作为独立的工作执行。
#4
0
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.
这会将任何句点转换为下划线,并将所有字母大写。使用脚本编写器脚本可确保您拥有一个独立于您正在使用的“插件汤”的方法。