5

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', ????]]
Tom Deseyn
  • 1,735
  • 3
  • 17
  • 29

4 Answers4

3

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

TheJeff
  • 3,665
  • 34
  • 52
2

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 
}
Daniel Hernández
  • 4,078
  • 6
  • 27
  • 38
1

Now you can use the latest File Parameters plugin to implement it.

Here's a simple example:

test-parent pipeline

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))])
                    }
                }
            }
        }
    }
}

test-child pipeline

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:

  1. Build test-parent pipeline with parameters, start with a test file
  2. test-parent pipeline invokes test-child pipeline with the test file which was uploaded at Step 1
  3. test-child pipeline prints the test file content to console
Shawn Hu
  • 349
  • 2
  • 8
0

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.