diff --git a/task.go b/task.go index b1830142..a4fa2cae 100644 --- a/task.go +++ b/task.go @@ -342,6 +342,22 @@ func (e *Executor) runDeferred(t *ast.Task, call *ast.Call, i int, deferredExitC cmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, extra) + if cmd.Vars != nil && cmd.Vars.Len() > 0 { + for _, key := range cmd.Vars.Keys() { + if v := cmd.Vars.Get(key); v.Value != "" || v.Sh != nil { + // Handle Value field + if str, isStr := v.Value.(string); isStr { + v.Value = templater.ReplaceWithExtra(str, cache, extra) + } + // Handle Sh field if it exists + if v.Sh != nil { + *v.Sh = templater.ReplaceWithExtra(*v.Sh, cache, extra) + } + cmd.Vars.Set(key, v) + } + } + } + if err := e.runCommand(ctx, t, call, i); err != nil { e.Logger.VerboseErrf(logger.Yellow, "task: ignored error in deferred cmd: %s\n", err.Error()) } diff --git a/task_test.go b/task_test.go index ef039d3b..1e7ab434 100644 --- a/task_test.go +++ b/task_test.go @@ -2238,6 +2238,10 @@ task: [task-2] exit 1 task: [task-2] echo 'failing' && exit 2 failing echo ran +task: [task-1] echo 'task-1 ran Variable with SH - EXIT_CODE : 1' +task-1 ran Variable with SH - EXIT_CODE : 1 +task: [task-1] echo 'task-1 ran EXIT_CODE : 1' +task-1 ran EXIT_CODE : 1 task-1 ran successfully task: [task-1] echo 'task-1 ran successfully' task-1 ran successfully diff --git a/testdata/deferred/Taskfile.yml b/testdata/deferred/Taskfile.yml index b193117c..f393f429 100644 --- a/testdata/deferred/Taskfile.yml +++ b/testdata/deferred/Taskfile.yml @@ -2,13 +2,24 @@ version: '3' tasks: task-1: - - echo 'task-1 ran {{.PARAM}}' + cmds: + - echo 'task-1 ran {{.PARAM}}' task-2: - - defer: { task: 'task-1', vars: { PARAM: 'successfully' } } - - defer: { task: 'task-1', vars: { PARAM: 'successfully' }, silent: true } - - defer: echo 'echo ran' - silent: true - - defer: echo 'failing' && exit 2 - - echo 'cmd ran' - - exit 1 + cmds: + - defer: { task: 'task-1', vars: { PARAM: 'successfully' } } + - defer: { task: 'task-1', vars: { PARAM: 'successfully' }, silent: true } + - defer: + task: 'task-1' + vars: + PARAM: "{{if .EXIT_CODE}}EXIT_CODE : {{.EXIT_CODE}}{{else}}Success{{end}}" + - defer: + task: 'task-1' + vars: + PARAM: + sh: "{{if .EXIT_CODE}}echo Variable with SH - EXIT_CODE : {{.EXIT_CODE}}{{else}}echo Variable with SH : Success{{end}}" + - defer: echo 'echo ran' + silent: true + - defer: echo 'failing' && exit 2 + - echo 'cmd ran' + - exit 1