Using go template if statements for imports. #3030
includes:
example: "{{if .CI }}https://raw.githubusercontent.com/example/taskfiles/refs/heads/main/example.yml{{else}}../taskfiles/example.yml{{end}}"I'm wondering if something like this is possible. Essentially include a remote taskfile if running in CI, but when running locally just include a local taskfile. the local and remote one are the same file, but I don't always push my taskfile to main because I want to test it locally in another repo. |
Replies: 1 comment 1 reply
|
Yes. Include paths are passed through Task's Go-template renderer before Task resolves them, and environment variables are part of that template context. Your conditional can therefore work as written, with two practical adjustments: quote the whole scalar and make the CI test explicit. version: '3'
includes:
example: '{{ if eq .CI "true" }}https://raw.githubusercontent.com/example/taskfiles/<pinned-commit>/example.yml{{ else }}../taskfiles/example.yml{{ end }}'Then: # local
task example:some-task
# CI
CI=true task --yes example:some-taskWhy this works on current Task: while reading includes, Task merges the process environment with Taskfile variables, then calls Two cautions:
If your CI system sets If this resolves the local/CI include split, please mark it as the accepted answer so the template behavior is easier to find. |
Yes. Include paths are passed through Task's Go-template renderer before Task resolves them, and environment variables are part of that template context. Your conditional can therefore work as written, with two practical adjustments: quote the whole scalar and make the CI test explicit.
Then:
Why this works on current Task: while reading includes, Task merges the process environment with Taskfile variables, then calls
templater.Replace(i…