Search code examples
jenkinsjenkins-pipelinejenkins-groovyjenkins-job-dsl

get fileParam file absolute path


I defined a pipeline job as follows:

pipelineJob('job/path/job name') {
...
parameters {
    ...
    fileParam("HASHES", "json file")
    ...
}

then, in the subsequent script how do I get the file full path? If I do echo "${HASHES}" it correctly prints the file name (for example hashes.json) however I can't find out where the file is.

If I do for example sh("ls ${workspace") the file is not listed


Solution

  • There is a long lasting issue (JENKINS-27413) with the native file parameter plugin and pipeline support - which basically means that in pipelines jobs the file is not loaded automatically into your job's workspace when the pipeline is executed.

    To overcome this a dedicated plugin was created called the File Parameter Plugin which offers alternative types of file parameter that are compatible with Pipeline and do not suffer from the architectural flaws of the type built into Jenkins core.

    When using this parameter type you gain several methods to load it into your workspace when needed.The easiest is to use the withFileParameter step:

    stage('Example') {
        steps {
            withFileParameter('HASHES') {
              sh 'cat $HASHES'
            }
        }
    }
    

    This can also be called several times for separated stages running on different agents.

    Using this parameter type in the Job DSL is also quite simple:

    // Simple file parameter compatible with Pipeline.
    base64File {
       name(String value)
       description(String value)
    }
    

    And in your script:

    pipelineJob('job/path/job name') {
    ...
    parameters {
        ...
        base64File {
           name("HASHES")
           description("json file")
        }
        ...
    }