Reclaim docker disk space on CI during build
Attempt to fix disk space issues by removing large docker images after they have been used. See gh-42776
This commit is contained in:
17
.github/scripts/reclaim-docker-diskspace.sh
vendored
Executable file
17
.github/scripts/reclaim-docker-diskspace.sh
vendored
Executable file
@@ -0,0 +1,17 @@
|
||||
echo "Reclaiming Docker Disk Space"
|
||||
echo
|
||||
|
||||
docker image ls --format "{{.Size}} {{.ID}}" | 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/MB/000000000/' )
|
||||
if [ $size -gt 200000000 ]; then
|
||||
image=$( echo "$line" | cut -d' ' -f2 )
|
||||
docker image rm -f $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
|
||||
@@ -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,25 @@ 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::runningOnCi);
|
||||
task.executable("sh");
|
||||
task.args("-c",
|
||||
project.getRootDir()
|
||||
.toPath()
|
||||
.resolve(".github/scripts/reclaim-docker-diskspace.sh")
|
||||
.toAbsolutePath());
|
||||
});
|
||||
}
|
||||
|
||||
private boolean runningOnCi(Task task) {
|
||||
return System.getenv("GITHUB_ACTIONS") != null || System.getenv("RECLAIM_DOCKER_SPACE") != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user