Skip to content

Commit

Permalink
Add --network flag; default to host if not using service containers o…
Browse files Browse the repository at this point in the history
…r set explicitly
  • Loading branch information
GuessWhoSamFoo committed Aug 12, 2023
1 parent b312499 commit 7a29ed9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Input struct {
matrix []string
actionCachePath string
logPrefixJobID bool
networkName string
}

func (i *Input) resolve(path string) string {
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/AlecAivazis/survey/v2"
"github.com/adrg/xdg"
"github.com/andreaskoch/go-fswatch"
docker_container "github.com/docker/docker/api/types/container"
"github.com/joho/godotenv"
gitignore "github.com/sabhiram/go-gitignore"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -96,6 +97,7 @@ func Execute(ctx context.Context, version string) {
rootCmd.PersistentFlags().StringVarP(&input.cacheServerAddr, "cache-server-addr", "", common.GetOutboundIP().String(), "Defines the address to which the cache server binds.")
rootCmd.PersistentFlags().Uint16VarP(&input.cacheServerPort, "cache-server-port", "", 0, "Defines the port where the artifact server listens. 0 means a randomly available port.")
rootCmd.PersistentFlags().StringVarP(&input.actionCachePath, "action-cache-path", "", filepath.Join(CacheHomeDir, "act"), "Defines the path where the actions get cached and host workspaces created.")
rootCmd.PersistentFlags().StringVarP(&input.networkName, "network", "", "host", "Sets a docker network name. Defaults to host.")
rootCmd.SetArgs(args())

if err := rootCmd.Execute(); err != nil {
Expand Down Expand Up @@ -612,6 +614,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
ReplaceGheActionWithGithubCom: input.replaceGheActionWithGithubCom,
ReplaceGheActionTokenWithGithubCom: input.replaceGheActionTokenWithGithubCom,
Matrix: matrixes,
ContainerNetworkMode: docker_container.NetworkMode(input.networkName),
}
r, err := runner.New(config)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/artifacts/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ func runTestJobFile(ctx context.Context, t *testing.T, tjfi TestJobFileInfo) {
ArtifactServerPath: artifactsPath,
ArtifactServerAddr: artifactsAddr,
ArtifactServerPort: artifactsPort,
ContainerNetworkMode: "host",
}

runner, err := runner.New(runnerConfig)
Expand Down
25 changes: 14 additions & 11 deletions pkg/runner/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ func (rc *RunContext) jobContainerName() string {
}

// networkName return the name of the network which will be created by `act` automatically for job,
// only create network if `rc.Config.ContainerNetworkMode` is empty string.
// only create network if using a service container
func (rc *RunContext) networkName() string {
return fmt.Sprintf("%s-network", rc.jobContainerName())
if rc.Config.ContainerNetworkMode == "" {
return "host"
}
if len(rc.Run.Job().Services) > 0 {
return fmt.Sprintf("%s-%s-network", rc.jobContainerName(), rc.Run.JobID)
}
return string(rc.Config.ContainerNetworkMode)

Check warning on line 99 in pkg/runner/run_context.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L96-L99

Added lines #L96 - L99 were not covered by tests
}

func getDockerDaemonSocketMountPath(daemonPath string) string {
Expand Down Expand Up @@ -266,12 +272,9 @@ func (rc *RunContext) startJobContainer() common.Executor {
binds, mounts := rc.GetBindsAndMounts()

// specify the network to which the container will connect when `docker create` stage. (like execute command line: docker create --network <networkName> <image>)
networkName := string(rc.Config.ContainerNetworkMode)
if networkName == "" {
// if networkName is empty string, will create a new network for the containers.
// and it will be removed after at last.
networkName = rc.networkName()
}
// if using service containers, will create a new network for the containers.
// and it will be removed after at last.
networkName := rc.networkName()

// add service containers
for serviceID, spec := range rc.Run.Job().Services {
Expand Down Expand Up @@ -317,9 +320,9 @@ func (rc *RunContext) startJobContainer() common.Executor {
if err := rc.stopServiceContainers()(ctx); err != nil {
logger.Errorf("Error while cleaning services: %v", err)
}

Check warning on line 322 in pkg/runner/run_context.go

View check run for this annotation

Codecov / codecov/patch

pkg/runner/run_context.go#L321-L322

Added lines #L321 - L322 were not covered by tests
if !rc.IsHostEnv(ctx) && rc.Config.ContainerNetworkMode == "" {
if !rc.IsHostEnv(ctx) {
// clean network in docker mode only
// if the value of `ContainerNetworkMode` is empty string,
// if using service containers
// it means that the network to which containers are connecting is created by `act_runner`,
// so, we should remove the network at last.
logger.Infof("Cleaning up network for job %s, and network name is: %s", rc.JobName, rc.networkName())
Expand Down Expand Up @@ -363,7 +366,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
return common.NewPipelineExecutor(
rc.pullServicesImages(rc.Config.ForcePull),
rc.JobContainer.Pull(rc.Config.ForcePull),
container.NewDockerNetworkCreateExecutor(networkName).IfBool(!rc.IsHostEnv(ctx) && rc.Config.ContainerNetworkMode == ""), // if the value of `ContainerNetworkMode` is empty string, then will create a new network for containers.
container.NewDockerNetworkCreateExecutor(networkName).IfBool(!rc.IsHostEnv(ctx) && networkName != "host"),
rc.startServiceContainers(networkName),
rc.JobContainer.Create(rc.Config.ContainerCapAdd, rc.Config.ContainerCapDrop),
rc.JobContainer.Start(false),
Expand Down

0 comments on commit 7a29ed9

Please sign in to comment.