Jenkins - How to run - docker system prune - in the pipeline

Viewed 2368

This is my Jenkinsfile for building docker image and pushing it to dockerhub. Everything works just great. I would like to clean up the untagged images after the build process. Currently I do docker system prune -f manually on the Jenkins node. Is there anyway to incorporate when agent is none?

pipeline {
    agent none  
    stages {
        stage('Build Jar') {
            agent {
                docker {
                    image 'maven:3.6.0'
                    args '-v $HOME/.m2:/root/.m2'
                }
            }
            steps {
                sh 'mvn clean package'
            }
        }
        stage('Build Image') {
            steps {
                script {
                    app = docker.build("myimagename")
                }
            }
        }
        stage('Push Image') {
            steps {
                script {
                    app.push("latest")
                }
            }
        }
    }
}
0 Answers
Related