'file parameter in declarative pipeline

I am developing declarative pipeline and want to use file parameter to read its content, but its not working as expected

parameters{
        file(fileLocation:'list.txt', description:'contains list of projects to be build')
   }

I am getting following error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 12: Invalid parameter "fileLocation", did you mean "description"? @ line 12, column 14.
           file(fileLocation:'release-list.txt', description:'contains list of projects to be build')

Following is another option mentioned for basic step plugin

readFile: Read file from workspace
Reads a file from a relative path (with root in current directory, usually workspace) and returns its content as a plain string.
file
Relative ( /-separated) path to file within a workspace to read.
Type: String
encoding (optional)
Type: String

its working in script step like

def myfile = readFile('list.txt')
echo "${myfile}"

But how to use it directly in declarative script as we used other basic steps like dir??



Solution 1:[1]

Following syntax is working

parameters{
        file name:'list.txt', description:'contains list of projects to be build'
   }

But fileLocation parameter is not acceptable still.

Below syntax is available in Jenkins2 Up & Running book but its not working

parameters{
        file(fileLocation:'list.txt', description:'contains list of projects to be build')
   }

Solution 2:[2]

The correct arguments for the file parameter are name and description. So it should be:

file(name:'list.txt', description:'contains list of projects to be build')

However there's an open jenkins issue dating back from 2015 about the file parameter not working for pipelines, so I don't think even this will solve your issue. https://issues.jenkins-ci.org/browse/JENKINS-27413

Solution 3:[3]

Till outstanding issues gets fixed, I believe we may have to stick to freestyle mode & handle things either in downstream pipeline job or within same job leveraging needy plugin feature.

Here is my attempt which looks to work file irrespective (yes supports Binaries as well) types : https://i.stack.imgur.com/vH7mQ.png

${list.txt} will point to right file in your case..

Solution 4:[4]

Take a look at the plug-in https://plugins.jenkins.io/file-parameters/.

This plug-in adds support for file parameters in your Jenkinsfile: https://plugins.jenkins.io/file-parameters/#plugin-content-usage-in-declarative-pipeline

parameters {
  base64File 'small'
  stashedFile 'large'
}

https://github.com/jenkinsci/file-parameters-plugin

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ImranRazaKhan
Solution 2 Alisdair Robertson
Solution 3 vinWin
Solution 4 Jesse