mirror of
https://github.com/juanfont/headscale.git
synced 2025-11-29 05:18:48 -05:00
cmd/hi: improve test cleanup to reduce CI disk usage (#2881)
This commit is contained in:
@@ -1,17 +1,25 @@
|
||||
package dockertestutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RunDockerBuildForDiagnostics runs docker build manually to get detailed error output.
|
||||
// This is used when a docker build fails to provide more detailed diagnostic information
|
||||
// than what dockertest typically provides.
|
||||
func RunDockerBuildForDiagnostics(contextDir, dockerfile string) string {
|
||||
cmd := exec.Command("docker", "build", "-f", dockerfile, contextDir)
|
||||
//
|
||||
// Returns the build output regardless of success/failure, and an error if the build failed.
|
||||
func RunDockerBuildForDiagnostics(contextDir, dockerfile string) (string, error) {
|
||||
// Use a context with timeout to prevent hanging builds
|
||||
const buildTimeout = 10 * time.Minute
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), buildTimeout)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "docker", "build", "--progress=plain", "--no-cache", "-f", dockerfile, contextDir)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return string(output)
|
||||
}
|
||||
return ""
|
||||
|
||||
return string(output), err
|
||||
}
|
||||
|
||||
@@ -108,6 +108,8 @@ func CleanUnreferencedNetworks(pool *dockertest.Pool) error {
|
||||
}
|
||||
|
||||
// CleanImagesInCI removes images if running in CI.
|
||||
// It only removes dangling (untagged) images to avoid forcing rebuilds.
|
||||
// Tagged images (golang:*, tailscale/tailscale:*, etc.) are automatically preserved.
|
||||
func CleanImagesInCI(pool *dockertest.Pool) error {
|
||||
if !util.IsCI() {
|
||||
log.Println("Skipping image cleanup outside of CI")
|
||||
@@ -119,9 +121,26 @@ func CleanImagesInCI(pool *dockertest.Pool) error {
|
||||
return fmt.Errorf("getting images: %w", err)
|
||||
}
|
||||
|
||||
removedCount := 0
|
||||
for _, image := range images {
|
||||
log.Printf("removing image: %s, %v", image.ID, image.RepoTags)
|
||||
_ = pool.Client.RemoveImage(image.ID)
|
||||
// Only remove dangling (untagged) images to avoid forcing rebuilds
|
||||
// Dangling images have no RepoTags or only have "<none>:<none>"
|
||||
if len(image.RepoTags) == 0 || (len(image.RepoTags) == 1 && image.RepoTags[0] == "<none>:<none>") {
|
||||
log.Printf("Removing dangling image: %s", image.ID[:12])
|
||||
|
||||
err := pool.Client.RemoveImage(image.ID)
|
||||
if err != nil {
|
||||
log.Printf("Warning: failed to remove image %s: %v", image.ID[:12], err)
|
||||
} else {
|
||||
removedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if removedCount > 0 {
|
||||
log.Printf("Removed %d dangling images in CI", removedCount)
|
||||
} else {
|
||||
log.Println("No dangling images to remove in CI")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user