Reclaim docker disk space on CI during build

Attempt to fix disk space issues by removing large docker images
after they have been used.

This commit backports commits from `3.4.x` that were applied to
test the changes.

Closes gh-42776
This commit is contained in:
Phillip Webb 2024-10-17 13:40:57 -07:00
parent 3481107ff7
commit a45844e7cd
2 changed files with 62 additions and 3 deletions

29
.github/scripts/reclaim-docker-diskspace.sh vendored Executable file
View File

@ -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" == "<none>" ]]; 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

View File

@ -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<Project> {
/**
* 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> {
});
project.getDependencies()
.add(dockerTestSourceSet.getRuntimeOnlyConfigurationName(), "org.junit.platform:junit-platform-launcher");
Provider<Exec> 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<Project> {
});
}
private Provider<Exec> createReclaimDockerSpaceTask(Project project,
Provider<DockerTestBuildService> 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;
}
}