How can a file from the current workspace be passed as a parameter to a build job, e.g.:
build job: 'other-project', parameters: [[$class: 'FileParameterValue', ????]]
How can a file from the current workspace be passed as a parameter to a build job, e.g.:
build job: 'other-project', parameters: [[$class: 'FileParameterValue', ????]]
What a nightmare - there is no documentation, looked into jenkins code, etc.. Tried everything
Eventually found out that this doesn't currently work. Here is the jenkins bug.
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
You need to pass in a FileParameterValue
http://javadoc.jenkins.io/hudson/model/FileParameterValue.html
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
}
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:
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'. 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.
© 2022 - 2024 — McMap. All rights reserved.