0

In our Jenkins pipeline we have following command

if(!fileExists("dist/main/sourcemaps/")) {
    sh "curl ${SERVER_URL}/${revision}.zip -o artifact.zip"
    sh 'unzip artifact.zip -d dist && mv dist/main/* dist/'
}

Which cause following error

mv: can't rename 'dist/main/sourcemaps': File exists

I don't understand why fileExists method doesn't check folder existence and how to fix this problem

Solution:

I was smart enough to check not a destination directory, but a source directory. It should be as follows:

if(!fileExists("dist/sourcemaps/")) {
    sh "curl ${SERVER_URL}/${revision}.zip -o artifact.zip"
    sh 'unzip artifact.zip -d dist && mv dist/main/* dist/'
}

although we've refactored it to more bash-style implementation

if [ ! -d dist/sourcemaps ]; then
  curl ${SERVER_URL}/${revision}.zip -o artifact.zip
  unzip artifact.zip -d dist && mv dist/main/* dist/
fi
Timothy
  • 2,805
  • 2
  • 16
  • 25
  • 1
    I believe the error message says that the destination also exists **and is a directory**. – user1934428 Apr 16 '21 at 13:14
  • I believe so as well... but if statement should also validate its existance – Timothy Apr 16 '21 at 14:14
  • but `fileExists` check for a file right? and you provide a path, a directory in fact? have a look [here](https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#fileexists-verify-if-file-exists-in-workspace) and this [solution](https://stackoverflow.com/questions/38534781/check-if-a-file-exists-in-jenkins-pipeline). I believe it ran successfully at some point, created the `sourcemaps` dir and after that the check returns false because the path provided is not a file, thus getting the error. You could wipe your workspace anyway – Vivere Apr 16 '21 at 15:53
  • try using `mv -v` to obtain some more debug – Chris Gillatt Apr 16 '21 at 16:28
  • @Timothy: IMO, `os.path.exists` would be the method to use for checking existence of file **or** directory. – user1934428 Apr 17 '21 at 16:51

0 Answers0