How to Upload a file in a Pipeline

Issue

  • How can I upload a file in a Pipeline?

Environment

Resolution

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 = 'uploaded.zip'

//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)
        localFile.copyFrom(masterFilePath)
        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?

0 Comments

Please sign in to leave a comment.