3

I'm trying to set up a Jenkinsfile to run our CI pipeline. One of the steps will involve collecting files from across our directory tree and copying them into a single directory, for zipping up.

I'm attempting to do this using the Jenkins sh step and using glob patterns, but I can't seem to get this to work.

A simple example Jenkinsfile would be:

pipeline {
    agent any
    stages {
        stage('List with Glob'){
            steps{
                sh 'ls **/*.xml'
            }
        }
    }
}

I would expect that to list any .xml files in the workspace, but instead I receive:

[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (List with Glob)
[Pipeline] sh
[jenkinsfile-pipeline] Running shell script
+ ls '**/*.xml'
ls: cannot access **/*.xml: No such file or directory
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 2
Finished: FAILURE

I think i'm missing something with Groovy string interpolation, but I need some help for this specific case (running in a Jenkins pipeline via a Jenkinsfile)

Any help much appreciated!

  • Could be different shell being used than you expect, I believe everything in `sh` by default is executed by the bourne shell (not `bash`). – mkobit Dec 01 '17 at 19:18
  • I think you're right. I use `zsh` and it seems to support this out of the box, whereas the answer below has informed me that this isn't a valid glob pattern. Thanks – jamie--stewart Dec 02 '17 at 21:07
  • I would say that it is a valid glob pattern. it just only works without the single quotes! – Domi W Nov 24 '22 at 14:32

1 Answers1

3

As far as I can tell **/*.xml' isn't a valid glob pattern (see this). Instead what you have there is an ant naming pattern, which, as far as I know, isn't supported by bash (or sh). Instead what you wan't to do is to use find:

pipeline {
    agent any
    stages {
        stage('List with find'){
            steps{
                sh "find . -type f -name '*.xml'"
            }
        }
    }
}
Jon S
  • 15,846
  • 4
  • 44
  • 45
  • 2
    Hi, thanks for this. As the comments below my question say, I've been using `zsh` and didn't realise it supports the globstar for recursive matching out of the box. Your answer works great, I've used `find` and then `-exec` to copy the files to where they need to go. – jamie--stewart Dec 02 '17 at 21:12
  • This does not work if you are using something like `rm -f '${dir:?}/*'`, using `${var:?}` checks if the var exists before performing the action. In this case, nothing would happen. Cant use ''. – Dave Mar 01 '22 at 20:24