datahub/gradle/docker/docker-utils.gradle
Chakru 40106be208
build: optimizations for incremental builds and faster CI (#13033)
Co-authored-by: Andrew Sikowitz <andrew.sikowitz@acryl.io>
2025-04-02 11:51:10 +05:30

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
}
}
}