From 1d92791718bcb426f2a95f90dc1baf0045b89eb2 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 18 Nov 2022 16:09:51 +0800 Subject: [PATCH] feat: support more options of containers --- act/container/docker_run.go | 3 +++ act/runner/action.go | 1 + act/runner/run_context.go | 6 ++++-- act/runner/runner.go | 2 ++ act/runner/step_docker.go | 1 + 5 files changed, 11 insertions(+), 2 deletions(-) diff --git a/act/container/docker_run.go b/act/container/docker_run.go index 896a269c..add0e2e3 100644 --- a/act/container/docker_run.go +++ b/act/container/docker_run.go @@ -57,6 +57,8 @@ type NewContainerInput struct { UsernsMode string Platform string Options string + + AutoRemove bool } // FileEntry is a file to copy to a container @@ -475,6 +477,7 @@ func (cr *containerReference) create(capAdd []string, capDrop []string) common.E NetworkMode: container.NetworkMode(input.NetworkMode), Privileged: input.Privileged, UsernsMode: container.UsernsMode(input.UsernsMode), + AutoRemove: input.AutoRemove, } logger.Debugf("Common container.HostConfig ==> %+v", hostConfig) diff --git a/act/runner/action.go b/act/runner/action.go index 2dee988c..275de097 100644 --- a/act/runner/action.go +++ b/act/runner/action.go @@ -366,6 +366,7 @@ func newStepContainer(ctx context.Context, step step, image string, cmd []string Privileged: rc.Config.Privileged, UsernsMode: rc.Config.UsernsMode, Platform: rc.Config.ContainerArchitecture, + AutoRemove: rc.Config.AutoRemove, }) return stepContainer } diff --git a/act/runner/run_context.go b/act/runner/run_context.go index 74469bb2..86e59a44 100644 --- a/act/runner/run_context.go +++ b/act/runner/run_context.go @@ -9,6 +9,7 @@ import ( "regexp" "runtime" "strings" + "time" "github.com/mitchellh/go-homedir" "github.com/opencontainers/selinux/go-selinux" @@ -150,7 +151,7 @@ func (rc *RunContext) startJobContainer() common.Executor { rc.JobContainer = container.NewContainer(&container.NewContainerInput{ Cmd: nil, - Entrypoint: []string{"/usr/bin/tail", "-f", "/dev/null"}, + Entrypoint: []string{"/bin/sleep", fmt.Sprint(rc.Config.ContainerMaxLifetime.Round(time.Second).Seconds())}, WorkingDir: rc.Config.ContainerWorkdir(), Image: image, Username: username, @@ -158,7 +159,7 @@ func (rc *RunContext) startJobContainer() common.Executor { Name: name, Env: envList, Mounts: mounts, - NetworkMode: "host", + NetworkMode: rc.Config.ContainerNetworkMode, Binds: binds, Stdout: logWriter, Stderr: logWriter, @@ -166,6 +167,7 @@ func (rc *RunContext) startJobContainer() common.Executor { UsernsMode: rc.Config.UsernsMode, Platform: rc.Config.ContainerArchitecture, Options: rc.options(ctx), + AutoRemove: rc.Config.AutoRemove, }) return common.NewPipelineExecutor( diff --git a/act/runner/runner.go b/act/runner/runner.go index b0b74ad7..c36fec40 100644 --- a/act/runner/runner.go +++ b/act/runner/runner.go @@ -59,6 +59,8 @@ type Config struct { PresetGitHubContext *model.GithubContext // the preset github context, overrides some fields like DefaultBranch, Env, Secrets etc. EventJSON string // the content of JSON file to use for event.json in containers, overrides EventPath ContainerNamePrefix string // the prefix of container name + ContainerMaxLifetime time.Duration // the max lifetime of job containers + ContainerNetworkMode string // the network mode of job containers DefaultActionInstance string // the default actions web site } diff --git a/act/runner/step_docker.go b/act/runner/step_docker.go index 4ac3b27f..b51da53c 100644 --- a/act/runner/step_docker.go +++ b/act/runner/step_docker.go @@ -130,6 +130,7 @@ func (sd *stepDocker) newStepContainer(ctx context.Context, image string, cmd [] Privileged: rc.Config.Privileged, UsernsMode: rc.Config.UsernsMode, Platform: rc.Config.ContainerArchitecture, + AutoRemove: rc.Config.AutoRemove, }) return stepContainer }