[英]How to pass a file parameter to another build job in jenkins pipeline?
How can a file from the current workspace be passed as a parameter to a build job, eg:如何将当前工作区中的文件作为参数传递给构建作业,例如:
build job: 'other-project', parameters: [[$class: 'FileParameterValue', ????]]
You can pass the full path of file, you could do:您可以传递文件的完整路径,您可以这样做:
node('master') {
//Read the workspace path
String path = pwd();
String pathFile = "${path}/exampleDir/fileExample.ext";
//Do whatever you wish with the file path
}
What a nightmare - there is no documentation, looked into jenkins code, etc.. Tried everything真是一场噩梦 - 没有文档,查看 jenkins 代码等。尝试了一切
Eventually found out that this doesn't currently work.最终发现这目前不起作用。 Here is the jenkins bug.
这是詹金斯错误。
https://issues.jenkins-ci.org/browse/JENKINS-27413 https://issues.jenkins-ci.org/browse/JENKINS-27413
Linked to from here: http://jenkins-ci.361315.n4.nabble.com/pipeline-build-job-with-FileParameterValue-td4861199.html从这里链接到: http : //jenkins-ci.361315.n4.nabble.com/pipeline-build-job-with-FileParameterValue-td4861199.html
You need to pass in a FileParameterValue您需要传入一个 FileParameterValue
http://javadoc.jenkins.io/hudson/model/FileParameterValue.html http://javadoc.jenkins.io/hudson/model/FileParameterValue.html
This approach assumes you have the file in the current job's workspace.此方法假定您在当前作业的工作区中有该文件。
pipeline
{
agent any
stages {
stage('Pass file type param to build job') {
steps {
script {
def propertiesFilePath = "${env.WORKSPACE}/sample.properties"
build job: 'other-project',
parameters: [[$class: "FileParameterValue", name: "propertiesFile", file: new FileParameterValue.FileItemImpl(new File(propertiesFilePath))]]
}
}
}
}
}
Here the name of the downstream/child job is 'other-project' and the name of the file type parameter in this downstream/child job is 'propertiesFile'.此处下游/子作业的名称为“other-project”,此下游/子作业中的文件类型参数名称为“propertiesFile”。 The type FileParameterValue.FileItemImpl is defined in the class FileParameterValue and is internally used in jenkins to handle FileItem, also adding serialization support to the same.
FileParameterValue.FileItemImpl类型在类FileParameterValue 中定义,并在 jenkins 内部使用以处理 FileItem,并为其添加序列化支持。
Now you can use the latest File Parameters plugin to implement it.现在您可以使用最新的文件参数插件来实现它。
Here's a simple example:这是一个简单的例子:
pipeline {
agent any
parameters {
base64File(name: 'testFileParent', description: 'Upload file test')
}
stages {
stage('Invoke Child Job') {
steps {
withFileParameter('testFileParent') {
script{
def fileContent = readFile(env.testFileParent)
build(job: 'test-child',
parameters: [base64File(name: 'testFileChild', base64: Base64.encoder.encodeToString(fileContent.bytes))])
}
}
}
}
}
}
pipeline {
agent any
parameters {
base64File(name: 'testFileChild', description: 'Upload file test')
}
stages {
stage('Show File') {
steps {
withFileParameter('testFileChild') {
sh("cat $testFileChild")
}
}
}
}
}
It works like this:它是这样工作的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.