Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic artifact server port #2200

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Input struct {
autoRemove bool
artifactServerPath string
artifactServerAddr string
artifactServerPort string
artifactServerPort uint16
noCacheServer bool
cacheServerPath string
cacheServerAddr string
Expand Down
15 changes: 13 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func Execute(ctx context.Context, version string) {
rootCmd.PersistentFlags().StringVarP(&input.githubInstance, "github-instance", "", "github.com", "GitHub instance to use. Don't use this if you are not using GitHub Enterprise Server.")
rootCmd.PersistentFlags().StringVarP(&input.artifactServerPath, "artifact-server-path", "", "", "Defines the path where the artifact server stores uploads and retrieves downloads from. If not specified the artifact server will not start.")
rootCmd.PersistentFlags().StringVarP(&input.artifactServerAddr, "artifact-server-addr", "", common.GetOutboundIP().String(), "Defines the address to which the artifact server binds.")
rootCmd.PersistentFlags().StringVarP(&input.artifactServerPort, "artifact-server-port", "", "34567", "Defines the port where the artifact server listens.")
rootCmd.PersistentFlags().Uint16VarP(&input.artifactServerPort, "artifact-server-port", "", 0, "Defines the port where the artifact server listens. 0 means a randomly available port.")
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.")
Expand Down Expand Up @@ -592,7 +592,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
return err
}

cancel := artifacts.Serve(ctx, input.artifactServerPath, input.artifactServerAddr, input.artifactServerPort)
cancel, artifactServer := artifacts.Serve(ctx, input.artifactServerPath, input.artifactServerAddr, input.artifactServerPort)

const cacheURLKey = "ACTIONS_CACHE_URL"
var cacheHandler *artifactcache.Handler
Expand All @@ -605,6 +605,17 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
envs[cacheURLKey] = cacheHandler.ExternalURL() + "/"
}

const actionsRuntimeURLKey = "ACTIONS_RUNTIME_URL"
if !input.noCacheServer && envs[actionsRuntimeURLKey] == "" && artifactServer != nil {
envs[actionsRuntimeURLKey] = fmt.Sprintf("http://%s/", artifactServer.Addr)
}
const actionsRuntimeTokenKey = "ACTIONS_RUNTIME_TOKEN"
actionsRuntimeToken := os.Getenv(actionsRuntimeTokenKey)
if !input.noCacheServer && actionsRuntimeToken == "" && artifactServer != nil {
actionsRuntimeToken = "token"
}
envs[actionsRuntimeTokenKey] = actionsRuntimeToken

ctx = common.WithDryrun(ctx, input.dryrun)
if watch, err := cmd.Flags().GetBool("watch"); err != nil {
return err
Expand Down
14 changes: 8 additions & 6 deletions pkg/artifacts/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/fs"
"net"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -275,12 +276,12 @@ func downloads(router *httprouter.Router, baseDir string, fsys fs.FS) {
})
}

func Serve(ctx context.Context, artifactPath string, addr string, port string) context.CancelFunc {
func Serve(ctx context.Context, artifactPath string, addr string, port uint16) (context.CancelFunc, *http.Server) {
serverContext, cancel := context.WithCancel(ctx)
logger := common.Logger(serverContext)

if artifactPath == "" {
return cancel
return cancel, nil
}

router := httprouter.New()
Expand All @@ -290,16 +291,17 @@ func Serve(ctx context.Context, artifactPath string, addr string, port string) c
uploads(router, artifactPath, fsys)
downloads(router, artifactPath, fsys)

listener, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port))
server := &http.Server{
Addr: fmt.Sprintf("%s:%s", addr, port),
Addr: listener.Addr().String(),
ReadHeaderTimeout: 2 * time.Second,
Handler: router,
}

// run server
go func() {
logger.Infof("Start server on http://%s:%s", addr, port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Debugf("Start Artifact Server on http://%s", listener.Addr())
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
logger.Fatal(err)
}
}()
Expand All @@ -314,5 +316,5 @@ func Serve(ctx context.Context, artifactPath string, addr string, port string) c
}
}()

return cancel
return cancel, server
}
8 changes: 4 additions & 4 deletions pkg/artifacts/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ type TestJobFileInfo struct {
}

var (
artifactsPath = path.Join(os.TempDir(), "test-artifacts")
artifactsAddr = "127.0.0.1"
artifactsPort = "12345"
artifactsPath = path.Join(os.TempDir(), "test-artifacts")
artifactsAddr = "127.0.0.1"
artifactsPort uint16 = 12345
)

func TestArtifactFlow(t *testing.T) {
Expand All @@ -252,7 +252,7 @@ func TestArtifactFlow(t *testing.T) {

ctx := context.Background()

cancel := Serve(ctx, artifactsPath, artifactsAddr, artifactsPort)
cancel, _ := Serve(ctx, artifactsPath, artifactsAddr, artifactsPort)
defer cancel()

platforms := map[string]string{
Expand Down
18 changes: 0 additions & 18 deletions pkg/runner/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,6 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon
env["GITHUB_API_URL"] = github.APIURL
env["GITHUB_GRAPHQL_URL"] = github.GraphQLURL

if rc.Config.ArtifactServerPath != "" {
setActionRuntimeVars(rc, env)
}

for _, platformName := range rc.runsOnPlatformNames(ctx) {
if platformName != "" {
if platformName == "ubuntu-latest" {
Expand All @@ -955,20 +951,6 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon
return env
}

func setActionRuntimeVars(rc *RunContext, env map[string]string) {
actionsRuntimeURL := os.Getenv("ACTIONS_RUNTIME_URL")
if actionsRuntimeURL == "" {
actionsRuntimeURL = fmt.Sprintf("http://%s:%s/", rc.Config.ArtifactServerAddr, rc.Config.ArtifactServerPort)
}
env["ACTIONS_RUNTIME_URL"] = actionsRuntimeURL

actionsRuntimeToken := os.Getenv("ACTIONS_RUNTIME_TOKEN")
if actionsRuntimeToken == "" {
actionsRuntimeToken = "token"
}
env["ACTIONS_RUNTIME_TOKEN"] = actionsRuntimeToken
}

func (rc *RunContext) handleCredentials(ctx context.Context) (string, string, error) {
// TODO: remove below 2 lines when we can release act with breaking changes
username := rc.Config.Secrets["DOCKER_USERNAME"]
Expand Down
2 changes: 1 addition & 1 deletion pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Config struct {
AutoRemove bool // controls if the container is automatically removed upon workflow completion
ArtifactServerPath string // the path where the artifact server stores uploads
ArtifactServerAddr string // the address the artifact server binds to
ArtifactServerPort string // the port the artifact server binds to
ArtifactServerPort uint16 // the port the artifact server binds to
NoSkipCheckout bool // do not skip actions/checkout
RemoteName string // remote name in local git repo config
ReplaceGheActionWithGithubCom []string // Use actions from GitHub Enterprise instance to GitHub
Expand Down
Loading