diff --git a/act/container/docker_run.go b/act/container/docker_run.go index c7939d8c..07223cf5 100644 --- a/act/container/docker_run.go +++ b/act/container/docker_run.go @@ -23,6 +23,7 @@ import ( "slices" "strconv" "strings" + "sync" "time" "gitea.com/gitea/runner/act/common" @@ -83,8 +84,13 @@ func (cr *containerReference) Start(attach bool) common.Executor { cr.attach().IfBool(attach), cr.start(), cr.wait().IfBool(attach), - cr.tryReadID("-u", func(id int) { cr.UID = id }), - cr.tryReadID("-g", func(id int) { cr.GID = id }), + func(ctx context.Context) error { + var wg sync.WaitGroup + wg.Go(func() { _ = cr.tryReadID("-u", func(id int) { cr.UID = id })(ctx) }) + _ = cr.tryReadID("-g", func(id int) { cr.GID = id })(ctx) + wg.Wait() + return nil + }, func(ctx context.Context) error { // If this fails, then folders have wrong permissions on non root container if cr.UID != 0 || cr.GID != 0 { @@ -184,6 +190,9 @@ func (cr *containerReference) Inspect(ctx context.Context) (*Info, error) { } else if err != nil { return nil, err } + if cr.containerEnvID != cr.id && result.Container.Config != nil { + cr.containerEnv, cr.containerEnvID = opts.ConvertKVStringsToMap(result.Container.Config.Env), cr.id + } return containerInfoFromInspect(result.Container), nil } @@ -307,15 +316,13 @@ func (cr *containerReference) UpdateFromEnv(srcPath string, env *map[string]stri func (cr *containerReference) UpdateFromImageEnv(env *map[string]string) common.Executor { envMap := *env return common.Executor(func(ctx context.Context) error { - inspect, err := cr.cli.ContainerInspect(ctx, cr.id, client.ContainerInspectOptions{}) - if err != nil { - common.Logger(ctx).Error(err) - return fmt.Errorf("inspect container: %w", err) + if cr.containerEnvID != cr.id { + if _, err := cr.Inspect(ctx); err != nil { + common.Logger(ctx).Error(err) + return fmt.Errorf("inspect container: %w", err) + } } - if inspect.Container.Config == nil { - return nil - } - for key, value := range opts.ConvertKVStringsToMap(inspect.Container.Config.Env) { + for key, value := range cr.containerEnv { if envMap[key] == "" { envMap[key] = value } else if key == "PATH" { @@ -365,7 +372,9 @@ type containerReference struct { // attachDone is closed by the attach() streaming goroutine once it has // drained and flushed the container's output. wait() blocks on it so the // tail of the log lands before the step proceeds. - attachDone chan struct{} + attachDone chan struct{} + containerEnv map[string]string // fixed at creation, cached by inspect for containerEnvID + containerEnvID string LinuxContainerEnvironmentExtensions } diff --git a/act/container/docker_run_test.go b/act/container/docker_run_test.go index a4cb85ee..634ea70c 100644 --- a/act/container/docker_run_test.go +++ b/act/container/docker_run_test.go @@ -45,14 +45,15 @@ func TestDockerEnvironmentUsesContainerPath(t *testing.T) { Return(mobyclient.ContainerInspectResult{Container: container.InspectResponse{Config: &container.Config{Env: []string{ "PATH=/opt/node/x64/bin:/usr/bin:/bin", "CONFLICT_VAR=container-value", "SOME_RANDOM_VAR=", "ANOTHER_ONE=old-value", "ANOTHER_ONE=BUT_I_HAVE_VALUE", "LITERAL='quoted'=$HOME\nsecond line", - }}}}, nil) + }}}}, nil).Once() env := map[string]string{ "PATH": "/custom/bin", "RANDOM_VAR": "WITH_VALUE", "ANOTHER_VAR": "", "CONFLICT_VAR": "step-value", } - require.NoError(t, (&containerReference{cli: client, id: "amd64-job"}).UpdateFromImageEnv(&env)(t.Context())) + cr := &containerReference{cli: client, id: "amd64-job"} + require.NoError(t, cr.UpdateFromImageEnv(&env)(t.Context())) assert.Equal(t, map[string]string{ "PATH": "/custom/bin:/opt/node/x64/bin:/usr/bin:/bin", "RANDOM_VAR": "WITH_VALUE", @@ -62,6 +63,10 @@ func TestDockerEnvironmentUsesContainerPath(t *testing.T) { "CONFLICT_VAR": "step-value", "LITERAL": "'quoted'=$HOME\nsecond line", }, env) + pathEnv := map[string]string{} + require.NoError(t, cr.UpdateFromImageEnv(&pathEnv)(t.Context())) + assert.Equal(t, "/opt/node/x64/bin:/usr/bin:/bin", pathEnv["PATH"]) + client.AssertExpectations(t) } type mockDockerClient struct { diff --git a/act/container/kubernetes.go b/act/container/kubernetes.go index aef929d6..b4d0cd73 100644 --- a/act/container/kubernetes.go +++ b/act/container/kubernetes.go @@ -50,6 +50,7 @@ type KubernetesPod struct { job *kubernetesContainer services []*kubernetesContainer arch, path string + imageOS string ips []net.IP transfer *transferServer transferImage string @@ -432,11 +433,12 @@ func (c *kubernetesContainer) Start(_ bool) common.Executor { if err != nil { return err } - probed := strings.SplitN(output, "\n", 3) - for len(probed) < 3 { + probed := strings.SplitN(output, "\x00", 5) + for len(probed) < 5 { probed = append(probed, "") } c.pod.arch, c.gzip, c.pod.path = goArchToActionArch(probed[0]), probed[1] == "gzip", probed[2] + c.pod.imageOS = cmp.Or(probed[3], ImageOSFromOSRelease(probed[4])) return nil } } @@ -447,7 +449,7 @@ func (p *KubernetesPod) probe(ctx context.Context, container string) (string, er deadline := time.Now().Add(time.Minute) for delay := 100 * time.Millisecond; ; delay = min(2*delay, 2*time.Second) { var stdout bytes.Buffer - err := p.client.exec(ctx, p.name, container, []string{"sh", "-c", `printf '%s\n%s\n%s' "$(uname -m)" "$(command -v gzip >/dev/null && echo gzip)" "$PATH"`}, nil, &stdout, io.Discard) + err := p.client.exec(ctx, p.name, container, []string{"sh", "-c", `printf '%s\0%s\0%s\0%s\0%s' "$(uname -m)" "$(command -v gzip >/dev/null && echo gzip)" "$PATH" "${ImageOS-}" "$(cat /etc/os-release 2>/dev/null || cat /usr/lib/os-release 2>/dev/null)"`}, nil, &stdout, io.Discard) var exitErr ExitCodeError var apiErr *apiError if err == nil || ctx.Err() != nil || errors.As(err, &exitErr) || time.Now().After(deadline) || @@ -582,6 +584,9 @@ func (c *kubernetesContainer) UpdateFromEnv(srcPath string, env *map[string]stri func (c *kubernetesContainer) UpdateFromImageEnv(env *map[string]string) common.Executor { return func(context.Context) error { (*env)["PATH"] = cmp.Or((*env)["PATH"], c.pod.path) + if c.pod.imageOS != "" { + (*env)["ImageOS"] = cmp.Or((*env)["ImageOS"], c.pod.imageOS) + } return nil } } diff --git a/act/container/kubernetes_test.go b/act/container/kubernetes_test.go index 04192955..6b3e82bc 100644 --- a/act/container/kubernetes_test.go +++ b/act/container/kubernetes_test.go @@ -193,7 +193,7 @@ func TestKubernetesPodRunsJobThroughAPI(t *testing.T) { path := workdir + ":" + os.Getenv("PATH") kubeconfig, cluster := fakeKubernetes(t, `{"spec":{"containers":[{},{}]},"status":{"phase":"Running","containerStatuses":[ {"name":"job","ready":true,"state":{"running":{}}}, - {"name":"my-db","ready":true,"state":{"running":{}}}]}}`, "PATH="+path) + {"name":"my-db","ready":true,"state":{"running":{}}}]}}`, "PATH="+path, "ImageOS=ubuntu24-arm64") options := KubernetesOptions{Kubeconfig: kubeconfig, RunnerUUID: "runner-uuid", PodTemplates: []map[string]any{ {"spec": map[string]any{ "automountServiceAccountToken": true, @@ -309,9 +309,9 @@ func TestKubernetesPodRunsJobThroughAPI(t *testing.T) { assert.Contains(t, lastExec(), "command="+url.QueryEscape(workdir+"/act/actions/sha/")) assert.Contains(t, lastExec(), url.QueryEscape("gzip -dc |")) - pathEnv := map[string]string{} - require.NoError(t, job.UpdateFromImageEnv(&pathEnv)(ctx)) - assert.Equal(t, path, pathEnv["PATH"]) + imageEnv := map[string]string{} + require.NoError(t, job.UpdateFromImageEnv(&imageEnv)(ctx)) + assert.Equal(t, map[string]string{"PATH": path, "ImageOS": "ubuntu24-arm64"}, imageEnv) info, err := service.Inspect(ctx) require.NoError(t, err) diff --git a/act/container/linux_container_environment_extensions.go b/act/container/linux_container_environment_extensions.go index d8d7ca33..98a140f7 100644 --- a/act/container/linux_container_environment_extensions.go +++ b/act/container/linux_container_environment_extensions.go @@ -108,3 +108,22 @@ func (*LinuxContainerEnvironmentExtensions) GetRunnerContext(ctx context.Context func (*LinuxContainerEnvironmentExtensions) IsEnvironmentCaseInsensitive() bool { return false } + +// ImageOSFromOSRelease derives ImageOS from an os-release file, empty for anything but Ubuntu, the only Linux GitHub names one. +func ImageOSFromOSRelease(osRelease string) string { + var id, major string + for line := range strings.Lines(osRelease) { + key, value, _ := strings.Cut(strings.TrimSpace(line), "=") + value = strings.Trim(value, `"'`) + switch key { + case "ID": + id = value + case "VERSION_ID": + major, _, _ = strings.Cut(value, ".") + } + } + if id != "ubuntu" || major == "" { + return "" + } + return id + major +} diff --git a/act/runner/cancellation_test.go b/act/runner/cancellation_test.go index ea2aa80d..55dcdba4 100644 --- a/act/runner/cancellation_test.go +++ b/act/runner/cancellation_test.go @@ -101,10 +101,10 @@ func TestMainStepsExecutorRunsAlwaysStepsAfterInterruption(t *testing.T) { assert.Equal(t, []string{"step1", "always-step"}, ran) require.NoError(t, laterStepCtxErr) assert.Equal(t, wantError == context.Canceled, rc.jobCancelled) + assert.Equal(t, "cancelled", rc.getJobContext().Status) setJobResult(t.Context(), rc, rc, true) if wantError == context.DeadlineExceeded { assert.True(t, rc.jobFailed) - assert.Equal(t, "failure", rc.getJobContext().Status) assert.Equal(t, "failure", rc.Run.Job().Result) } else { assert.Equal(t, "cancelled", rc.Run.Job().Result) @@ -259,8 +259,11 @@ func TestReportStepErrorTreatsCancelAsInterruption(t *testing.T) { assert.True(t, rc.jobCancelled) // a real error still fails the job - failed := common.WithJobErrorContainer(t.Context()) - reportStepError(failed, rc, assert.AnError) - require.ErrorIs(t, common.JobError(failed), assert.AnError) - assert.True(t, rc.jobFailed) + for _, stepErr := range []error{assert.AnError, context.DeadlineExceeded} { + rc := &RunContext{} + failed := common.WithJobErrorContainer(t.Context()) + reportStepError(failed, rc, stepErr) + require.ErrorIs(t, common.JobError(failed), stepErr) + assert.Equal(t, "failure", rc.getJobContext().Status) + } } diff --git a/act/runner/container_mock_test.go b/act/runner/container_mock_test.go index eea0447b..4524e7aa 100644 --- a/act/runner/container_mock_test.go +++ b/act/runner/container_mock_test.go @@ -54,6 +54,10 @@ func (cm *containerMock) UpdateFromEnv(srcPath string, env *map[string]string) c return cm.Called(srcPath, env).Get(0).(func(context.Context) error) } +func (cm *containerMock) UpdateFromImageEnv(_env *map[string]string) common.Executor { + return noopExecutor +} + func (cm *containerMock) Copy(destPath string, files ...*container.FileEntry) common.Executor { return cm.Called(destPath, files).Get(0).(func(context.Context) error) } @@ -68,8 +72,9 @@ func (cm *containerMock) Exec(command []string, env map[string]string, user, wor func (cm *containerMock) GetContainerArchive(ctx context.Context, srcPath string) (io.ReadCloser, error) { args := cm.Called(ctx, srcPath) + reader, _ := args.Get(0).(io.ReadCloser) err, _ := args.Get(1).(error) - return args.Get(0).(io.ReadCloser), err + return reader, err } func (cm *containerMock) DumpLogs(ctx context.Context) error { diff --git a/act/runner/expression.go b/act/runner/expression.go index ea0d7583..fbfecedb 100644 --- a/act/runner/expression.go +++ b/act/runner/expression.go @@ -324,6 +324,12 @@ func (rc *RunContext) resolveWorkflowCall(ctx context.Context) error { if rc.workflowCallSecrets, err = callerEval.EvaluateCallerSecrets(secrets); err != nil { return fmt.Errorf("unable to interpolate %w", err) } + for _, name := range []string{"GITHUB_TOKEN", "GITEA_TOKEN"} { + token, ok := rc.Config.Secrets[name] + if _, mapped := common.LookupFold(rc.workflowCallSecrets, name, true); ok && !mapped { + rc.workflowCallSecrets[name] = token + } + } return nil } diff --git a/act/runner/expression_test.go b/act/runner/expression_test.go index 9a8cbf6e..642b2cb6 100644 --- a/act/runner/expression_test.go +++ b/act/runner/expression_test.go @@ -503,7 +503,7 @@ jobs: assert.Equal(t, "workflow", env["INPUT_FALLBACK"]) } -func TestResolveWorkflowCallToleratesMismatchesAndPassesInheritedSecretsAndRuntimeToken(t *testing.T) { +func TestResolveWorkflowCallToleratesMismatchesAndPassesSecretsAndTokens(t *testing.T) { callerWorkflow, err := model.ReadWorkflow(strings.NewReader(` jobs: call: @@ -513,6 +513,9 @@ jobs: inherit: uses: ./reuse.yml secrets: inherit + override: + uses: ./reuse.yml + secrets: {github_token: "${{ secrets.token }}", GITEA_TOKEN: ""} `)) require.NoError(t, err) callee, err := model.ReadWorkflow(strings.NewReader(` @@ -527,7 +530,7 @@ jobs: notCallable, err := model.ReadWorkflow(strings.NewReader("on: push\njobs:\n test: {}\n")) require.NoError(t, err) config := &Config{ - Secrets: map[string]string{"token": "${{ github.token }}"}, + Secrets: map[string]string{"token": "${{ github.token }}", "GITHUB_TOKEN": "github-token", "GITEA_TOKEN": "gitea-token"}, PresetGitHubContext: &model.GithubContext{}, RuntimeTokens: map[*model.Workflow]string{callerWorkflow: "caller-run"}, } @@ -541,11 +544,17 @@ jobs: called := resolve("call", callee) assert.Equal(t, map[string]any{"flag": true}, called.workflowCallInputs) - assert.Equal(t, map[string]string{"undeclared": "${{ github.token }}"}, called.workflowCallSecrets) + assert.Equal(t, map[string]string{"undeclared": "${{ github.token }}", "GITHUB_TOKEN": "github-token", "GITEA_TOKEN": "gitea-token"}, called.workflowCallSecrets) assert.Equal(t, "caller-run", called.GetEnv()["ACTIONS_RUNTIME_TOKEN"]) inherited := resolve("inherit", notCallable) assert.Empty(t, inherited.workflowCallInputs) - assert.Equal(t, map[string]string{"token": "${{ github.token }}", "ACTIONS_RUNTIME_TOKEN": "caller-run"}, inherited.workflowCallSecrets) + assert.Equal(t, map[string]string{"token": "${{ github.token }}", "GITHUB_TOKEN": "github-token", "GITEA_TOKEN": "gitea-token", "ACTIONS_RUNTIME_TOKEN": "caller-run"}, inherited.workflowCallSecrets) + assert.Equal(t, "${{ github.token }}:", resolve("override", callee).ExprEval.InterpolateName(t.Context(), "${{ secrets.GITHUB_TOKEN }}:${{ secrets.GITEA_TOKEN }}")) + middle, err := model.ReadWorkflow(strings.NewReader("on: workflow_call\njobs:\n test: {uses: ./reuse.yml}\n")) + require.NoError(t, err) + nested, err := (&runnerImpl{config: config, caller: &caller{runContext: resolve("override", middle)}}).newRunContext(t.Context(), &model.Run{Workflow: callee, JobID: "test"}, nil) + require.NoError(t, err) + assert.Equal(t, "github-token:gitea-token", nested.ExprEval.InterpolateName(t.Context(), "${{ secrets.GITHUB_TOKEN }}:${{ secrets.GITEA_TOKEN }}")) } func TestJobNameMasksSecrets(t *testing.T) { diff --git a/act/runner/job_hooks.go b/act/runner/job_hooks.go index c95c1291..ecf65164 100644 --- a/act/runner/job_hooks.go +++ b/act/runner/job_hooks.go @@ -44,7 +44,7 @@ func (rc *RunContext) runJobHook(ctx context.Context, hookPath, name string) err rc.CurrentStep = "" defer func() { rc.CurrentStep = previousStep }() - env := rc.mergeEnv(rc.caEnv, rc.containerSpec.Env, rc.GetEnv()) + env := rc.mergeEnv(rc.defaultEnv, rc.containerSpec.Env, rc.GetEnv()) rc.withGithubEnv(ctx, rc.getGithubContext(ctx), env) rc.ApplyExtraPath(ctx, &env) diff --git a/act/runner/reusable_workflow.go b/act/runner/reusable_workflow.go index 64d7453b..691a91ea 100644 --- a/act/runner/reusable_workflow.go +++ b/act/runner/reusable_workflow.go @@ -165,6 +165,10 @@ func setReusedWorkflowCallerResult(rc *RunContext, runner *runnerImpl) common.Ex hasFailure = true } } + if err := setJobOutputs(ctx, rc); err != nil { // a bad output fails this job, as for ordinary jobs + common.Logger(ctx).Errorf("##[error]%s", EscapeCommandData(err.Error())) + hasFailure = true + } reusedWorkflowJobResult := "success" reusedWorkflowJobResultMessage := "succeeded" @@ -173,14 +177,15 @@ func setReusedWorkflowCallerResult(rc *RunContext, runner *runnerImpl) common.Ex reusedWorkflowJobResultMessage = "failed" } + // Serialize this shared Job.Result write against the other matrix combos + // and setJobResult (same lockJob key). + unlock := lockJob(rc.Run.Job()) + rc.result(reusedWorkflowJobResult) + unlock() + if rc.caller != nil { rc.caller.setReusedWorkflowJobResult(rc.Run.JobID, reusedWorkflowJobResult) } else { - // Serialize this shared Job.Result write against the other matrix combos - // and setJobResult (same lockJob key). - unlock := lockJob(rc.Run.Job()) - rc.result(reusedWorkflowJobResult) - unlock() common.Logger(ctx).With("jobResult", reusedWorkflowJobResult).Infof("Job %s", reusedWorkflowJobResultMessage) } diff --git a/act/runner/reusable_workflow_test.go b/act/runner/reusable_workflow_test.go index 282547d9..f2e4f08d 100644 --- a/act/runner/reusable_workflow_test.go +++ b/act/runner/reusable_workflow_test.go @@ -13,6 +13,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "sync" "testing" "time" @@ -213,3 +214,17 @@ func gitMust(t *testing.T, dir string, args ...string) { out, err := cmd.CombinedOutput() require.NoError(t, err, "git %v: %s", args, string(out)) } + +func TestSetReusedWorkflowCallerResultFailsOnBadOutputs(t *testing.T) { + callerWorkflow, err := model.ReadWorkflow(strings.NewReader("jobs:\n call: {uses: ./reuse.yml}\n")) + require.NoError(t, err) + called, err := model.ReadWorkflow(strings.NewReader("on:\n workflow_call:\n outputs:\n bad: {value: \"${{ fromJSON('not-json') }}\"}\njobs:\n inner: {uses: ./leaf.yml}\n")) + require.NoError(t, err) + config := &Config{PresetGitHubContext: &model.GithubContext{}} + parent, err := (&runnerImpl{config: config}).newRunContext(t.Context(), &model.Run{Workflow: callerWorkflow, JobID: "call"}, nil) + require.NoError(t, err) + rc, err := (&runnerImpl{config: config, caller: &caller{runContext: parent, reusedWorkflowJobResults: map[string]string{}}}).newRunContext(t.Context(), &model.Run{Workflow: called, JobID: "inner"}, nil) + require.NoError(t, err) + require.NoError(t, setReusedWorkflowCallerResult(rc, &runnerImpl{caller: &caller{reusedWorkflowJobResults: map[string]string{"leaf": "success"}}})(t.Context())) + require.Equal(t, "failure", rc.Run.Job().Result) +} diff --git a/act/runner/run_context.go b/act/runner/run_context.go index daba5dcf..0c7e1904 100644 --- a/act/runner/run_context.go +++ b/act/runner/run_context.go @@ -69,7 +69,7 @@ type RunContext struct { kubernetes bool containerSpec model.ContainerSpec // container:, resolved once with platformImage containerEnv yaml.Node // container.env of the kept containerSpec, decoded once the job env resolves - caEnv map[string]string // NODE_EXTRA_CA_CERTS for the runner CA, beneath any env the job and its image set + defaultEnv map[string]string // ImageOS and the runner CA's NODE_EXTRA_CA_CERTS, beneath any env the job sets JobName string ActionPath string Parent *RunContext @@ -103,6 +103,7 @@ type RunContext struct { // getJobContext report the "cancelled" status so cancelled()/always() evaluate the way // GitHub Actions does, letting cleanup and always() steps run while normal steps skip. jobCancelled bool + jobTimedOut bool // jobFailed records failures outside normal main-step results, such as action pre-step // failures. Those failures must still make success() false and failure() true for later // main-step if evaluation. @@ -160,12 +161,13 @@ func (rc *RunContext) markFailed() { // markInterrupted records the job's interruption status from a context error so later step `if` evaluations and the job result observe it, // keeping the timeout path symmetric with the cancel path: // - context.Canceled (server cancel) marks the job cancelled, matching GitHub's "only always()/cancelled() run on cancel". -// - context.DeadlineExceeded (job timeout-minutes) marks the job failed, matching the "Timeout -> FAILURE" reporting semantics. +// - context.DeadlineExceeded (job timeout-minutes) evaluates later steps as cancelled like GitHub, but reports the job failed. func (rc *RunContext) markInterrupted(err error) { switch { case errors.Is(err, context.Canceled): rc.markCancelled() case errors.Is(err, context.DeadlineExceeded): + rc.jobTimedOut = true rc.markFailed() } } @@ -1216,6 +1218,24 @@ func (svc *serviceContainer) healthOutputSuffix() string { func (rc *RunContext) copyWorkflowFiles() common.Executor { return func(ctx context.Context) error { + imageEnv := map[string]string{} + imageEnvErr := rc.JobContainer.UpdateFromImageEnv(&imageEnv)(ctx) + rc.defaultEnv = map[string]string{} + imageOS, set := imageEnv["ImageOS"] + if !set { + if !rc.kubernetes { // a pod reports its release through its startup probe + for _, osReleasePath := range []string{"/usr/lib/os-release", "/etc/os-release"} { // the archive skips symlinks, as /etc's is on Ubuntu + if osRelease, ok := readSingleFileFromContainerArchive(ctx, rc.JobContainer, osReleasePath, 64<<10); ok { + imageOS = container.ImageOSFromOSRelease(string(osRelease)) + break + } + } + } + imageOS = cmp.Or(imageOS, rc.imageOS(ctx)) + } + if set || imageOS != "" { + rc.defaultEnv["ImageOS"] = imageOS + } files := []*container.FileEntry{ {Name: "workflow/event.json", Mode: 0o644, Body: rc.EventJSON}, {Name: "workflow/envs.txt", Mode: 0o666}, @@ -1226,12 +1246,11 @@ func (rc *RunContext) copyWorkflowFiles() common.Executor { return fmt.Errorf("read runner CA file: %w", err) } files = append(files, &container.FileEntry{Name: "ca.crt", Mode: 0o644, Body: string(ca)}) - imageEnv := map[string]string{} - if err = rc.JobContainer.UpdateFromImageEnv(&imageEnv)(ctx); err != nil { - return err + if imageEnvErr != nil { + return imageEnvErr } if _, set := imageEnv["NODE_EXTRA_CA_CERTS"]; !set { - rc.caEnv = map[string]string{"NODE_EXTRA_CA_CERTS": rc.jobCAFile()} + rc.defaultEnv["NODE_EXTRA_CA_CERTS"] = rc.jobCAFile() } } return rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", files...)(ctx) @@ -1604,7 +1623,7 @@ func (rc *RunContext) getJobContext() *model.JobContext { // A cancelled run takes precedence over success/failure so cancelled() is true and // success()/failure() are false, matching GitHub Actions: on cancellation only // always() and cancelled() steps run. - if job.jobCancelled { + if job.jobCancelled || job.jobTimedOut { jobStatus = "cancelled" } @@ -1789,7 +1808,7 @@ func aliasGiteaEnv(env map[string]string) { } } -func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubContext, env map[string]string) { +func (rc *RunContext) withGithubEnv(_ctx context.Context, github *model.GithubContext, env map[string]string) { if _, ok := env["CI"]; !ok { env["CI"] = "true" } @@ -1858,10 +1877,6 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon env["ACTIONS_RUNTIME_URL"] = cmp.Or(os.Getenv("ACTIONS_RUNTIME_URL"), "http://"+net.JoinHostPort(rc.Config.ArtifactServerAddr, rc.Config.ArtifactServerPort)+"/") env["ACTIONS_RUNTIME_TOKEN"] = cmp.Or(rc.runtimeToken, os.Getenv("ACTIONS_RUNTIME_TOKEN"), "token") } - - if imageOS := rc.imageOS(ctx); imageOS != "" { - env["ImageOS"] = imageOS - } } // parentDir returns the directory containing p, or "" when p names no parent. Both @@ -1878,7 +1893,7 @@ func parentDir(p string) string { // from another. The resolved image tag is preferred over the runs-on label because it // still names a release when the label is a rolling one such as ubuntu-latest. func (rc *RunContext) imageOS(ctx context.Context) string { - if rc.Run.Job().RunsOn() == nil { + if job := rc.Run.Job(); job == nil || job.RunsOn() == nil { return "" // a composite action runs on a synthetic job without runs-on } if imageOS := imageOSFromImage(rc.platformImage); imageOS != "" { @@ -1887,8 +1902,8 @@ func (rc *RunContext) imageOS(ctx context.Context) string { for _, platformName := range slices.Backward(rc.runsOnPlatformNames(ctx)) { if platformName == "ubuntu-latest" { - // Rolling label whose image names no release either, so keep the historical value. - return "ubuntu20" + // Rolling label whose image names no release either, so report GitHub's current ubuntu-latest. + return "ubuntu24" } else if platformName != "" { return strings.SplitN(strings.Replace(platformName, `-`, ``, 1), `.`, 2)[0] } diff --git a/act/runner/run_context_test.go b/act/runner/run_context_test.go index a672c4e3..9591e444 100644 --- a/act/runner/run_context_test.go +++ b/act/runner/run_context_test.go @@ -168,6 +168,14 @@ func (fakeContainer) Copy(string, ...*container.FileEntry) common.Executor { return func(context.Context) error { return nil } } +func (fakeContainer) UpdateFromImageEnv(_env *map[string]string) common.Executor { + return noopExecutor +} + +func (fakeContainer) GetContainerArchive(_ctx context.Context, _srcPath string) (io.ReadCloser, error) { + return nil, os.ErrNotExist +} + func (fakeContainer) Inspect(context.Context) (*container.Info, error) { return &container.Info{ID: "fake", State: "running", Health: container.HealthNone}, nil } @@ -1395,6 +1403,7 @@ func TestRunContext_cleanupFailedStart(t *testing.T) { instance.On("Close").Return(record(name+".Close", nil)) } job.On("Copy", mock.Anything, mock.Anything).Return(record("job.Copy", nil)) + job.On("GetContainerArchive", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist) job.On("Inspect", mock.Anything).Return(&container.Info{ID: "job-id"}, nil). Run(func(mock.Arguments) { operations = append(operations, "job.Inspect") }) for _, health := range []string{container.HealthStarting, testcase.health} { @@ -1608,13 +1617,21 @@ func TestGetJobContextReportsContainers(t *testing.T) { type imageEnvContainer struct { *container.HostEnvironment - imageEnv map[string]string + imageEnv map[string]string + osRelease []byte } func (c imageEnvContainer) UpdateFromImageEnv(env *map[string]string) common.Executor { return func(_ctx context.Context) error { maps.Copy(*env, c.imageEnv); return nil } } +func (c imageEnvContainer) GetContainerArchive(_ctx context.Context, srcPath string) (io.ReadCloser, error) { + if srcPath != "/usr/lib/os-release" || c.osRelease == nil { + return nil, os.ErrNotExist + } + return io.NopCloser(bytes.NewReader(c.osRelease)), nil +} + func TestCopyWorkflowFilesHandsNodeTheRunnerCAUnlessTheJobOrImageSetsIt(t *testing.T) { caFile, actPath := filepath.Join(t.TempDir(), "ca.crt"), t.TempDir() require.NoError(t, os.WriteFile(caFile, []byte("PEM\n"), 0o600)) @@ -1624,7 +1641,7 @@ func TestCopyWorkflowFilesHandsNodeTheRunnerCAUnlessTheJobOrImageSetsIt(t *testi Config: &Config{CAFile: caFile}, Run: &model.Run{JobID: "job", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"job": {}}}}, Env: maps.Clone(jobEnv), - JobContainer: imageEnvContainer{&container.HostEnvironment{Path: t.TempDir(), ActPath: actPath, StdOut: io.Discard}, imageEnv}, + JobContainer: imageEnvContainer{&container.HostEnvironment{Path: t.TempDir(), ActPath: actPath, StdOut: io.Discard}, imageEnv, nil}, } rc.containerSpec.Env = containerEnv require.NoError(t, rc.copyWorkflowFiles()(t.Context())) @@ -1741,6 +1758,30 @@ func TestRunContextContainerImageCannotSelectBackend(t *testing.T) { func TestRunContextImageOS(t *testing.T) { ctx := t.Context() + t.Run("keeps an ImageOS from the image, the job env or the job's Ubuntu release", func(t *testing.T) { + ubuntu := tarArchive(t, tarEntry{name: "os-release", body: "ID=ubuntu\nVERSION_ID=\"22.04\"\n"}) + for _, tc := range []struct { + imageEnv map[string]string + jobEnv string + osRelease []byte + want string + }{ + {imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, osRelease: ubuntu, want: "ubuntu24-arm64"}, + {imageEnv: map[string]string{"ImageOS": "ubuntu24-arm64"}, jobEnv: "ImageOS: ubuntu20", want: "ubuntu20"}, + {osRelease: ubuntu, want: "ubuntu22"}, + {osRelease: tarArchive(t, tarEntry{name: "os-release", body: "ID=debian\nVERSION_ID=\"12\"\n"}), want: "ubuntu24"}, + } { + rc := createRunsOnRunContext(t, "ubuntu-latest\nenv: {"+tc.jobEnv+"}") + rc.platformImage = "docker.gitea.com/runner-images:ubuntu-latest" + rc.JobContainer = imageEnvContainer{&container.HostEnvironment{ActPath: t.TempDir()}, tc.imageEnv, tc.osRelease} + require.NoError(t, rc.copyWorkflowFiles()(ctx)) + require.NoError(t, evaluateJobEnvAndDefaults(ctx, rc)) + env := map[string]string{} + require.NoError(t, setupEnv(ctx, &stepRun{RunContext: rc, Step: &model.Step{}, env: env})) + assert.Equal(t, tc.want, env["ImageOS"]) + } + }) + t.Run("prefers the release in the resolved image tag", func(t *testing.T) { rc := createRunsOnRunContext(t, "ubuntu-latest") rc.Config.PlatformPicker = func([]string) string { return "docker.gitea.com/runner-images:ubuntu-24.04" } @@ -1755,8 +1796,8 @@ func TestRunContextImageOS(t *testing.T) { assert.Equal(t, "ubuntu22", rc.imageOS(ctx)) }) - t.Run("keeps the historical value for a rolling label with no release", func(t *testing.T) { - assert.Equal(t, "ubuntu20", createRunsOnRunContext(t, "ubuntu-latest").imageOS(ctx)) + t.Run("reports the current release for the rolling ubuntu-latest label", func(t *testing.T) { + assert.Equal(t, "ubuntu24", createRunsOnRunContext(t, "ubuntu-latest").imageOS(ctx)) }) t.Run("is empty for the synthetic job of a composite action", func(t *testing.T) { diff --git a/act/runner/step.go b/act/runner/step.go index 8581ee7f..44fd9ffd 100644 --- a/act/runner/step.go +++ b/act/runner/step.go @@ -265,7 +265,7 @@ func setupEnv(ctx context.Context, step step) error { env := step.getEnv() // container env is the image's baseline, which job env and $GITHUB_ENV override - mergeIntoMap(step, env, rc.caEnv, rc.containerSpec.Env, rc.GetEnv()) + mergeIntoMap(step, env, rc.defaultEnv, rc.containerSpec.Env, rc.GetEnv()) rc.withGithubEnv(ctx, getStepGithubContext(ctx, step), *env) var err error diff --git a/act/runner/testdata/.github/workflows/nested-reusable-workflow.yml b/act/runner/testdata/.github/workflows/nested-reusable-workflow.yml new file mode 100644 index 00000000..782fe3dc --- /dev/null +++ b/act/runner/testdata/.github/workflows/nested-reusable-workflow.yml @@ -0,0 +1,24 @@ +on: + workflow_call: + outputs: + output: + value: ${{ jobs.first.outputs.output }} + dependent: + value: ${{ jobs.dependent.outputs.output }} + +jobs: + first: + uses: ./.github/workflows/local-reusable-workflow.yml + with: + string_required: string + bool_required: true + number_required: 1 + secrets: inherit + dependent: + needs: first + uses: ./.github/workflows/local-reusable-workflow.yml + with: + string_required: ${{ needs.first.result == 'success' && needs.first.outputs.output || 'failure' }} + bool_required: true + number_required: 1 + secrets: inherit diff --git a/act/runner/testdata/uses-workflow/push.yml b/act/runner/testdata/uses-workflow/push.yml index 9da95379..fb2336b2 100644 --- a/act/runner/testdata/uses-workflow/push.yml +++ b/act/runner/testdata/uses-workflow/push.yml @@ -21,11 +21,18 @@ jobs: number_required: 1 secrets: inherit + nested-reusable-workflow: + uses: ./.github/workflows/nested-reusable-workflow.yml + secrets: inherit + output-test: runs-on: ubuntu-latest needs: - reusable-workflow - reusable-workflow-with-inherited-secrets + - nested-reusable-workflow steps: - run: '[[ "${{ needs.reusable-workflow.outputs.output == ''string'' }}" = "true" ]] || exit 1' - run: '[[ "${{ needs.reusable-workflow-with-inherited-secrets.outputs.output == ''string'' }}" = "true" ]] || exit 1' + - run: '[[ "${{ needs.nested-reusable-workflow.outputs.output == ''string'' }}" = "true" ]] || exit 1' + - run: '[[ "${{ needs.nested-reusable-workflow.outputs.dependent == ''string'' }}" = "true" ]] || exit 1' diff --git a/e2e/testdata/workflows/kubernetes.yml b/e2e/testdata/workflows/kubernetes.yml index f4ddef5d..19afe397 100644 --- a/e2e/testdata/workflows/kubernetes.yml +++ b/e2e/testdata/workflows/kubernetes.yml @@ -17,4 +17,5 @@ jobs: - run: | test "$FROM_ENV" = exported test -n "$KUBERNETES_SERVICE_HOST" -a -n "${{ runner.arch }}" + tr '\0' '\n' < /proc/1/environ | grep -qx "ImageOS=$ImageOS" curl -fsS -o /dev/null http://web/