How to pass a file parameter to another build job in jenkins pipeline?
Asked Answered
H

4

6

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', ????]]
Handicraft answered 18/5, 2016 at 9:56 Comment(0)
G
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

Germiston answered 28/10, 2017 at 0:36 Comment(0)
W
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 
}
Westlund answered 18/5, 2016 at 14:36 Comment(0)
X
2

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
Xenomorphic answered 11/5, 2022 at 7:37 Comment(0)
P
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.

Performance answered 4/6, 2020 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.