2
votes

Per File Parameter 'help text' on Jenkins build,

Accepts a file submission from a browser as a build parameter. The uploaded file will be placed at the specified location in the workspace, which your build can then access and use. This is useful for many situations, such as:

  • Letting people run tests on the artifacts they built.
  • Automating the upload/release/deployment process by allowing the user to place the file.
  • Perform data processing by uploading a dataset.

The name of the submitted file is available in the environment variable whose name is the same as file location. For example, if you set the file location to be abc.zip, then ${abc.zip} would give you the original file name passed from the browser (such as my.zip.) The name will not include the directory name portion.

File upload is optional. If a user chooses not to upload anything, Jenkins will simply skips this parameter and will not place anything (but it also will not delete anything that's already in the workspace.)

And 'File location'

Specifies the location, relative in the workspace, where the uploaded file will be placed (for example, like "jaxb-ri/data.zip")

Simple upload of zip file per example tried - doesn't seem to upload file anywhere - Neither in Workspace , nor under some temp directory. How to locate the file &/ make use of it?.

Here is simple pipeline for attempt on file upload..

properties(
    [
        parameters(
            [ file(name: "file1", file: "file1.zip", description: 'Choose path to upload file1.zip from local system.') ]
            )
    ]
)

node {
    stage("Upload File") {
        sh '''
        ls -lrt
        ls  ${file1.zip} ${file1} file1.zip
        '''
     }


}

And respective error on run as observed in Console log.

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Upload File)
[Pipeline] sh
[testSh] Running shell script
+ ls -lrt
total 0
Workspacedir///testSh@tmp/durable-ba40512f/script.sh: line 4: ${file1.zip}: bad substitution
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

Have tried Groovy suggestions per this (multiple ways) : Fetching binary or zipped uploaded files in Jenkins - Windows cannot open the folder . The Compressed(zipped) Folder is invalid , but no luck on making this work.

2
Actual problem is locating uploaded file, I couldn't able to locate with printing path with different format as part of ls command. Tried looking up on server as well, but don't see it is present under ${WORKSPACE} or temp dir or any other directory.. Any pointers on locating stored/uploaded file from file-parameter Jenkins job would helpvinWin

2 Answers

3
votes

With limitations around using file parameter with scripted pipeline, just a simple free-style job looks to do trick :( .

I am going with it for now & proceed with downstream dependency or similar on consuming uploaded artifacts.

enter image description here

With this it just uploads file & no tampering of file size &/ it's contents :)

Accessibility wise it is fetching by ${paramNameDefined}.

Under execute shell build type we can just put ${file1} for example above to do the trick of fetching file..

So here is output of running above free style job!

Copying file to file1
[sharedspace] $ /bin/sh -xe /tmp/hudson3237344085461334672.sh
+ ls file1.zip
file1.zip
Finished: SUCCESS
1
votes

Question Jenkins Pipeline Job with file parameter already provides the answer to this question.

new hudson.FilePath(new File("file1.zip")).copyFrom(file1)
file1.delete()