Fix container volumes being reused sometimes (#283)

This commit is contained in:
Stan Wohlwend 2020-06-18 17:21:55 +02:00 committed by GitHub
parent f5e1bd45b3
commit d4e41a90a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 10 deletions

View File

@ -3,11 +3,34 @@ package container
import ( import (
"context" "context"
"github.com/docker/docker/api/types/filters"
"github.com/nektos/act/pkg/common" "github.com/nektos/act/pkg/common"
) )
// NewDockerVolumeRemoveExecutor function
func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor { func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
return func(ctx context.Context) error {
cli, err := GetDockerClient(ctx)
if err != nil {
return err
}
list, err := cli.VolumeList(ctx, filters.NewArgs())
if err != nil {
return err
}
for _, vol := range list.Volumes {
if vol.Name == volume {
return removeExecutor(volume, force)(ctx)
}
}
// Volume not found - do nothing
return nil
}
}
func removeExecutor(volume string, force bool) common.Executor {
return func(ctx context.Context) error { return func(ctx context.Context) error {
logger := common.Logger(ctx) logger := common.Logger(ctx)
logger.Debugf("%sdocker volume rm %s", logPrefix, volume) logger.Debugf("%sdocker volume rm %s", logPrefix, volume)
@ -23,5 +46,4 @@ func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
return cli.VolumeRemove(ctx, volume, force) return cli.VolumeRemove(ctx, volume, force)
} }
} }

View File

@ -83,7 +83,6 @@ func TestEvaluate(t *testing.T) {
{"matrix.os", "Linux", ""}, {"matrix.os", "Linux", ""},
{"matrix.foo", "bar", ""}, {"matrix.foo", "bar", ""},
{"env.key", "value", ""}, {"env.key", "value", ""},
} }
for _, table := range tables { for _, table := range tables {
@ -121,8 +120,8 @@ func TestInterpolate(t *testing.T) {
}, },
} }
ee := rc.NewExpressionEvaluator() ee := rc.NewExpressionEvaluator()
tables := []struct{ tables := []struct {
in string in string
out string out string
}{ }{
{" ${{1}} to ${{2}} ", " 1 to 2 "}, {" ${{1}} to ${{2}} ", " 1 to 2 "},

View File

@ -115,7 +115,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
return common.NewPipelineExecutor( return common.NewPipelineExecutor(
rc.JobContainer.Pull(rc.Config.ForcePull), rc.JobContainer.Pull(rc.Config.ForcePull),
rc.JobContainer.Remove().IfBool(!rc.Config.ReuseContainers), rc.stopJobContainer(),
rc.JobContainer.Create(), rc.JobContainer.Create(),
rc.JobContainer.Start(false), rc.JobContainer.Start(false),
rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+"/.").IfBool(copyWorkspace), rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+"/.").IfBool(copyWorkspace),
@ -136,6 +136,8 @@ func (rc *RunContext) execJobContainer(cmd []string, env map[string]string) comm
return rc.JobContainer.Exec(cmd, env)(ctx) return rc.JobContainer.Exec(cmd, env)(ctx)
} }
} }
// stopJobContainer removes the job container (if it exists) and its volume (if it exists) if !rc.Config.ReuseContainers
func (rc *RunContext) stopJobContainer() common.Executor { func (rc *RunContext) stopJobContainer() common.Executor {
return func(ctx context.Context) error { return func(ctx context.Context) error {
if rc.JobContainer != nil && !rc.Config.ReuseContainers { if rc.JobContainer != nil && !rc.Config.ReuseContainers {

View File

@ -16,8 +16,8 @@ func TestRunContext_EvalBool(t *testing.T) {
Workdir: ".", Workdir: ".",
}, },
Env: map[string]string{ Env: map[string]string{
"TRUE": "true", "TRUE": "true",
"FALSE": "false", "FALSE": "false",
"SOME_TEXT": "text", "SOME_TEXT": "text",
}, },
Run: &model.Run{ Run: &model.Run{
@ -52,8 +52,8 @@ func TestRunContext_EvalBool(t *testing.T) {
rc.ExprEval = rc.NewExpressionEvaluator() rc.ExprEval = rc.NewExpressionEvaluator()
tables := []struct { tables := []struct {
in string in string
out bool out bool
}{ }{
// The basic ones // The basic ones
{"true", true}, {"true", true},