task
task copied to clipboard
Add global preconditions
It would be great to be able to set global preconditions to prevent running any tasks in a Taskfile and its children if the precondition is not met. This would be a simple way to add a precondition to all tasks without having to add the same precondition to every task individually.
The use case for this is for example to enforce that a repository has been initialized somehow before allowing to run any other tasks, like setting up release targets for goreleaser or similar. In this example I would have the .goreleaser.yml contain a string like "INIT_PACKAGE" which should be replaced by the package name of the repository, and until that's done all tasks should fail (precondition greps for "INIT_PACKAGE"), and return the same message "This repository hasn't been initialized, please run 'task init'".
Obviously to be able to run the initial task init
there needs to be a property to disable the global precondition for that specific task. So the Taskfile would end up something like this:
version: '2'
preconditions:
- grep INIT_PACKAGE .goreleaser.yml
msg: "this repository needs to be initialized, run 'task init' to continue"
tasks:
build:
desc: build go project
cmds:
- go build .
init:
desc: initalize repository
cmds:
- sed -i 's/INIT_PACKAGE/myPackage/g' .goreleaser.yml
ignore_preconditions: yes
Also the logic should probably be that any preconditions from the parent Taskfile which includes other taskfiles should be applied on all the child taskfiles' tasks as well, i.e. they inherit the parent's global preconditions. The preconditions should only be propagated downwards in the hierarchy meaning that the parent Taskfile doesn't inherit global preconditions from it's children. Or maybe there should be a property to select this behavior as well, "include_preconditions: yes/no"?
Hi @tommyalatalo,
This seems like an useful feature request, but shouldn't be my priority for the near future, unless someone else decides to contribute with this.
Since #405 is a duplication, let me paste my workaround [1, 2] here too:
There is already a way to reuse preconditions by the use of YAML anchors:
tasks:
task1:
cmds:
- "echo task1"
preconditions:
- &test-precondition
sh: "[ 1 = 0 ]"
msg: "One doesn't equal Zero, Halting"
task2:
cmds:
- "echo task2"
preconditions:
- *test-precondition
task3:
cmds:
- "echo task3"
preconditions:
- *test-precondition
However, the anchor have to be on the same YAML file. Adding global preconditions would only reduce duplications, if they are used on multiple imported taskfiles.
Since we do not use strict YAML unmarshal, even this is possible:
preconditions:
<<: &test-precondition
sh: "[ 1 = 0 ]"
msg: "One doesn't equal Zero, Halting"
tasks:
task1:
cmds:
- "echo task1"
preconditions:
- *test-precondition
task2:
cmds:
- "echo task2"
preconditions:
- *test-precondition
task3:
cmds:
- "echo task3"
preconditions:
- *test-precondition
I'm aware of yaml anchors, but as you said yourself, it doesn't work across multiple taskfiles, and it requires a lot of duplication which is error prone.
I'm aware of yaml anchors, but as you said yourself, it doesn't work across multiple taskfiles, and it requires a lot of duplication which is error prone.
sure, I understand. I prefer a proper solution for this issue, too. This is just a comment for anyone who is not aware of yaml anchors and are looking for a workaround solution.