mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-11-23 20:33:23 +08:00
Closes many small gaps between how workflows run on GitHub and on the runner, each checked against GitHub's runner and live runs. - `action.yml` expressions are checked against the contexts GitHub allows per field, and input defaults are evaluated with those contexts only - Reusable workflows take inputs from `workflow_call` only, evaluate outputs once after all called jobs finish, stop at 10 nested workflows and resolve relative `./` paths in remote workflows against that workflow's repository - A called job failing under `continue-on-error` no longer fails the caller, and same-repository calls use their own clone cache - Job timeouts cancel the job with GitHub's message, step timeouts fail the step with GitHub's message, and invalid `timeout-minutes` values behave like on GitHub - `runner.timeout` expiry names the setting and server cancellation is logged - Matrix and strategy evaluation errors fail the job - `exec` records skipped jobs and sets `github.run_attempt`, `workflow_ref` and `workflow_sha` - `runs-on` expressions keep their types - Step summaries upload as each step ends - Docker actions join the job network, and the job container no longer gets a network alias from the job name - Container, network and volume names are unique per job invocation - Cached images are matched against the daemon's platform and Dockerfile actions build for the requested platform - `runner.arch` maps 32-bit ARM and x86 names like GitHub - Pinned action SHAs are verified and git replace objects are ignored - `hashFiles` uses GitHub's current bundle - Registration trims labels and writes `.runner` as `0600` - The container entrypoint quotes its arguments and reads the registration token from the environment or a file - The systemd example uses `KillMode=mixed` so running jobs can finish on stop - The cache server releases its database between eviction batches - Shared rules come from actionslib v1.7.0, added in https://gitea.com/gitea/actionslib/pulls/34 _Written by Claude (Opus 5.5)._ Reviewed-on: https://gitea.com/gitea/runner/pulls/1421 Reviewed-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io>
396 lines
14 KiB
Go
396 lines
14 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package runner
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"fmt"
|
|
maps0 "maps"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.com/gitea/runner/act/common"
|
|
"gitea.com/gitea/runner/act/container"
|
|
"gitea.com/gitea/runner/internal/pkg/telemetry"
|
|
|
|
"gitea.dev/actionslib/pkg/exprparser"
|
|
"gitea.dev/actionslib/pkg/model"
|
|
)
|
|
|
|
const (
|
|
pathFileCommand = "workflow/pathcmd.txt"
|
|
envFileCommand = "workflow/envs.txt"
|
|
)
|
|
|
|
type step interface {
|
|
pre() common.Executor
|
|
main() common.Executor
|
|
post() common.Executor
|
|
|
|
getRunContext() *RunContext
|
|
getStepModel() *model.Step
|
|
getEnv() *map[string]string
|
|
getIfExpression(context context.Context, stage stepStage) string
|
|
}
|
|
|
|
func stepActionIdentity(step step) (repository, ref string) {
|
|
if action, ok := step.(interface{ actionIdentity() (string, string) }); ok {
|
|
return action.actionIdentity()
|
|
}
|
|
return "", ""
|
|
}
|
|
|
|
func getStepGithubContext(ctx context.Context, step step) *model.GithubContext {
|
|
github := step.getRunContext().getGithubContext(ctx)
|
|
github.ActionRepository, github.ActionRef = stepActionIdentity(step)
|
|
return github
|
|
}
|
|
|
|
type stepStage int
|
|
|
|
const (
|
|
stepStagePre stepStage = iota
|
|
stepStageMain
|
|
stepStagePost
|
|
)
|
|
|
|
// Controls how many symlinks are resolved for local and remote Actions
|
|
const maxSymlinkDepth = 10
|
|
|
|
func (s stepStage) String() string {
|
|
switch s {
|
|
case stepStagePre:
|
|
return StagePre
|
|
case stepStageMain:
|
|
return StageMain
|
|
case stepStagePost:
|
|
return StagePost
|
|
}
|
|
return "Unknown"
|
|
}
|
|
|
|
func processRunnerEnvFileCommand(ctx context.Context, fileName string, rc *RunContext, setter func(context.Context, map[string]string, string)) error {
|
|
if rc.JobContainer.IsEnvironmentCaseInsensitive() {
|
|
return container.ParseEnvFile(rc.JobContainer, container.JoinPath(rc.JobContainer.GetActPath(), fileName), func(name, value string) {
|
|
setter(ctx, map[string]string{"name": name}, value)
|
|
}, true)(ctx)
|
|
}
|
|
env := map[string]string{}
|
|
err := rc.JobContainer.UpdateFromEnv(container.JoinPath(rc.JobContainer.GetActPath(), fileName), &env)(ctx)
|
|
for key, value := range env {
|
|
setter(ctx, map[string]string{"name": key}, value)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func runStepExecutor(step step, stage stepStage, executor common.Executor) common.Executor {
|
|
return func(ctx context.Context) (err error) {
|
|
logger := common.Logger(ctx)
|
|
rc := step.getRunContext()
|
|
stepModel := step.getStepModel()
|
|
|
|
stepResult := &model.StepResult{
|
|
Outcome: model.StepStatusSuccess,
|
|
Conclusion: model.StepStatusSuccess,
|
|
Outputs: make(map[string]string),
|
|
}
|
|
ctx, endStep := telemetry.StartStep(ctx, stepModel, stage.String())
|
|
defer func() { endStep(err, stepResult) }()
|
|
|
|
ifExpression := step.getIfExpression(ctx, stage)
|
|
rc.CurrentStep = stepModel.ID
|
|
|
|
if stage == stepStageMain {
|
|
rc.StepResults[rc.CurrentStep] = stepResult
|
|
}
|
|
|
|
err = setupEnv(ctx, step)
|
|
var runStep bool
|
|
if err == nil {
|
|
runStep, err = isStepEnabled(ctx, ifExpression, step, stage)
|
|
}
|
|
if err == nil && runStep {
|
|
env := *step.getEnv()
|
|
env["GITHUB_ACTION_REPOSITORY"], env["GITHUB_ACTION_REF"] = stepActionIdentity(step)
|
|
err = setupInputs(ctx, step)
|
|
}
|
|
if err != nil {
|
|
stepResult.Conclusion = model.StepStatusFailure
|
|
stepResult.Outcome = model.StepStatusFailure
|
|
logger.With("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepModel)
|
|
return err
|
|
}
|
|
|
|
if !runStep {
|
|
stepResult.Conclusion = model.StepStatusSkipped
|
|
stepResult.Outcome = model.StepStatusSkipped
|
|
logger.With("stepResult", stepResult.Conclusion).Debugf("Skipping step '%s' due to '%s'", stepModel, ifExpression)
|
|
return nil
|
|
}
|
|
|
|
stepString := rc.ExprEval.InterpolateName(ctx, stepModel.String())
|
|
if strings.Contains(stepString, "::add-mask::") {
|
|
stepString = "add-mask command"
|
|
}
|
|
startLogger := logger.With(StepStartedField, true)
|
|
if stage == stepStageMain {
|
|
// Main steps print their own raw "Run <title>" header, so this line is redundant and
|
|
// only leaks into the "Set up job" section for the first step; keep it as a debug trace.
|
|
startLogger.Debugf("Run %s %s", stage, stepString)
|
|
} else {
|
|
startLogger.Infof("Run %s %s", stage, stepString)
|
|
}
|
|
|
|
// Prepare and clean Runner File Commands
|
|
actPath := rc.JobContainer.GetActPath()
|
|
|
|
outputFileCommand := path.Join("workflow", "outputcmd.txt")
|
|
(*step.getEnv())["GITHUB_OUTPUT"] = container.JoinPath(actPath, outputFileCommand)
|
|
|
|
stateFileCommand := path.Join("workflow", "statecmd.txt")
|
|
(*step.getEnv())["GITHUB_STATE"] = container.JoinPath(actPath, stateFileCommand)
|
|
|
|
(*step.getEnv())["GITHUB_PATH"] = container.JoinPath(actPath, pathFileCommand)
|
|
(*step.getEnv())["GITHUB_ENV"] = container.JoinPath(actPath, envFileCommand)
|
|
|
|
// Per-step summary file. Composite sub-steps share the outer job step's index
|
|
// via the Parent chain so all writes from within a composite action accumulate
|
|
// in the same file and upload under the outer step_index.
|
|
topRC := rc.topLevelRunContext()
|
|
stepSummaryIndex := topRC.CurrentStepIndex
|
|
summaryFileCommand := path.Join("workflow", "step-summary-"+strconv.Itoa(stepSummaryIndex)+".md")
|
|
summaryPath := container.JoinPath(actPath, summaryFileCommand)
|
|
(*step.getEnv())["GITHUB_STEP_SUMMARY"] = summaryPath
|
|
aliasGiteaEnv(*step.getEnv())
|
|
|
|
// Reset the per-phase file-command files. GITHUB_STEP_SUMMARY is intentionally
|
|
// excluded here and initialized below at most once per step so writes from later
|
|
// phases and from composite sub-steps accumulate instead of being truncated.
|
|
files := []*container.FileEntry{
|
|
{Name: outputFileCommand, Mode: 0o666},
|
|
{Name: stateFileCommand, Mode: 0o666},
|
|
{Name: pathFileCommand, Mode: 0o666},
|
|
{Name: envFileCommand, Mode: 0o666},
|
|
}
|
|
if topRC.summaryFileInitialized == nil {
|
|
topRC.summaryFileInitialized = map[int]bool{}
|
|
}
|
|
initialFiles := files
|
|
if !topRC.summaryFileInitialized[stepSummaryIndex] {
|
|
initialFiles = append([]*container.FileEntry{{Name: summaryFileCommand, Mode: 0o666}}, files...)
|
|
topRC.summaryFileInitialized[stepSummaryIndex] = true
|
|
}
|
|
fileCommandInitErr := rc.JobContainer.Copy(actPath, initialFiles...)(ctx)
|
|
if fileCommandInitErr != nil && len(initialFiles) > len(files) {
|
|
fileCommandInitErr = rc.JobContainer.Copy(actPath, files...)(ctx) // a broken summary must not block the resets
|
|
}
|
|
|
|
timeoutCtx, cancelTimeout := evaluateStepTimeout(ctx, step, stage, stepString)
|
|
defer cancelTimeout()
|
|
err = fileCommandInitErr
|
|
if err == nil {
|
|
err = rc.runWithCommands(timeoutCtx, *step.getEnv(), executor)
|
|
err = cmp.Or(context.Cause(timeoutCtx), err)
|
|
}
|
|
fileCommandCtx := ctx
|
|
if ctx.Err() != nil { // an interrupted step still records its file commands, a live deadline stays in force
|
|
var cancelFileCommands context.CancelFunc
|
|
fileCommandCtx, cancelFileCommands = context.WithTimeout(context.WithoutCancel(ctx), jobCleanupTimeout)
|
|
defer cancelFileCommands()
|
|
}
|
|
if fileCommandInitErr == nil {
|
|
if fileErr := processRunnerEnvFileCommand(fileCommandCtx, envFileCommand, rc, rc.setEnvFile); fileErr != nil && err == nil {
|
|
err = fileErr
|
|
}
|
|
if fileErr := processRunnerEnvFileCommand(fileCommandCtx, stateFileCommand, rc, rc.saveState); fileErr != nil && err == nil {
|
|
err = fileErr
|
|
}
|
|
if fileErr := processRunnerEnvFileCommand(fileCommandCtx, outputFileCommand, rc, rc.setOutput); fileErr != nil && err == nil {
|
|
err = fileErr
|
|
}
|
|
if fileErr := rc.UpdateExtraPath(fileCommandCtx, container.JoinPath(actPath, pathFileCommand)); fileErr != nil && err == nil {
|
|
err = fileErr
|
|
}
|
|
processStepSummary(fileCommandCtx, rc, summaryPath)
|
|
}
|
|
_ = rc.JobContainer.Copy(actPath, files...)(fileCommandCtx)
|
|
|
|
if err == nil {
|
|
logger.With("stepResult", stepResult.Conclusion).Infof("Success - %s %s", stage, stepString)
|
|
} else if _, timedOut := context.Cause(ctx).(jobTimeout); timedOut || ctx.Err() == context.Canceled {
|
|
stepResult.Outcome, stepResult.Conclusion = model.StepStatusFailure, model.StepStatusFailure
|
|
logger.With("stepResult", "cancelled").Infof("Cancelled - %s %s", stage, stepString)
|
|
} else {
|
|
stepResult.Outcome = model.StepStatusFailure
|
|
|
|
continueOnError, parseErr := isContinueOnError(ctx, stepModel.RawContinueOnError, step)
|
|
if parseErr != nil {
|
|
stepResult.Conclusion = model.StepStatusFailure
|
|
logger.With("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepString)
|
|
return parseErr
|
|
}
|
|
|
|
if continueOnError {
|
|
logger.Errorf("##[error]%s", EscapeCommandData(err.Error()))
|
|
logger.Infof("Failed but continue next step")
|
|
err = nil
|
|
stepResult.Conclusion = model.StepStatusSuccess
|
|
} else {
|
|
stepResult.Conclusion = model.StepStatusFailure
|
|
}
|
|
|
|
// Infof: Errorf entries are promoted to the user log by the reporter,
|
|
// which would duplicate the ##[error] annotation emitted elsewhere.
|
|
logger.With("stepResult", stepResult.Conclusion).Infof("Failure - %s %s", stage, stepString)
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
|
|
type stepTimeout string
|
|
|
|
func (timeout stepTimeout) Error() string { return string(timeout) }
|
|
func (stepTimeout) Unwrap() error { return context.DeadlineExceeded }
|
|
|
|
func evaluateStepTimeout(ctx context.Context, step step, stage stepStage, name string) (context.Context, context.CancelFunc) {
|
|
if raw := step.getStepModel().TimeoutMinutes; raw != "" {
|
|
minutes, err := step.getRunContext().NewStepExpressionEvaluator(ctx, step).shared(ctx).EvaluateTimeoutMinutes(raw)
|
|
if err != nil {
|
|
logger := common.Logger(ctx)
|
|
logger.Error("##[error]An error occurred when attempting to determine the step timeout.")
|
|
logger.Errorf("##[error]%s", EscapeCommandData(err.Error()))
|
|
} else if minutes > 0 {
|
|
name = model.StepDisplayName(step.getStepModel(), name)
|
|
if stage != stepStageMain {
|
|
name = stage.String() + " " + name
|
|
}
|
|
return context.WithTimeoutCause(ctx, time.Duration(minutes)*time.Minute, stepTimeout(fmt.Sprintf("The action '%s' has timed out after %d minutes.", name, minutes)))
|
|
}
|
|
}
|
|
return ctx, func() {}
|
|
}
|
|
|
|
func setupEnv(ctx context.Context, step step) error {
|
|
rc := step.getRunContext()
|
|
|
|
env := step.getEnv()
|
|
// container env is the image's baseline, which job env and $GITHUB_ENV override
|
|
mergeIntoMap(step, env, rc.defaultEnv, rc.containerSpec.Env, rc.GetEnv())
|
|
rc.withGithubEnv(ctx, getStepGithubContext(ctx, step), *env)
|
|
|
|
var err error
|
|
exprEval := rc.NewExpressionEvaluator(ctx)
|
|
stepEnv := step.getStepModel().Environment()
|
|
if envNode := step.getStepModel().Env; strings.Contains(envNode.Value, "${{") { // only a whole-map expression has a value
|
|
if err := model.DecodeEvaluated("step env", envNode, exprEval.shared(ctx).EvaluateYamlNode, &stepEnv); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
for k, v := range stepEnv {
|
|
if stepEnv[k], err = exprEval.Interpolate(ctx, v); err != nil {
|
|
return fmt.Errorf("unable to interpolate env %s: %w", k, err)
|
|
}
|
|
}
|
|
}
|
|
// merge step env last, since it should not be overwritten
|
|
mergeIntoMap(step, step.getEnv(), stepEnv)
|
|
return nil
|
|
}
|
|
|
|
// setupInputs evaluates `with` once the condition passed, as GitHub does, so every consumer reads the evaluated With.
|
|
func setupInputs(ctx context.Context, step step) error {
|
|
stepModel := step.getStepModel()
|
|
if stepModel.RawWith.Kind == 0 {
|
|
return nil
|
|
}
|
|
if err := model.DecodeEvaluated("with", stepModel.RawWith, step.getRunContext().NewStepExpressionEvaluator(ctx, step).shared(ctx).EvaluateYamlNode, &stepModel.With); err != nil {
|
|
return err
|
|
}
|
|
mergeIntoMap(step, step.getEnv(), (&model.Step{With: stepModel.With}).GetEnv())
|
|
return nil
|
|
}
|
|
|
|
func isStepEnabled(ctx context.Context, expr string, step step, stage stepStage) (bool, error) {
|
|
rc := step.getRunContext()
|
|
|
|
var defaultStatusCheck exprparser.DefaultStatusCheck
|
|
if stage == stepStagePost {
|
|
defaultStatusCheck = exprparser.DefaultStatusCheckAlways
|
|
} else {
|
|
defaultStatusCheck = exprparser.DefaultStatusCheckSuccess
|
|
}
|
|
|
|
// success() and failure() in a composite's own main steps read the composite's result, other stages the job's
|
|
jobContext := rc.getJobContext()
|
|
if rc.Parent != nil && stage == stepStageMain && jobContext.Status != "cancelled" {
|
|
jobContext.Status = rc.ownStatus()
|
|
}
|
|
runStep, err := EvalBool(ctx, rc.newExpressionEvaluator(ctx, *step.getEnv(), jobContext, rc.getGithubContext(ctx), "step"), expr, defaultStatusCheck)
|
|
if err != nil {
|
|
return false, fmt.Errorf("if-expression %q evaluation failed: %s", expr, err)
|
|
}
|
|
|
|
return runStep, nil
|
|
}
|
|
|
|
func isContinueOnError(ctx context.Context, expr string, step step) (bool, error) {
|
|
// https://github.com/github/docs/blob/3ae84420bd10997bb5f35f629ebb7160fe776eae/content/actions/reference/workflow-syntax-for-github-actions.md?plain=true#L962
|
|
if strings.TrimSpace(expr) == "" {
|
|
return false, nil
|
|
}
|
|
|
|
rc := step.getRunContext()
|
|
|
|
continueOnError, err := EvalBool(ctx, rc.NewStepExpressionEvaluator(ctx, step), expr, exprparser.DefaultStatusCheckNone)
|
|
if err != nil {
|
|
return false, fmt.Errorf("continue-on-error expression %q evaluation failed: %s", expr, err)
|
|
}
|
|
|
|
return continueOnError, nil
|
|
}
|
|
|
|
func mergeIntoMap(step step, target *map[string]string, maps ...map[string]string) {
|
|
if rc := step.getRunContext(); rc != nil && rc.JobContainer != nil && rc.JobContainer.IsEnvironmentCaseInsensitive() {
|
|
mergeIntoMapCaseInsensitive(*target, maps...)
|
|
} else {
|
|
mergeIntoMapCaseSensitive(*target, maps...)
|
|
}
|
|
}
|
|
|
|
func mergeIntoMapCaseSensitive(target map[string]string, maps ...map[string]string) {
|
|
for _, m := range maps {
|
|
maps0.Copy(target, m)
|
|
}
|
|
}
|
|
|
|
func mergeIntoMapCaseInsensitive(target map[string]string, maps ...map[string]string) {
|
|
foldKeys := make(map[string]string, len(target))
|
|
for k := range target {
|
|
foldKeys[strings.ToLower(k)] = k
|
|
}
|
|
for _, m := range maps {
|
|
for key, value := range m {
|
|
foldKey := strings.ToLower(key)
|
|
if previous, ok := foldKeys[foldKey]; ok {
|
|
delete(target, previous)
|
|
}
|
|
foldKeys[foldKey] = key
|
|
target[key] = value
|
|
}
|
|
}
|
|
}
|
|
|
|
func symlinkJoin(filename, sym, parent string) (string, error) {
|
|
dest := path.Join(path.Dir(filename), sym)
|
|
prefix := path.Clean(parent) + "/"
|
|
if strings.HasPrefix(dest, prefix) || prefix == "./" {
|
|
return dest, nil
|
|
}
|
|
return "", fmt.Errorf("symlink tries to access file '%s' outside of '%s'", strings.ReplaceAll(dest, "'", "''"), strings.ReplaceAll(parent, "'", "''"))
|
|
}
|