diff --git a/internal/app/cmd/cache-server.go b/internal/app/cmd/cache-server.go index 21b3352..406396c 100644 --- a/internal/app/cmd/cache-server.go +++ b/internal/app/cmd/cache-server.go @@ -24,11 +24,12 @@ type cacheServerArgs struct { func runCacheServer(ctx context.Context, configFile *string, cacheArgs *cacheServerArgs) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { + fmt.Println("cache1") cfg, err := config.LoadDefault(*configFile) if err != nil { return fmt.Errorf("invalid configuration: %w", err) } - + fmt.Println("cache2") initLogging(cfg) var ( @@ -47,7 +48,7 @@ func runCacheServer(ctx context.Context, configFile *string, cacheArgs *cacheSer if cacheArgs.Port != 0 { port = cacheArgs.Port } - + fmt.Println("cache2") cacheHandler, err := artifactcache.StartHandler( dir, host, diff --git a/internal/app/cmd/cmd.go b/internal/app/cmd/cmd.go index 7c8e9a4..e3015a7 100644 --- a/internal/app/cmd/cmd.go +++ b/internal/app/cmd/cmd.go @@ -15,6 +15,8 @@ import ( ) func Execute(ctx context.Context) { + configFile := "" + // ./act_runner rootCmd := &cobra.Command{ Use: "act_runner [event name to run]\nIf no event name passed, will default to \"on: push\"", @@ -22,8 +24,16 @@ func Execute(ctx context.Context) { Args: cobra.MaximumNArgs(1), Version: ver.Version(), SilenceUsage: true, + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + // Auto-detect config.yaml if not specified + if configFile == "" { + if _, err := os.Stat("config.yaml"); err == nil { + configFile = "config.yaml" + } + } + return nil + }, } - configFile := "" rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Config file path") // ./act_runner register diff --git a/internal/app/run/runner.go b/internal/app/run/runner.go index 4b0f76b..eb555ac 100644 --- a/internal/app/run/runner.go +++ b/internal/app/run/runner.go @@ -7,6 +7,7 @@ import ( "context" "encoding/json" "fmt" + "net/url" "path/filepath" "strings" "sync" @@ -38,6 +39,8 @@ type Runner struct { labels labels.Labels envs map[string]string + useHostDockerInternal bool + runningTasks sync.Map } @@ -52,9 +55,14 @@ func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client) for k, v := range cfg.Runner.Envs { envs[k] = v } + // Setup cache + useHostDockerInternal := false if cfg.Cache.Enabled == nil || *cfg.Cache.Enabled { if cfg.Cache.ExternalServer != "" { envs["ACTIONS_CACHE_URL"] = cfg.Cache.ExternalServer + if strings.Contains(cfg.Cache.ExternalServer, "host.docker.internal") { + useHostDockerInternal = true + } } else { cacheHandler, err := artifactcache.StartHandler( cfg.Cache.Dir, @@ -66,7 +74,16 @@ func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client) log.Errorf("cannot init cache server, it will be disabled: %v", err) // go on } else { - envs["ACTIONS_CACHE_URL"] = cacheHandler.ExternalURL() + "/" + // Use host.docker.internal for container access + externalURL := cacheHandler.ExternalURL() + if parsedURL, err := url.Parse(externalURL); err == nil { + parsedURL.Host = "host.docker.internal:" + parsedURL.Port() + envs["ACTIONS_CACHE_URL"] = parsedURL.String() + "/" + } else { + envs["ACTIONS_CACHE_URL"] = externalURL + "/" + } + useHostDockerInternal = true + log.Infof("Cache server started at %s, using host.docker.internal for container access", externalURL) } } } @@ -81,11 +98,12 @@ func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client) envs["GITEA_ACTIONS_RUNNER_VERSION"] = ver.Version() return &Runner{ - name: reg.Name, - cfg: cfg, - client: cli, - labels: ls, - envs: envs, + name: reg.Name, + cfg: cfg, + client: cli, + labels: ls, + envs: envs, + useHostDockerInternal: useHostDockerInternal, } } @@ -197,6 +215,17 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report. maxLifetime = time.Until(deadline) } + // Auto-add host.docker.internal if cache is enabled + containerOptions := r.cfg.Container.Options + if r.useHostDockerInternal { + if containerOptions == "" { + containerOptions = "--add-host=host.docker.internal:host-gateway" + } else if !strings.Contains(containerOptions, "--add-host=host.docker.internal") { + containerOptions += " --add-host=host.docker.internal:host-gateway" + } + log.Infof("Auto-added host.docker.internal mapping for cache") + } + runnerConfig := &runner.Config{ // On Linux, Workdir will be like "///" // On Windows, Workdir will be like "\\\" @@ -219,7 +248,7 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report. ContainerNamePrefix: fmt.Sprintf("GITEA-ACTIONS-TASK-%d", task.Id), ContainerMaxLifetime: maxLifetime, ContainerNetworkMode: container.NetworkMode(r.cfg.Container.Network), - ContainerOptions: r.cfg.Container.Options, + ContainerOptions: containerOptions, ContainerDaemonSocket: r.cfg.Container.DockerHost, Privileged: r.cfg.Container.Privileged, DefaultActionInstance: r.getDefaultActionsURL(ctx, task),