How to Upload a file in a Pipeline

Article ID:360046660152
1 minute readKnowledge base

Issue

  • How can I upload a file in a Pipeline?

Resolution

Waiting for JENKINS-27413 to be fixed, uploading a file in a Pipeline involves manually copying the file from the controller to the agent.

The following sample will:

  • ask the end user for the file (it is assumed that it is an archive)

  • copy it on the agent

  • prove that the file is visible on the agent

  • archive it back to the master

// assuming you wish to upload a zip archive def uploadedFile = 'uploaded.zip' //file is uploaded to $JENKINS_HOME/$PATH_TO_THE_JOB/build/$BUILD_ID def controllerFilePath = input message: 'Upload your archive', parameters: [file(description: 'archive', name: uploadedFile)] node('agent') { stage('Copy From controller') { def localFile = getContext(hudson.FilePath).child(uploadedFile) localFile.copyFrom(controllerFilePath) sh 'ls -al' archiveArtifacts uploadedFile } }

Note that, for this to work you will have to approve two methods in the script approval screen.

Tested product/plugin versions

  • Jenkins 2.190

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.