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) +}