From a197fea4ba6e14b9582b909c4becbef47041eb35 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 14 Aug 2025 10:46:28 +0200 Subject: [PATCH] feat: assign a unique random name to each run context If the run context has a parent, both share the same unique random name. A composite action does not have a run context of its own, it re-uses the run context of the job that calls it: this is when a parent is used and needed. There may be any level of parent / child relationship and ensureRandom name recursively look for the first parent with a non empty random. --- act/runner/run_context.go | 20 ++++++++++++++++++++ act/runner/run_context_test.go | 15 +++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/act/runner/run_context.go b/act/runner/run_context.go index c7760de3..a1db2867 100644 --- a/act/runner/run_context.go +++ b/act/runner/run_context.go @@ -52,6 +52,7 @@ type RunContext struct { Masks []string cleanUpJobContainer common.Executor caller *caller // job calling this RunContext (reusable workflows) + randomName string networkName string networkCreated bool } @@ -391,6 +392,25 @@ func (rc *RunContext) startHostEnvironment() common.Executor { } } +func (rc *RunContext) ensureRandomName(ctx context.Context) { + if rc.randomName == "" { + logger := common.Logger(ctx) + if rc.Parent != nil { + // composite actions inherit their run context from the parent job + rootRunContext := rc + for rootRunContext.Parent != nil { + rootRunContext = rootRunContext.Parent + } + rootRunContext.ensureRandomName(ctx) + rc.randomName = rootRunContext.randomName + logger.Debugf("RunContext inherited random name %s from its parent", rc.Name, rc.randomName) + } else { + rc.randomName = common.MustRandName(16) + logger.Debugf("RunContext %s is assigned random name %s", rc.Name, rc.randomName) + } + } +} + func (rc *RunContext) getNetworkCreated(ctx context.Context) bool { rc.ensureNetworkName(ctx) return rc.networkCreated diff --git a/act/runner/run_context_test.go b/act/runner/run_context_test.go index 9d167a0f..d03a1b72 100644 --- a/act/runner/run_context_test.go +++ b/act/runner/run_context_test.go @@ -937,3 +937,18 @@ func Test_waitForServiceContainer(t *testing.T) { m.AssertExpectations(t) }) } + +func TestRunContext_ensureRandomName(t *testing.T) { + parent := &RunContext{ + Name: "parentname", + } + rc := &RunContext{ + Name: "runname", + Parent: parent, + } + + parent.ensureRandomName(t.Context()) + assert.NotEmpty(t, parent.randomName) + rc.ensureRandomName(t.Context()) + assert.Equal(t, parent.randomName, rc.randomName) +}