Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deferred): add EXIT_CODE to deferred tasks #1967

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 4 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions testdata/deferred/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading