diff options
author | Jason Song <wolfogre@noreply.gitea.io> | 2022-11-24 07:45:32 +0100 |
---|---|---|
committer | Jason Song <wolfogre@noreply.gitea.io> | 2022-11-24 07:45:32 +0100 |
commit | 4cacc14d22447358f1d34b4ef556fe1fd9cb9dd0 (patch) | |
tree | de31d61de5efb14efdfc609e94a6074c104a044b | |
parent | feat: support PlatformPicker (diff) | |
download | forgejo-act-0.233.5.tar.xz forgejo-act-0.233.5.zip |
feat: adjust container name format (#1)v0.233.5
Co-authored-by: Jason Song <i@wolfogre.com>
Reviewed-on: https://gitea.com/gitea/act/pulls/1
-rw-r--r-- | pkg/runner/action.go | 2 | ||||
-rw-r--r-- | pkg/runner/run_context.go | 19 | ||||
-rw-r--r-- | pkg/runner/run_context_test.go | 21 | ||||
-rw-r--r-- | pkg/runner/step_docker.go | 2 |
4 files changed, 41 insertions, 3 deletions
diff --git a/pkg/runner/action.go b/pkg/runner/action.go index 275de09..bae766e 100644 --- a/pkg/runner/action.go +++ b/pkg/runner/action.go @@ -356,7 +356,7 @@ func newStepContainer(ctx context.Context, step step, image string, cmd []string Image: image, Username: rc.Config.Secrets["DOCKER_USERNAME"], Password: rc.Config.Secrets["DOCKER_PASSWORD"], - Name: createContainerName(rc.jobContainerName(), stepModel.ID), + Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+stepModel.ID), Env: envList, Mounts: mounts, NetworkMode: fmt.Sprintf("container:%s", rc.jobContainerName()), diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index 4b42cd1..ee7aaf7 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -67,7 +67,7 @@ func (rc *RunContext) GetEnv() map[string]string { } func (rc *RunContext) jobContainerName() string { - return createContainerName(rc.Config.ContainerNamePrefix, rc.String()) + return createSimpleContainerName(rc.Config.ContainerNamePrefix, "WORKFLOW-"+rc.Run.Workflow.Name, "JOB-"+rc.Name) } // Returns the binds and mounts for the container, resolving paths as appopriate @@ -367,6 +367,7 @@ func mergeMaps(maps ...map[string]string) map[string]string { return rtnMap } +// deprecated: use createSimpleContainerName func createContainerName(parts ...string) string { name := make([]string, 0) pattern := regexp.MustCompile("[^a-zA-Z0-9]") @@ -390,6 +391,22 @@ func createContainerName(parts ...string) string { return strings.ReplaceAll(strings.Trim(strings.Join(name, "-"), "-"), "--", "-") } +func createSimpleContainerName(parts ...string) string { + pattern := regexp.MustCompile("[^a-zA-Z0-9-]") + name := make([]string, 0, len(parts)) + for _, v := range parts { + v = pattern.ReplaceAllString(v, "-") + v = strings.Trim(v, "-") + for strings.Contains(v, "--") { + v = strings.ReplaceAll(v, "--", "-") + } + if v != "" { + name = append(name, v) + } + } + return strings.Join(name, "_") +} + func trimToLen(s string, l int) string { if l < 0 { l = 0 diff --git a/pkg/runner/run_context_test.go b/pkg/runner/run_context_test.go index cdafb8e..4d68dc8 100644 --- a/pkg/runner/run_context_test.go +++ b/pkg/runner/run_context_test.go @@ -623,3 +623,24 @@ func TestRunContextGetEnv(t *testing.T) { }) } } + +func Test_createSimpleContainerName(t *testing.T) { + tests := []struct { + parts []string + want string + }{ + { + parts: []string{"a--a", "BBæ£", "c-C"}, + want: "a-a_BB_c-C", + }, + { + parts: []string{"a-a", "", "-"}, + want: "a-a", + }, + } + for _, tt := range tests { + t.Run(strings.Join(tt.parts, " "), func(t *testing.T) { + assert.Equalf(t, tt.want, createSimpleContainerName(tt.parts...), "createSimpleContainerName(%v)", tt.parts) + }) + } +} diff --git a/pkg/runner/step_docker.go b/pkg/runner/step_docker.go index b51da53..4010b55 100644 --- a/pkg/runner/step_docker.go +++ b/pkg/runner/step_docker.go @@ -120,7 +120,7 @@ func (sd *stepDocker) newStepContainer(ctx context.Context, image string, cmd [] Image: image, Username: rc.Config.Secrets["DOCKER_USERNAME"], Password: rc.Config.Secrets["DOCKER_PASSWORD"], - Name: createContainerName(rc.jobContainerName(), step.ID), + Name: createSimpleContainerName(rc.jobContainerName(), "STEP-"+step.ID), Env: envList, Mounts: mounts, NetworkMode: fmt.Sprintf("container:%s", rc.jobContainerName()), |