mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-03-02 18:00:18 +00:00
feat: Support graceful job step cancellation (#69)
* for gh-act-runner * act-cli support as well * respecting always() and cancelled() of steps * setup-job, bug report, gh cli and watch wait call is cancelled early
This commit is contained in:
@@ -51,6 +51,7 @@ type RunContext struct {
|
||||
Masks []string
|
||||
cleanUpJobContainer common.Executor
|
||||
caller *caller // job calling this RunContext (reusable workflows)
|
||||
Cancelled bool
|
||||
nodeToolFullPath string
|
||||
}
|
||||
|
||||
@@ -435,6 +436,8 @@ func (rc *RunContext) execJobContainer(cmd []string, env map[string]string, user
|
||||
|
||||
func (rc *RunContext) InitializeNodeTool() common.Executor {
|
||||
return func(ctx context.Context) error {
|
||||
ctx, cancel := common.EarlyCancelContext(ctx)
|
||||
defer cancel()
|
||||
rc.GetNodeToolFullPath(ctx)
|
||||
return nil
|
||||
}
|
||||
@@ -660,6 +663,8 @@ func (rc *RunContext) interpolateOutputs() common.Executor {
|
||||
|
||||
func (rc *RunContext) startContainer() common.Executor {
|
||||
return func(ctx context.Context) error {
|
||||
ctx, cancel := common.EarlyCancelContext(ctx)
|
||||
defer cancel()
|
||||
if rc.IsHostEnv(ctx) {
|
||||
return rc.startHostEnvironment()(ctx)
|
||||
}
|
||||
@@ -863,10 +868,14 @@ func trimToLen(s string, l int) string {
|
||||
|
||||
func (rc *RunContext) getJobContext() *model.JobContext {
|
||||
jobStatus := "success"
|
||||
for _, stepStatus := range rc.StepResults {
|
||||
if stepStatus.Conclusion == model.StepStatusFailure {
|
||||
jobStatus = "failure"
|
||||
break
|
||||
if rc.Cancelled {
|
||||
jobStatus = "cancelled"
|
||||
} else {
|
||||
for _, stepStatus := range rc.StepResults {
|
||||
if stepStatus.Conclusion == model.StepStatusFailure {
|
||||
jobStatus = "failure"
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return &model.JobContext{
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/actions-oss/act-cli/pkg/container"
|
||||
"github.com/actions-oss/act-cli/pkg/exprparser"
|
||||
"github.com/actions-oss/act-cli/pkg/model"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type step interface {
|
||||
@@ -84,6 +85,9 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
||||
return err
|
||||
}
|
||||
|
||||
cctx := common.JobCancelContext(ctx)
|
||||
rc.Cancelled = cctx != nil && cctx.Err() != nil
|
||||
|
||||
runStep, err := isStepEnabled(ctx, ifExpression, step, stage)
|
||||
if err != nil {
|
||||
stepResult.Conclusion = model.StepStatusFailure
|
||||
@@ -139,9 +143,13 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
||||
Mode: 0o666,
|
||||
})(ctx)
|
||||
|
||||
timeoutctx, cancelTimeOut := evaluateStepTimeout(ctx, rc.ExprEval, stepModel)
|
||||
stepCtx, cancelStepCtx := context.WithCancel(ctx)
|
||||
defer cancelStepCtx()
|
||||
var cancelTimeOut context.CancelFunc
|
||||
stepCtx, cancelTimeOut = evaluateStepTimeout(stepCtx, rc.ExprEval, stepModel)
|
||||
defer cancelTimeOut()
|
||||
err = executor(timeoutctx)
|
||||
monitorJobCancellation(ctx, stepCtx, cctx, rc, logger, ifExpression, step, stage, cancelStepCtx)
|
||||
err = executor(stepCtx)
|
||||
|
||||
if err == nil {
|
||||
logger.WithField("stepResult", stepResult.Outcome).Infof(" \u2705 Success - %s %s", stage, stepString)
|
||||
@@ -189,6 +197,24 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo
|
||||
}
|
||||
}
|
||||
|
||||
func monitorJobCancellation(ctx context.Context, stepCtx context.Context, jobCancellationCtx context.Context, rc *RunContext, logger logrus.FieldLogger, ifExpression string, step step, stage stepStage, cancelStepCtx context.CancelFunc) {
|
||||
if !rc.Cancelled && jobCancellationCtx != nil {
|
||||
go func() {
|
||||
select {
|
||||
case <-jobCancellationCtx.Done():
|
||||
rc.Cancelled = true
|
||||
logger.Infof("Reevaluate condition %v due to cancellation", ifExpression)
|
||||
keepStepRunning, err := isStepEnabled(ctx, ifExpression, step, stage)
|
||||
logger.Infof("Result condition keepStepRunning=%v", keepStepRunning)
|
||||
if !keepStepRunning || err != nil {
|
||||
cancelStepCtx()
|
||||
}
|
||||
case <-stepCtx.Done():
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func evaluateStepTimeout(ctx context.Context, exprEval ExpressionEvaluator, stepModel *model.Step) (context.Context, context.CancelFunc) {
|
||||
timeout := exprEval.Interpolate(ctx, stepModel.TimeoutMinutes)
|
||||
if timeout != "" {
|
||||
|
||||
Reference in New Issue
Block a user