How to Upload a file in a Pipeline


  • How can I upload a file in a Pipeline?



Waiting for JENKINS-27413 to be fixed, uploading a file in a Pipeline involves manually copying the file from the master 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 = ''

//file is uploaded to $JENKINS_HOME/$PATH_TO_THE_JOB/build/$BUILD_ID
def masterFilePath = input message: 'Upload your archive', parameters: [file(description: 'archive', name: uploadedFile)]

node('agent') { 
    stage('Copy From Master') {
        def localFile = getContext(hudson.FilePath).child(uploadedFile)
        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

Have more questions?


Please sign in to leave a comment.