How can I check for directory existence before docker cp in jenkins pipeline

577 views Asked by At

In my pipeline, I run testcases in the docker container then I copy some directories from the docker container to the Jenkins workspace.

It isn't necessary that all directories will exist in the docker container (for example screenshot dir may exists or not according to failing tests). How can I check for directory or file existence before copying it from docker container.

Here the part I mention in the pipeline

 post {
        always {
            echo 'Generating Test Reports ...'
            sh 'make posttest'
            echo('Copying Test Files ...')
            sh 'docker cp container-name:/app/results/mochareports/assets/videos ./results'
            sh 'docker cp container-name:/app/results/mochareports/assets/screenshots ./results'
            sh 'docker cp container-name:/app/results/mochareports/report.html ./results'
            echo 'Publish Test Reports ...'
            publishHTML (target : [allowMissing: false,
                alwaysLinkToLastBuild: true,
                keepAll: true,
                reportDir: 'results/mochareports',
                reportFiles: 'report.html',
                reportName: 'Cypress Test Reports',
                reportTitles: 'The test report'])
            echo 'Destroy Build'
            sh 'make destroy'
            cleanWs()
        }
    }
1

There are 1 answers

0
GeF On

Groovy-way is to use fileExists https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#fileexists-verify-if-file-exists-in-workspace

if(fileExists('/app/results/mochareports/assets/screenshots/')) {
  ...
}