38
votes

I'm putting together a Jenkins pipeline job which will take a file parameter. I can trigger the job and point it at a file however I can't find where the file has ended up (In an ordinary freestyle job it would be in the workspace).

Where has the uploaded file gone? Or do file parameters not currently work with pipelines?

9

9 Answers

19
votes

There is currently an issue with pipeline and file parameter (https://issues.jenkins-ci.org/browse/JENKINS-27413).

13
votes

Solved it the following way:

node {
    deleteDir()
    stage("upload") {
        def inputFile = input message: 'Upload file', parameters: [file(name: 'data.zip')]
        new hudson.FilePath(new File("$workspace/data.zip")).copyFrom(inputFile)
        inputFile.delete()
    }
    stage("checkout") {
        echo fileExists('data.zip').toString()
    }
}

I know the solution is not that beautiful because the pipeline gets interrupted for the upload but it works.

Further the "copyFrom" is necessary, because the input stores the "data.zip" in the jobs directory and not in the workspace (don't know why)

9
votes

Found a WA (Strictly for text based file input) We can use Jenkins multi-line string parameter and ask user to paste file contents to it. And in our pipeline, write contents of this parameter using pipeline step writeFile, as :

stage('File Param WA') {
          writeFile file: 'demo.yaml', text: params.DEMO_YAML
}

Multi Line String Parameter

3
votes

I tried using the solution provided by @Christoph Forster , but the input File was not getting copied anywhere in the workspace . So I used the workaround as provided in https://bitbucket.org/janvrany/jenkins-27413-workaround-library/src/6b7dada8ea37?at=default

The library provides a new library - unstashParam - that saves the file build parameter into a workspace. Works fine with text and yaml file .

2
votes

I also tried using the solution by @Christoph Forster but I received a script security error when Groovy Sandbox is enable

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new hudson.FilePath java.io.File

However, it seems we can skip the file copying and deleting actions (and bypass the Groovy sandbox restriction) by simply requiring that the file is uploaded to the job workspace. Just add the workspace variable to the file name as follows:

stage("upload") {
def inputFile = input message: 'Upload file', parameters: [file(name: "$workspace/data.zip")]
        }
2
votes

I found a solution in the form of a global library here: https://bitbucket.org/janvrany/jenkins-27413-workaround-library/src/default/

It contains calls to inner methods of Jenkins which are deprecated (I guess). So I made my own version like this:

import hudson.FilePath
import hudson.model.ParametersAction
import hudson.model.FileParameterValue
import hudson.model.Executor

def call(String name, String fname = null) {
    def paramsAction = currentBuild.rawBuild.getAction(ParametersAction.class);

    if (paramsAction == null) {
        error "unstashParam: No file parameter named '${name}'"
    }

    for (param in paramsAction.getParameters()) {
        if (param.getName().equals(name)) {
            if (! param instanceof FileParameterValue) {
                error "unstashParam: not a file parameter: ${name}"
            }
            if (env['NODE_NAME'] == null) {
                error "unstashParam: no node in current context"
            }
            if (env['WORKSPACE'] == null) {
                error "unstashParam: no workspace in current context"
            }
            workspace = new FilePath(getComputer(env['NODE_NAME']), env['WORKSPACE'])
            filename = fname == null ? param.getOriginalFileName() : fname
            file = workspace.child(filename)
            file.copyFrom(param.getFile())
            return filename;
        }
    }
}


def getComputer(name){

    for(computer in Jenkins.getInstance().getComputers()){ 
        if(computer.getDisplayName() == name){
            return computer.getChannel()
        }
    }

    error "Cannot find computer for file parameter workaround"
}

You can insert it in a global library and then use it like:

library "file-workaround"

node {
    def file_in_workspace = unstashParam "myFile"
    sh "cat ${file_in_workspace}"
}

It's not pretty but it's working and as long as there is no official fix, it's my best shot.

Update
Turns out you might run into "No such file or directory". That's because nothing in the workaround triggers Jenkins to create the workspace directory. If that was triggered somewhere else in the pipeline good, otherwise you'll be scratching your head.
You might wanna throw a

touch "thisIsAFile"

in there

1
votes

To handle an optional file parameter in pipeline (to handle the use case where no file should be accepted) you could use jenkinsci-unstashParam-library (add it in Jenkins>Manage Jenkins>Configure System>Global Pipeline Libraries https://github.com/janvrany/jenkinsci-unstashParam-library) with a try/catch in a script as this sample stage:

    stage('upload') {
            steps {
                // delete workspace
                cleanWs()

                // handle file parameters in pipeline (JENKINS-27413)
                script {

                    try {
                        // force workspace directory creation
                        sh "touch emptyFileToCreateWorkspace"

                        // https://stackguides.com/questions/59468464/fetching-uploaded-files-in-jenkins
                        def file_in_workspace = unstashParam 'MY_FILE.xlsx'

                        // https://unix.stackexchange.com/questions/125776/error-with-a-file-name-containing-parentheses
                        sh "mv '${file_in_workspace}' MY_FILE.xlsx"
                    }
                    catch (Exception e) {
                        echo e.getMessage()
                        echo "No file parameter, we will continue.."
                    }
                }
            }
        }
0
votes

Tried what Christoph suggested and it didnt work for me. Here is what worked for me and the setup which I have, his should help others figure out what to do.

Problem: I am executing my pipeline on dedicated nodes and use sanitized workspaces. After some research and troubleshooting I found out that by default the file upload only works with Master node. I realized this after digging through the file system and finding the file I am uploading in the workspace on the master

Solution:

stage('Upload Key') {
    agent { label 'master' }
    steps {
        script {
            // Uploads file via master node and stases it for other nodes to access
            def inputFile = input message: 'Upload file', parameters: [file(name: "key.p12")]
            new hudson.FilePath(new File("${workspace}/key.p12")).copyFrom(inputFile)
            inputFile.delete()
        }
        stash name: 'key.p12' , includes: "key.p12"
    }
}
    stage('Register') {
        steps {
            ws (sanitizedWorkspaceName) {
                echo "Registering"
                unstash 'key.p12'
            }
        }
    }
  1. Execute the suggested file copy solution by Christoph. This stores the file in the job workspace on the master node
  2. Allow the scripts in Manage Jenkins > In Process Script approval
  3. use the stash step to stash the uploaded file
  4. In the target stage "running on a different node" use the unstash

Hope this helps

0
votes

I wasn't able to make Christoph's solution working if the file was uploaded on master node and needed on slave. The solution was to stash it on master and later unstash it on slave. Don't forget to remove the uploaded file on master node.