|
16 | 16 |
|
17 | 17 | final class ContainerBuilder |
18 | 18 | { |
| 19 | + private readonly bool $autoPruneImages; |
| 20 | + |
19 | 21 | public function __construct( |
20 | 22 | private readonly ContainerEngine $containerEngine, |
21 | 23 | private readonly ContainerFileMap $map, |
22 | 24 | private readonly Tagger $tagger, |
| 25 | + ?bool $autoPruneImages = null, |
23 | 26 | ) { |
| 27 | + $this->autoPruneImages = $autoPruneImages ?? self::determineAutoPruneImages(); |
24 | 28 | } |
25 | 29 |
|
26 | 30 | private function buildContainerImpl(ContainerFileRecipe $recipe, BaseImage $baseImage): ContainerBuilderStatus |
@@ -59,6 +63,8 @@ private function buildContainer(ContainerFileRecipe $recipe, BaseImage $baseImag |
59 | 63 | writeln('--> Updating tags.'); |
60 | 64 | $this->tagger->applyAll(); |
61 | 65 |
|
| 66 | + $this->pruneImagesIfEnabled(); |
| 67 | + |
62 | 68 | return $result; |
63 | 69 | } |
64 | 70 |
|
@@ -144,4 +150,32 @@ public function hasBuiltExact(string $app, ContainerVersionTag $version): ?Conta |
144 | 150 |
|
145 | 151 | return null; |
146 | 152 | } |
| 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 | + } |
147 | 181 | } |
0 commit comments