0

I am trying to use file parameter which is not saving the file in the workspace for pipeline project its returning the filename that was uploaded (not the full path only the filename)

pipeline {
    agent any
    parameters {
        file(name:'FILE', description: 'upload excel file')
    }

    stages {
        stage('upload') {
            steps {
                script {
                    echo "Parameter file: ${params}" // output: Parameter file: [:]
                    echo "file name: ${FILE}" // file name: uploaded_file.xlsx
                    def workspace = pwd()
                    def fullPath = workspace + "\\" + FILE
                    if (fileExists(fullPath)) {
                        echo "File exists"
                    } else {
                        echo "File does not exist" // this will be run as fullPath has no such file.
                    }
                }
            }
        }
    }
}

Just to let you know params.FILE is null and FILE contains the filename. Now if I try to use readFile(FILE) its working and returning the content of the file (without full path as FILE is just the filename). So, its there a good solution to save the file in the workspace. I know we can use file parameter plugin but try to do it using file parameter (if possible). And any explanation how readFile is working?

uditkumar01
  • 400
  • 5
  • 11

1 Answers1

0

Seems that the file parameter is not working as expected in the pipeline but only in Freestyle jobs. You can find more on this topic here:

Jenkins Pipeline Job with file parameter

or here:

https://issues.jenkins.io/browse/JENKINS-27413

Regarding readFile, you can find the description in the Jenkins Docs here:

https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#readfile-read-file-from-workspace

In short, it's reading the contents of a file, relative to the workspace. So in case of an uploaded file that resides in the workspace root, for example this should work:

filecontents = readFile file: "filename.txt"
echo "${filecontents}"
Franco
  • 193
  • 1
  • 9