Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-27413

Handle file parameters

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      JENKINS-27295 discusses getting values from various kinds of parameters. Handling FileParameterValue is another matter. buildEnvironment (what is called today) is useless since it gives only a file name. createBuildWrapper is the way this works in a freestyle project, but this cannot work in a workflow; even if it were to return a SimpleBuildWrapper (JENKINS-24673) it is not clear where that would be called, since we can only use it from a workspace. getValue as currently implemented is useless since a FileItem does not have whitelisted methods, and anyway we would not want the flow itself to be copying streams to the workspace; this needs to be done by infrastructure. The only way forward I can see at the moment is for getValue to return a SimpleBuildWrapper, so that your flow could read

      node {
        wrap([$delegate: parameters.myFileParam]) {
          sh 'cat myFileParam'
        }
      }
      

        Attachments

          Issue Links

            Activity

            Hide
            baukaman Bauyrzhan Makhambetov added a comment -

            Thank you for pointing that out.

            Show
            baukaman Bauyrzhan Makhambetov added a comment - Thank you for pointing that out.
            Hide
            ifernandezcalvo Ivan Fernandez Calvo added a comment - - edited

            my 5 cents, this code shows how to load a properties file into the environment, in the example the properties come from a String but also you can load a file from disk with the readProperties pipeline utility step

            node {
               def configPrperties = """
               VAR01 = value1
               VAR02 = value2
               """
               def config = readProperties(text: configPrperties)
               config.each { k, v ->
                   env."${k}" = v
               }
               sh 'export'
            }
            
            Show
            ifernandezcalvo Ivan Fernandez Calvo added a comment - - edited my 5 cents, this code shows how to load a properties file into the environment, in the example the properties come from a String but also you can load a file from disk with the readProperties pipeline utility step node { def configPrperties = """ VAR01 = value1 VAR02 = value2 """ def config = readProperties(text: configPrperties) config.each { k, v -> env. "${k}" = v } sh 'export' }
            Hide
            deseao Alex Haynes added a comment -

            If there is no plan to support the file parameter option, it would be nice to remove it from the documentation here: https://jenkins.io/doc/book/pipeline/syntax/#parameters

            Show
            deseao Alex Haynes added a comment - If there is no plan to support the file parameter option, it would be nice to remove it from the documentation here:  https://jenkins.io/doc/book/pipeline/syntax/#parameters
            Hide
            jglick Jesse Glick added a comment -

            Alex Haynes good point! You can help.

            Show
            jglick Jesse Glick added a comment - Alex Haynes good point! You can help .
            Hide
            hogarthj James Hogarth added a comment - - edited

            Avoid actively breaking it though please as as we do actually use this functionality with the help of our global library. 

             

            def inputGetFile(String savedfile = null) {
             def filedata = null
             def filename = null
             // Get file using input step, will put it in build directory
             // the filename will not be included in the upload data, so optionally allow it to be specified
             if (savedfile == null) {
             def inputFile = input message: 'Upload file', parameters: [file(name: 'library_data_upload'), string(name: 'filename', defaultValue: 'uploaded-file-data')]
             filedata = inputFile['library_data_upload']
             filename = inputFile['filename']
             } else {
             def inputFile = input message: 'Upload file', parameters: [file(name: 'library_data_upload')]
             filedata = inputFile
             filename = savedfile
             }
             // Read contents and write to workspace
             writeFile(file: filename, encoding: 'Base64', text: filedata.read().getBytes().encodeBase64().toString())
             // Remove the file from the master to avoid stuff like secret leakage
             filedata.delete()
             return filename
            }

            And this library code is used with this jenkinsfile snippet:

            stage('request a file with chosen filename') {
             node {
             deleteDir()
             uploaded_file = library.inputGetFile('my-file-here')
             sh "file '${uploaded_file}'"
             sh "ls -la ."
             }
            }

             

             

            Show
            hogarthj James Hogarth added a comment - - edited Avoid actively breaking it though please as as we do actually use this functionality with the help of our global library.    def inputGetFile( String savedfile = null ) { def filedata = null def filename = null // Get file using input step, will put it in build directory // the filename will not be included in the upload data, so optionally allow it to be specified if (savedfile == null ) { def inputFile = input message: 'Upload file' , parameters: [file(name: 'library_data_upload' ), string(name: 'filename' , defaultValue: 'uploaded-file-data' )] filedata = inputFile[ 'library_data_upload' ] filename = inputFile[ 'filename' ] } else { def inputFile = input message: 'Upload file' , parameters: [file(name: 'library_data_upload' )] filedata = inputFile filename = savedfile } // Read contents and write to workspace writeFile(file: filename, encoding: 'Base64' , text: filedata.read().getBytes().encodeBase64().toString()) // Remove the file from the master to avoid stuff like secret leakage filedata.delete() return filename } And this library code is used with this jenkinsfile snippet: stage( 'request a file with chosen filename' ) { node { deleteDir() uploaded_file = library.inputGetFile( 'my-file-here' ) sh "file '${uploaded_file}' " sh "ls -la ." } }    

              People

              • Assignee:
                Unassigned
                Reporter:
                jglick Jesse Glick
              • Votes:
                89 Vote for this issue
                Watchers:
                96 Start watching this issue

                Dates

                • Created:
                  Updated: