From ca9b783491b4ea1c5d67dbffdb95f34fd4a22573 Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Wed, 3 May 2023 19:49:17 +0200 Subject: [PATCH] fix: don't allow `-self-hosted` mode as container image (#1783) * fix: don't allow `-self-hosted` mode as container image * fix: jobcontainer in hostmode platform * Update run_context.go --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- pkg/runner/run_context.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index e6f5dfc..acffb76 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -420,8 +420,9 @@ func (rc *RunContext) startContainer() common.Executor { } func (rc *RunContext) IsHostEnv(ctx context.Context) bool { - image := rc.platformImage(ctx) - return strings.EqualFold(image, "-self-hosted") + platform := rc.runsOnImage(ctx) + image := rc.containerImage(ctx) + return image == "" && strings.EqualFold(platform, "-self-hosted") } func (rc *RunContext) stopContainer() common.Executor { @@ -474,7 +475,7 @@ func (rc *RunContext) Executor() common.Executor { } } -func (rc *RunContext) platformImage(ctx context.Context) string { +func (rc *RunContext) containerImage(ctx context.Context) string { job := rc.Run.Job() c := job.Container() @@ -482,6 +483,12 @@ func (rc *RunContext) platformImage(ctx context.Context) string { return rc.ExprEval.Interpolate(ctx, c.Image) } + return "" +} + +func (rc *RunContext) runsOnImage(ctx context.Context) string { + job := rc.Run.Job() + if job.RunsOn() == nil { common.Logger(ctx).Errorf("'runs-on' key not defined in %s", rc.String()) } @@ -497,6 +504,14 @@ func (rc *RunContext) platformImage(ctx context.Context) string { return "" } +func (rc *RunContext) platformImage(ctx context.Context) string { + if containerImage := rc.containerImage(ctx); containerImage != "" { + return containerImage + } + + return rc.runsOnImage(ctx) +} + func (rc *RunContext) options(ctx context.Context) string { job := rc.Run.Job() c := job.Container()