How to cleanup workspace built from docker

27 views
Skip to first unread message

Den

unread,
May 12, 2020, 3:51:43 AM5/12/20
to Jenkins Users
Hi,

I'm building artifacts in docker image and if I'm aborting a build, the deleteDir function fails to delete artifacts since
they were created with root user in docker and the function is called by jenkins user outside of docker. Is there any workaround?

pipeline {
    agent {label 'docker'}
    stages {
        stage('Build') {
            agent {
                docker {
                    image 'adoptopenjdk/openjdk8' args '-u root -v $HOME/.m2:/root/.m2' reuseNode true label 'docker'
                }
            }
            stages {
                stage ('Package') {
                        sh "./mvnw package"
                }
            }
    post {
always {deleteDir()}
    }
        }
    }
    post {
        always {deleteDir()}
    }
}

Gianluca

unread,
May 12, 2020, 4:21:52 AM5/12/20
to Jenkins Users
The best solution is not run any command as root inside docker.
Use the environment variables UID and GID and run the docker with a user corresponding to the UID and GIU; something like "--user $UID:$GID" instead of root

Den

unread,
May 12, 2020, 4:50:08 AM5/12/20
to Jenkins Users
And how to mount .m2 directory then? Currently I do: args '-u root -v $HOME/.m2:/root/.m2'

Den

unread,
May 12, 2020, 8:58:23 AM5/12/20
to Jenkins Users
If I omit -u root then jenkins uses 'docker run -t -d -u 993:990 -v $HOME/.m2:/root/.m2' command to run the image. That works, but it will download all dependencies every time. When using root user, the repository is mounted and maven uses already downloaded dependencies.


Am Dienstag, 12. Mai 2020 10:21:52 UTC+2 schrieb Gianluca:
Reply all
Reply to author
Forward
0 new messages