Skip to content

Commit

Permalink
Reclaim docker disk space on CI during build
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Oct 18, 2024
1 parent 3481107 commit a45844e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
29 changes: 29 additions & 0 deletions .github/scripts/reclaim-docker-diskspace.sh
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

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;
Expand All @@ -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) {
Expand All @@ -73,6 +77,8 @@ private void configureDockerTesting(Project 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) {
Expand Down Expand Up @@ -110,4 +116,28 @@ private Provider<Test> createTestTask(Project project, SourceSet dockerTestSourc
});
}

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

}

0 comments on commit a45844e

Please sign in to comment.