From def1d0a6f2cdaafed6a0e24b9cff79fbc3306e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE=20=28Jade=20Lin=29?= Date: Mon, 17 Jun 2024 23:26:04 +0800 Subject: [PATCH] Make the external url of cache server configurable if necessary --- cmd/input.go | 1 + cmd/root.go | 3 ++- pkg/artifactcache/handler.go | 18 ++++++++++++------ pkg/artifactcache/handler_test.go | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/input.go b/cmd/input.go index 59c1400..a748d75 100644 --- a/cmd/input.go +++ b/cmd/input.go @@ -46,6 +46,7 @@ type Input struct { artifactServerPort string noCacheServer bool cacheServerPath string + cacheServerAdvertiseURL string cacheServerAddr string cacheServerPort uint16 jsonLogger bool diff --git a/cmd/root.go b/cmd/root.go index 3eca040..814c5c9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -94,6 +94,7 @@ func Execute(ctx context.Context, version string) { rootCmd.PersistentFlags().BoolVarP(&input.noSkipCheckout, "no-skip-checkout", "", false, "Do not skip actions/checkout") rootCmd.PersistentFlags().BoolVarP(&input.noCacheServer, "no-cache-server", "", false, "Disable cache server") rootCmd.PersistentFlags().StringVarP(&input.cacheServerPath, "cache-server-path", "", filepath.Join(CacheHomeDir, "actcache"), "Defines the path where the cache server stores caches.") + rootCmd.PersistentFlags().StringVarP(&input.cacheServerAdvertiseURL, "cache-server-advertise-url", "", "", "Defines the URL for advertising the cache server behind a proxy. e.g.: https://act-cache-server.example.com") 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.") @@ -598,7 +599,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str var cacheHandler *artifactcache.Handler if !input.noCacheServer && envs[cacheURLKey] == "" { var err error - cacheHandler, err = artifactcache.StartHandler(input.cacheServerPath, input.cacheServerAddr, input.cacheServerPort, common.Logger(ctx)) + cacheHandler, err = artifactcache.StartHandler(input.cacheServerPath, input.cacheServerAdvertiseURL, input.cacheServerAddr, input.cacheServerPort, common.Logger(ctx)) if err != nil { return err } diff --git a/pkg/artifactcache/handler.go b/pkg/artifactcache/handler.go index 065c7dd..c7c3c27 100644 --- a/pkg/artifactcache/handler.go +++ b/pkg/artifactcache/handler.go @@ -38,10 +38,11 @@ type Handler struct { gcing atomic.Bool gcAt time.Time - outboundIP string + outboundIP string + advertiseURL string } -func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) { +func StartHandler(dir, advertiseURL, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) { h := &Handler{} if logger == nil { @@ -71,6 +72,8 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger } h.storage = storage + h.advertiseURL = advertiseURL + if outboundIP != "" { h.outboundIP = outboundIP } else if ip := common.GetOutboundIP(); ip == nil { @@ -111,10 +114,13 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger } func (h *Handler) ExternalURL() string { - // TODO: make the external url configurable if necessary - return fmt.Sprintf("http://%s:%d", - h.outboundIP, - h.listener.Addr().(*net.TCPAddr).Port) + if h.advertiseURL != "" { + return h.advertiseURL + } else { + return fmt.Sprintf("http://%s:%d", + h.outboundIP, + h.listener.Addr().(*net.TCPAddr).Port) + } } func (h *Handler) Close() error { diff --git a/pkg/artifactcache/handler_test.go b/pkg/artifactcache/handler_test.go index 252c420..cd0edf1 100644 --- a/pkg/artifactcache/handler_test.go +++ b/pkg/artifactcache/handler_test.go @@ -20,7 +20,7 @@ import ( func TestHandler(t *testing.T) { dir := filepath.Join(t.TempDir(), "artifactcache") - handler, err := StartHandler(dir, "", 0, nil) + handler, err := StartHandler(dir, "", "", 0, nil) require.NoError(t, err) base := fmt.Sprintf("%s%s", handler.ExternalURL(), urlBase) @@ -589,7 +589,7 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte func TestHandler_gcCache(t *testing.T) { dir := filepath.Join(t.TempDir(), "artifactcache") - handler, err := StartHandler(dir, "", 0, nil) + handler, err := StartHandler(dir, "", "", 0, nil) require.NoError(t, err) defer func() {