control network by passing NeedCreate to act

This commit is contained in:
sillyguodong 2023-05-13 09:03:01 +08:00
parent d4fc49b9e7
commit cb973caba1
4 changed files with 11 additions and 13 deletions

View file

@ -13,7 +13,7 @@ import (
"strings"
"time"
docker_container "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/container"
"github.com/joho/godotenv"
"github.com/nektos/act/pkg/artifactcache"
"github.com/nektos/act/pkg/artifacts"
@ -356,7 +356,6 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command
log.Infof("cache handler listens on: %v", handler.ExternalURL())
execArgs.cacheHandler = handler
networkMode := docker_container.NetworkMode("bridge")
// run the plan
config := &runner.Config{
Workdir: execArgs.Workdir(),
@ -386,7 +385,7 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command
// EventJSON: string(eventJSON),
ContainerNamePrefix: fmt.Sprintf("GITEA-ACTIONS-TASK-%s", eventName),
ContainerMaxLifetime: maxLifetime,
ContainerNetworkMode: &networkMode,
ContainerNetworkMode: container.NetworkMode("bridge"),
DefaultActionInstance: execArgs.defaultActionsUrl,
PlatformPicker: func(_ []string) string {
return execArgs.image

View file

@ -13,7 +13,7 @@ import (
"time"
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
docker_container "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/container"
"github.com/nektos/act/pkg/artifactcache"
"github.com/nektos/act/pkg/common"
"github.com/nektos/act/pkg/model"
@ -171,7 +171,6 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
maxLifetime = time.Until(deadline)
}
networkMode := docker_container.NetworkMode(r.cfg.Container.NetworkMode)
runnerConfig := &runner.Config{
// On Linux, Workdir will be like "/<parent_directory>/<owner>/<repo>"
// On Windows, Workdir will be like "\<parent_directory>\<owner>\<repo>"
@ -192,12 +191,13 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
EventJSON: string(eventJSON),
ContainerNamePrefix: fmt.Sprintf("GITEA-ACTIONS-TASK-%d", task.Id),
ContainerMaxLifetime: maxLifetime,
ContainerNetworkMode: &networkMode,
ContainerNetworkMode: container.NetworkMode(r.cfg.Container.Network),
ContainerOptions: r.cfg.Container.Options,
Privileged: r.cfg.Container.Privileged,
DefaultActionInstance: taskContext["gitea_default_actions_url"].GetStringValue(),
PlatformPicker: r.labels.PickPlatform,
Vars: task.Vars,
NeedCreateNetwork: strings.TrimSpace(r.cfg.Container.Network) == "",
}
rr, err := runner.New(runnerConfig)

View file

@ -42,9 +42,10 @@ cache:
port: 0
container:
# Which network to use for the job containers. Could be bridge, host or the name of a custom network.
# More info: https://gitea.com/gitea/act/pulls/56
network_mode: bridge
# Specifies the network to which the container will connect.
# Could be host, bridge or the name of a custom network.
# If it's empty, act_runner will create a network automatically.
network:
# Whether to use privileged mode or not when launching task containers (privileged mode is required for Docker-in-Docker).
privileged: false
# And other options to be used when the container is started (eg, --add-host=my.gitea.url:host-gateway).

View file

@ -34,7 +34,8 @@ type Config struct {
Port uint16 `yaml:"port"`
} `yaml:"cache"`
Container struct {
NetworkMode string `yaml:"network_mode"`
// NetworkMode string `yaml:"network_mode"` // Legacy
Network string `yaml:"network"`
Privileged bool `yaml:"privileged"`
Options string `yaml:"options"`
WorkdirParent string `yaml:"workdir_parent"`
@ -92,9 +93,6 @@ func LoadDefault(file string) (*Config, error) {
cfg.Cache.Dir = filepath.Join(home, ".cache", "actcache")
}
}
if cfg.Container.NetworkMode == "" {
cfg.Container.NetworkMode = "bridge"
}
if cfg.Container.WorkdirParent == "" {
cfg.Container.WorkdirParent = "workspace"
}