diff --git a/.github/scripts/reclaim-docker-diskspace.sh b/.github/scripts/reclaim-docker-diskspace.sh new file mode 100755 index 00000000000..e32f3b1d8c8 --- /dev/null +++ b/.github/scripts/reclaim-docker-diskspace.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +echo "Reclaiming Docker Disk Space" +echo + +docker image ls --format "{{.Size}} {{.ID}} {{.Repository}} {{.Tag}}" | LANG=en_US sort -rh | while read line; do + size=$( echo "$line" | cut -d' ' -f1 | sed -e 's/\.[0-9]*//' | sed -e 's/MB/000000/' | sed -e 's/GB/000000000/' ) + image=$( echo "$line" | cut -d' ' -f2 ) + repository=$( echo "$line" | cut -d' ' -f3 ) + tag=$( echo "$line" | cut -d' ' -f4 ) + echo "Considering $image $repository:$tag $size" + if [[ "$tag" =~ ^[a-f0-9]{32}$ ]]; then + echo "Ignoring GitHub action image $image $repository:$tag" + elif [[ "$tag" == "" ]]; then + echo "Ignoring untagged image $image $repository:$tag" + elif [[ "$size" -lt 200000000 ]]; then + echo "Ignoring small image $image $repository:$tag" + else + echo "Cleaning $image $repository:$tag" + docker image rm $image + fi +done + +echo "Finished cleanup, leaving the following containers:" +echo +docker image ls --format "{{.Size}} {{.ID}} {{.Repository}}:{{.Tag}}" | LANG=en_US sort -rh +echo +df -h +echo diff --git a/buildSrc/src/main/java/org/springframework/boot/build/test/DockerTestPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/test/DockerTestPlugin.java index 6c33d0fb9a8..d614c388c96 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/test/DockerTestPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/test/DockerTestPlugin.java @@ -18,10 +18,12 @@ package org.springframework.boot.build.test; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.Task; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.provider.Provider; import org.gradle.api.services.BuildService; +import org.gradle.api.tasks.Exec; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.testing.Test; @@ -43,17 +45,19 @@ public class DockerTestPlugin implements Plugin { /** * Name of the {@code dockerTest} task. */ - public static String DOCKER_TEST_TASK_NAME = "dockerTest"; + public static final String DOCKER_TEST_TASK_NAME = "dockerTest"; /** * Name of the {@code dockerTest} source set. */ - public static String DOCKER_TEST_SOURCE_SET_NAME = "dockerTest"; + public static final String DOCKER_TEST_SOURCE_SET_NAME = "dockerTest"; /** * Name of the {@code dockerTest} shared service. */ - public static String DOCKER_TEST_SERVICE_NAME = "dockerTest"; + public static final String DOCKER_TEST_SERVICE_NAME = "dockerTest"; + + private static final String RECLAIM_DOCKER_SPACE_TASK_NAME = "reclaimDockerSpace"; @Override public void apply(Project project) { @@ -73,6 +77,8 @@ public class DockerTestPlugin implements Plugin { }); project.getDependencies() .add(dockerTestSourceSet.getRuntimeOnlyConfigurationName(), "org.junit.platform:junit-platform-launcher"); + Provider reclaimDockerSpace = createReclaimDockerSpaceTask(project, buildService); + project.getTasks().getByName(LifecycleBasePlugin.CHECK_TASK_NAME).dependsOn(reclaimDockerSpace); } private SourceSet createSourceSet(Project project) { @@ -110,4 +116,28 @@ public class DockerTestPlugin implements Plugin { }); } + private Provider createReclaimDockerSpaceTask(Project project, + Provider buildService) { + return project.getTasks().register(RECLAIM_DOCKER_SPACE_TASK_NAME, Exec.class, (task) -> { + task.usesService(buildService); + task.setGroup(LifecycleBasePlugin.VERIFICATION_GROUP); + task.setDescription("Reclaims Docker space on CI."); + task.shouldRunAfter(DOCKER_TEST_TASK_NAME); + task.onlyIf(this::shouldReclaimDockerSpace); + task.executable("bash"); + task.args("-c", + project.getRootDir() + .toPath() + .resolve(".github/scripts/reclaim-docker-diskspace.sh") + .toAbsolutePath()); + }); + } + + private boolean shouldReclaimDockerSpace(Task task) { + if (System.getProperty("os.name").startsWith("Windows")) { + return false; + } + return System.getenv("GITHUB_ACTIONS") != null || System.getenv("RECLAIM_DOCKER_SPACE") != null; + } + }