Search code examples
jenkinsjenkins-plugins

how to copy files from a directory outside the workspace to workspace in Jenkins pipeline


I'm starting with Jenkins pipelines, and I want to copy some video files from outside any jenkins directory to my job workspace directory. I'm using the File Operations Plugin to perform some file operations for other jobs I have. With that plugin, I'm able to copy files from inside my workspace to outside:

fileOperations([fileCopyOperation(excludes: '', flattenFiles: false, includes: "videos\\*.MTS", targetLocation: "H:\\home\\Videos")])

With this step, for example, I can copy 4 video files located in my workspace to the mentioned directory, located in another disk.

But I want to do the opposite. I want to copy video files from a source directory in the other disk to the workspace. I tried it in several ways, but seems that 'includes' field doesn't accept absolute paths. For example:

fileOperations([fileCopyOperation(excludes: '', flattenFiles: false, includes: "H:\\home\\Videos\\videos\\*.MTS", targetLocation: ".")])

This returned the following error in the console output:

File Copy Operation: FATAL: Expecting Ant GLOB pattern, but saw 'H:\home\Videos\videos\*.MTS'. See http://ant.apache.org/manual/Types/fileset.html for syntax

So, i'm stuck trying to carry some files to the workspace directory in order to be processed there.

Note: I'm using a declarative pipeline for my job.


Solution

  • In fact, seems that the problem is not copying files from outside the workspace but from outside the current working dir. I still don't know how to do this.

    But, you can change the current working dir to be the one that contains files you want to copy, so:

    dir("H:\\home\\Videos\\videos") {
        fileOperations([fileCopyOperation(excludes: '', flattenFiles: true, includes: '*.MTS', targetLocation: "${WORKSPACE}")])
    }
    

    This code allows you to copy mts files placed in the mentioned directory in the workspace dir. You can see additional help for the dir step here