From 159c6af0dd1f16a842fc1570b3964d528b8f98b5 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 14 Aug 2025 15:46:52 +0000 Subject: [PATCH] chore: refactor NewDockerVolumeRemoveExecutor (#854) - remove the force argument as it is always false - accept more than one volume to be removed - other - [PR](https://code.forgejo.org/forgejo/runner/pulls/854): chore: refactor NewDockerVolumeRemoveExecutor Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/854 Reviewed-by: Mathieu Fenniak Co-authored-by: Earl Warren Co-committed-by: Earl Warren --- act/container/docker_volume.go | 13 ++++++++----- act/runner/run_context.go | 3 +-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/act/container/docker_volume.go b/act/container/docker_volume.go index 94e4036b..d6c9a348 100644 --- a/act/container/docker_volume.go +++ b/act/container/docker_volume.go @@ -4,13 +4,14 @@ package container import ( "context" + "slices" "code.forgejo.org/forgejo/runner/v9/act/common" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/volume" ) -func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executor { +func NewDockerVolumesRemoveExecutor(volumeNames []string) common.Executor { return func(ctx context.Context) error { cli, err := GetDockerClient(ctx) if err != nil { @@ -24,17 +25,18 @@ func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executo } for _, vol := range list.Volumes { - if vol.Name == volumeName { - return removeExecutor(volumeName, force)(ctx) + if slices.Contains(volumeNames, vol.Name) { + if err := removeExecutor(vol.Name)(ctx); err != nil { + return err + } } } - // Volume not found - do nothing return nil } } -func removeExecutor(volume string, force bool) common.Executor { +func removeExecutor(volume string) common.Executor { return func(ctx context.Context) error { logger := common.Logger(ctx) logger.Debugf("%sdocker volume rm %s", logPrefix, volume) @@ -49,6 +51,7 @@ func removeExecutor(volume string, force bool) common.Executor { } defer cli.Close() + force := false return cli.VolumeRemove(ctx, volume, force) } } diff --git a/act/runner/run_context.go b/act/runner/run_context.go index 533de691..15efc312 100644 --- a/act/runner/run_context.go +++ b/act/runner/run_context.go @@ -521,8 +521,7 @@ func (rc *RunContext) prepareJobContainer(ctx context.Context) error { if rc.JobContainer != nil { return rc.JobContainer.Remove().IfNot(reuseJobContainer). - Then(container.NewDockerVolumeRemoveExecutor(rc.jobContainerName(), false)).IfNot(reuseJobContainer). - Then(container.NewDockerVolumeRemoveExecutor(rc.jobContainerName()+"-env", false)).IfNot(reuseJobContainer). + Then(container.NewDockerVolumesRemoveExecutor([]string{rc.jobContainerName(), rc.jobContainerName() + "-env"})).IfNot(reuseJobContainer). Then(func(ctx context.Context) error { if len(rc.ServiceContainers) > 0 { logger.Infof("Cleaning up services for job %s", rc.JobName)