0

I am using Jenkins declarative pipeline syntax and I need to check if a file exists. Otherwise it should abort the current stage. The problem I encounter is that the file contains a timestamp which is different every time the build process runs.

I have found this thread. But sadly they use a plugin I dont have access to, so it does not fit my problem.

Here is what I have so far:

    stage('Check if file exists') {
        steps {
            script {
                if(fileExists('./path/to/file/name_1234567890.tar.gz')) {
                    currentBuild.result = "ABORTED"
                    error('Could not find file!')
                }
            }
        }
    }

Thanks in advance.

1
  • If your OS build is linux or windows, I think you can use bash script or powershell and save stdout and compare with groovy script. Commented Jul 25, 2023 at 10:16

1 Answer 1

1

Use findFiles which allows glob syntax:

stage('Check if file exists') {
        steps {
            script {
                def any_files = false
                def output_files = findFiles glob: './path/to/file/name_*.tar.gz'
                for (def one_file in output_files) { any_files = true; break }
                if (any_files) { // or maybe (!any_files) 
                    currentBuild.result = "ABORTED"
                    error('Could not find file!')
                }
            }
        }
    }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.