mirror of
https://github.com/datahub-project/datahub.git
synced 2025-07-03 23:28:11 +00:00
42 lines
1.5 KiB
Groovy
42 lines
1.5 KiB
Groovy
ext.getDockerImages = {
|
|
docker_registry, docker_repo, docker_tag ->
|
|
def stdOut = new ByteArrayOutputStream()
|
|
exec {
|
|
commandLine "docker", "images", "-q", "${docker_registry}/${docker_repo}:${docker_tag}"
|
|
standardOutput = stdOut
|
|
}
|
|
return stdOut.toString().trim().split("\\R").findAll {!it.empty}.unique() as List
|
|
}
|
|
|
|
ext.getDockerContainers = {
|
|
docker_registry, docker_repo, docker_tag ->
|
|
def stdOut = new ByteArrayOutputStream()
|
|
exec {
|
|
commandLine "docker", "container", "ls", "-q", "--filter", "ancestor=${docker_registry}/${docker_repo}:${docker_tag}"
|
|
standardOutput = stdOut
|
|
}
|
|
return stdOut.toString().trim().split("\\R").findAll {!it.empty}.unique() as List
|
|
}
|
|
|
|
ext.cleanLocalDockerImages = {
|
|
String docker_registry, String docker_repo, String docker_tag ->
|
|
println("Docker image string: ${docker_registry}/${docker_repo}:${docker_tag}")
|
|
def containers = getDockerContainers(docker_registry, docker_repo, docker_tag)
|
|
if(!containers.isEmpty()) {
|
|
println "Stopping containers: $containers"
|
|
exec {
|
|
commandLine = ["docker", "container", "stop"] + containers
|
|
}
|
|
exec {
|
|
commandLine = ["docker", "container", "rm"] + containers
|
|
}
|
|
}
|
|
def images = getDockerImages(docker_registry, docker_repo, docker_tag)
|
|
if(!images.isEmpty()) {
|
|
println "Removing images: $images"
|
|
exec {
|
|
ignoreExitValue true // may not work if used by downstream image
|
|
commandLine = ["docker", "rmi", "-f"] + images
|
|
}
|
|
}
|
|
} |