Pipeline - Files manipulation


  • I am trying to port some legacy Jenkins build jobs that used Linux shell scripts (to Jenkins Workflow scripts) to download and stage/deploy to multiple server nodes. I need to be able to save and manipulate files to the Workflow workspace (or to somewhere under /tmp) directory structure.
  • When we use groovy library classes ( AntBuilder, File ) its working fine on Client-Master but its not working on Slave machines.


  • CloudBees Jenkins Enterprise
  • Pipeline plugin


The operation with File class are run on master,so only works if build is run on master, in this example I create a file and checks if I can access to it on a node with method exists, it does not exist because the “new File(file)” is executed on master, to check this I search for folder “Users” that exist on my master but not into node.

stage 'file move wrong way'

//it only works on master
node('slave') {

    def ws = pwd()
    def context  = ws + "/testArtifact"
    def file = ws + '/file'
    sh 'touch ' + file
    sh 'ls ' + ws

    echo 'File on node : ' + new File(file).exists()
    echo 'Users : ' + new File('/Users').exists()

    sh 'mv ' + file + ' ' + context
    sh 'ls ' + ws

To execute file manipulation command we recommend to use native commands.

This is a simple example of operations in shell

stage 'Init'
node {
    stage 'Environment'
    sh 'export'

    stage 'Create file'
    sh 'touch test.txt'

    stage 'List relative Directory'
    sh 'ls ../../../../../'

    stage 'List / dir'
    sh 'ls /'

    stage 'List Home of user'
    sh 'ls $HOME'

    stage 'List Home of agent'
    sh 'ls $JENKINS_HOME'

    stage 'List temp'
    sh 'ls $TMPDIR'

    stage 'List job dir'
    sh '''
        echo $(pwd)
        ls .
        ls $(pwd)

    stage 'download file'
    def out='$(pwd)/download/maven.tgz'
    sh 'mkdir -p ./download'
    sh 'curl -L http://ftp.cixug.es/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz -o ' + out

    stage 'move/rename'
    def newName = 'mvn.tgz'
    sh 'mkdir -p $(pwd)/other'
    sh 'mv ' + out + ' ' + newName
    sh 'cp ' + newName + ' ' + out


Have more questions?


Please sign in to leave a comment.