Skip to content

Commit 354d2c8

Browse files
committed
Implement option for automatic pruning of images
Closes #21.
1 parent cd57ad5 commit 354d2c8

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

tools/ddct-src/Util/ContainerBuilder.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
final class ContainerBuilder
1818
{
19+
private readonly bool $autoPruneImages;
20+
1921
public function __construct(
2022
private readonly ContainerEngine $containerEngine,
2123
private readonly ContainerFileMap $map,
2224
private readonly Tagger $tagger,
25+
?bool $autoPruneImages = null,
2326
) {
27+
$this->autoPruneImages = $autoPruneImages ?? self::determineAutoPruneImages();
2428
}
2529

2630
private function buildContainerImpl(ContainerFileRecipe $recipe, BaseImage $baseImage): ContainerBuilderStatus
@@ -59,6 +63,8 @@ private function buildContainer(ContainerFileRecipe $recipe, BaseImage $baseImag
5963
writeln('--> Updating tags.');
6064
$this->tagger->applyAll();
6165

66+
$this->pruneImagesIfEnabled();
67+
6268
return $result;
6369
}
6470

@@ -144,4 +150,32 @@ public function hasBuiltExact(string $app, ContainerVersionTag $version): ?Conta
144150

145151
return null;
146152
}
153+
154+
private function pruneImagesIfEnabled(): void
155+
{
156+
if (!$this->autoPruneImages) {
157+
return;
158+
}
159+
160+
writeln('--> Pruning images.');
161+
$this->containerEngine->pruneImages();
162+
}
163+
164+
public static function determineAutoPruneImages(): bool
165+
{
166+
if (!isset($_SERVER['AUTO_PRUNE_IMAGES'])) {
167+
return false;
168+
}
169+
170+
$value = filter_var(
171+
$_SERVER['AUTO_PRUNE_IMAGES'],
172+
FILTER_VALIDATE_BOOL,
173+
FILTER_NULL_ON_FAILURE,
174+
);
175+
if ($value === null) {
176+
throw new Exception('Unsupported value for environment variable `AUTO_PRUNE_IMAGES`.');
177+
}
178+
179+
return $value;
180+
}
147181
}

tools/ddct-src/Util/ContainerEngine.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public function removeImages(bool $force, string ...$images): void
159159
}
160160
}
161161

162+
public function pruneImages(): void
163+
{
164+
$this->passthruCommand('image', 'prune', '--force');
165+
}
166+
162167
public function pushImage(string $name): void
163168
{
164169
$this->passthruCommand('push', $name);

0 commit comments

Comments
 (0)