From 0c1f2edb996a87ee17dcf3cfa7259c04be02abd7 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Sun, 23 Apr 2023 14:55:17 +0800 Subject: [PATCH] Support specifying command for `services` (#50) This PR is to support overwriting the default `CMD` command of `services` containers. This is a Gitea specific feature and GitHub Actions doesn't support this syntax. Reviewed-on: https://gitea.com/gitea/act/pulls/50 Reviewed-by: Jason Song Co-authored-by: Zettat123 Co-committed-by: Zettat123 --- pkg/jobparser/model.go | 1 + pkg/model/workflow.go | 3 +++ pkg/runner/run_context.go | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/pkg/jobparser/model.go b/pkg/jobparser/model.go index 3236704..d68545b 100644 --- a/pkg/jobparser/model.go +++ b/pkg/jobparser/model.go @@ -168,6 +168,7 @@ type ContainerSpec struct { Volumes []string `yaml:"volumes,omitempty"` Options string `yaml:"options,omitempty"` Credentials map[string]string `yaml:"credentials,omitempty"` + Cmd []string `yaml:"cmd,omitempty"` } type Strategy struct { diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index a77a688..d39c405 100644 --- a/pkg/model/workflow.go +++ b/pkg/model/workflow.go @@ -522,6 +522,9 @@ type ContainerSpec struct { Args string Name string Reuse bool + + // Gitea specific + Cmd []string `yaml:"cmd"` } // Step is the structure of one step in a job diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index e7f9e2b..64d4f20 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -246,6 +246,7 @@ func (rc *RunContext) startJobContainer() common.Executor { // add service containers for name, spec := range rc.Run.Job().Services { + // interpolate env interpolatedEnvs := make(map[string]string, len(spec.Env)) for k, v := range spec.Env { interpolatedEnvs[k] = rc.ExprEval.Interpolate(ctx, v) @@ -254,6 +255,11 @@ func (rc *RunContext) startJobContainer() common.Executor { for k, v := range interpolatedEnvs { envs = append(envs, fmt.Sprintf("%s=%s", k, v)) } + // interpolate cmd + interpolatedCmd := make([]string, 0, len(spec.Cmd)) + for _, v := range spec.Cmd { + interpolatedCmd = append(interpolatedCmd, rc.ExprEval.Interpolate(ctx, v)) + } serviceContainerName := createSimpleContainerName(rc.jobContainerName(), name) c := container.NewContainer(&container.NewContainerInput{ Name: serviceContainerName, @@ -261,6 +267,7 @@ func (rc *RunContext) startJobContainer() common.Executor { Image: spec.Image, Username: username, Password: password, + Cmd: interpolatedCmd, Env: envs, Mounts: map[string]string{ // TODO merge volumes