Home > database >  How do you cleanup Jenkins workspace after failure?
How do you cleanup Jenkins workspace after failure?

Time:09-24

We use Jenkins to test our Drupal site builds. One of the stages is the install process. When there is a failure in the Jenkins pipeline stage, a directory that was used to build the local testing directory becomes a "leftover" artifact and Jenkins is unable to clean up the files. They are left owned as root and even trying to do a "Wipe out repository and force clone" does not succeed on the next build due to these left over files.

The directory is first mapped with:

sh "docker create -P --name=${DB_CONTAINER_NAME} -v `pwd`/deploy:/var/www/html:Z llnl-lamp:latest"

We later try to clean it up:

post {
        always {
            sh "rm -rf `pwd`/deploy"
            sh "docker stop ${DB_CONTAINER_NAME} || true && docker rm ${DB_CONTAINER_NAME} || true;"
        }

We have also tried:

post {
        always {
            sh "docker exec -u root ${DB_CONTAINER_NAME} rm -rf /var/www/html/deploy"
            sh "docker stop ${DB_CONTAINER_NAME} || true && docker rm ${DB_CONTAINER_NAME} || true;"
        }

We have even switched the order, but these don't work and these files are left in the Jenkins workspace:

ls -lsaR
.:
total 172
  4 drwxr-xr-x    3 jenkins jenkins   4096 Sep 16 14:08 .
164 drwxr-xr-x 1323 jenkins jenkins 163840 Sep 16 15:54 ..
  4 drwxrwxrwx    3 jenkins jenkins   4096 Sep 16 14:08 deploy

./deploy:
total 12
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 .
4 drwxr-xr-x 3 jenkins jenkins 4096 Sep 16 14:08 ..
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 docroot

./deploy/docroot:
total 12
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 .
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 ..
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 sites

./deploy/docroot/sites:
total 12
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 .
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 ..
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 www

./deploy/docroot/sites/www:
total 12
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 .
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 ..
4 drwxrwxr-x 5 root    root    4096 Sep 16 14:03 files

./deploy/docroot/sites/www/files:
total 24
4 drwxrwxr-x 5 root    root    4096 Sep 16 14:03 .
4 drwxrwxrwx 3 jenkins jenkins 4096 Sep 16 14:08 ..
4 -r--r--r-- 1 root    root     487 Sep 16 14:03 .htaccess
4 drwxrwxr-x 3 root    root    4096 Sep 16 14:03 media-icons
4 drwxrwxr-x 2 root    root    4096 Sep 16 14:03 styles
4 drwxrwxr-x 2 root    root    4096 Sep 16 14:03 xmlsitemap

./deploy/docroot/sites/www/files/media-icons:
total 12
4 drwxrwxr-x 3 root root 4096 Sep 16 14:03 .
4 drwxrwxr-x 5 root root 4096 Sep 16 14:03 ..
4 drwxrwxr-x 2 root root 4096 Sep 16 14:03 generic

./deploy/docroot/sites/www/files/media-icons/generic:
total 44
4 drwxrwxr-x 2 root root 4096 Sep 16 14:03 .
4 drwxrwxr-x 3 root root 4096 Sep 16 14:03 ..
8 -rw-rw-r-- 1 root root 5294 Sep 16 14:03 audio.png
4 -rw-rw-r-- 1 root root 3900 Sep 16 14:03 generic.png
4 -rw-rw-r-- 1 root root 3343 Sep 16 14:03 instagram.png
8 -rw-rw-r-- 1 root root 7318 Sep 16 14:03 no-thumbnail.png
4 -rw-rw-r-- 1 root root 4064 Sep 16 14:03 twitter.png
8 -rw-rw-r-- 1 root root 4156 Sep 16 14:03 video.png

./deploy/docroot/sites/www/files/styles:
total 8
4 drwxrwxr-x 2 root root 4096 Sep 16 14:03 .
4 drwxrwxr-x 5 root root 4096 Sep 16 14:03 ..

./deploy/docroot/sites/www/files/xmlsitemap:

The errors when trying to delete the files are "no permission" errors, even when using the exec -u root. But these same files that it doesn't have permission to remove, it had permissions to create.

CodePudding user response:

You are correct in using post { always{}}, that should do the trick to run those commands.

I recommend looking closer at your builds console output. You might find that the variables you are using in that stage is out of scope, or perhaps jenkins might not have permission to delete files from the docker containers because they may have different file permissions and ownership.

Additionally, you may wish to try removing the use of || true , as that will ignore a failure, and might make it harder to find the issue of why those commands are not working as expected.

CodePudding user response:

Jenkins user (jenkins?) does not have permissions to remove files created by root user root. This is normal.

To remove the files, either use sudo:

 sh "sudo rm -rf `pwd`/deploy"

or change owner first:

 sh "sudo chown -R jenkins:jenkins `pwd`/deploy && rm -rf `pwd`/deploy"
  • Related