Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a FileParameterValue in a jenkins 2 pipeline

How can a file from the current project workspace be passed as a parameter to another project.

e.g. something like:

build job: 'otherproject', parameters: [[$class: 'FileParameterValue', name: 'output.tar.gz', value: ??? ]], wait: false
like image 745
Tom Deseyn Avatar asked May 18 '16 09:05

Tom Deseyn


1 Answers

The java.File object only can recover files from the master node.
So to load the files as a java.File objects we use the master node to unstash the required files, then we wrap them as file objects and finally we send them as a FileParameterValue objects.

node("myNode") {
    sh " my-commands -f myFile.any " // This command create a new file.
    stash includes: "*.any", name: "my-custom-name", useDefaultExcludes: true
}

node("master") {
    unstash "my-custom-name"
    def myFile = new File("${WORKSPACE}/myFile.any")
    def myJob = build(job: "my-job", parameters: 
                    [ string(name: 'required-param-1', value: "myValue1"),
                      new FileParameterValue("myFile.any", myFile, "myFile.any")
                    ], propagate: false)

    print "The Job execution status is: ${myJob.result}."

    if(myJob.result == "FAILURE") {
      error("The Job execution has failed.")
    }
    else {
      print "The Job was executed successfully."
    }
}

You could skip the master node If the file that you need to send contain only text.

def myFileContent = readFile("myFile.txt")
FilePath fp = new FilePath(new File("${WORKSPACE}","myFile.txt"))
if(fp!=null){
    fp.write(myFileContent, null)
}
def file = new File("${WORKSPACE}/myFile.txt")

Then use the file on the FileParameterValue object as usual.
Don't forget to import the FilePath object -> import hudson.FilePath

like image 84
Jose Luis Choque Chavez Avatar answered Sep 28 '22 03:09

Jose Luis Choque Chavez