-
-
Notifications
You must be signed in to change notification settings - Fork 774
feat: add conditional execution for tasks and commands #2564
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
Open
vmaerten
wants to merge
5
commits into
main
Choose a base branch
from
if
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+443
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f140d9a
feat(if): add conditional execution for tasks and commands
vmaerten 39d9ca7
test(if): add tests for task calls and go templates
vmaerten f6fd264
test(if): add golden fixture tests for if conditions
vmaerten d778576
docs(if): add documentation for conditional execution
vmaerten c30adc4
fix docs
vmaerten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| version: '3' | ||
|
|
||
| vars: | ||
| SHOULD_RUN: "yes" | ||
| ENV: "prod" | ||
| FEATURE_ENABLED: "true" | ||
| FEATURE_DISABLED: "false" | ||
|
|
||
| tasks: | ||
| # Basic command-level if (condition met) | ||
| cmd-if-true: | ||
| cmds: | ||
| - cmd: echo "executed" | ||
| if: "true" | ||
|
|
||
| # Basic command-level if (condition not met) | ||
| cmd-if-false: | ||
| cmds: | ||
| - cmd: echo "should not appear" | ||
| if: "false" | ||
| - echo "this runs" | ||
|
|
||
| # Task-level if (condition met) | ||
| task-if-true: | ||
| if: "true" | ||
| cmds: | ||
| - echo "task executed" | ||
|
|
||
| # Task-level if (condition not met) | ||
| task-if-false: | ||
| if: "false" | ||
| cmds: | ||
| - echo "should not appear" | ||
|
|
||
| # With template variables | ||
| if-with-template: | ||
| cmds: | ||
| - cmd: echo "Running because SHOULD_RUN={{.SHOULD_RUN}}" | ||
| if: '[ "{{.SHOULD_RUN}}" = "yes" ]' | ||
|
|
||
| # If inside for loop | ||
| if-in-for-loop: | ||
| cmds: | ||
| - for: ["a", "b", "c"] | ||
| cmd: echo "processing {{.ITEM}}" | ||
| if: '[ "{{.ITEM}}" != "b" ]' | ||
|
|
||
| # If on task call | ||
| if-on-task-call: | ||
| cmds: | ||
| - task: subtask | ||
| if: "true" | ||
|
|
||
| subtask: | ||
| internal: true | ||
| cmds: | ||
| - echo "subtask ran" | ||
|
|
||
| # If combined with platforms (both must pass) | ||
| if-with-platforms: | ||
| cmds: | ||
| - cmd: echo "condition and platform met" | ||
| platforms: [linux, darwin, windows] | ||
| if: "true" | ||
|
|
||
| # Skip task call | ||
| skip-task-call: | ||
| cmds: | ||
| - task: subtask | ||
| if: "false" | ||
| - echo "after skipped task call" | ||
|
|
||
| # Task call in cmds with if condition met | ||
| task-call-if-true: | ||
| cmds: | ||
| - task: subtask | ||
| if: "true" | ||
| - echo "after task call" | ||
|
|
||
| # Task call in cmds with if condition not met | ||
| task-call-if-false: | ||
| cmds: | ||
| - task: subtask | ||
| if: "false" | ||
| - echo "continues after skipped task" | ||
|
|
||
| # Template eq - condition met | ||
| template-eq-true: | ||
| cmds: | ||
| - cmd: echo "env is prod" | ||
| if: '{{ eq .ENV "prod" }}' | ||
|
|
||
| # Template eq - condition not met | ||
| template-eq-false: | ||
| cmds: | ||
| - cmd: echo "should not appear" | ||
| if: '{{ eq .ENV "dev" }}' | ||
| - echo "this runs" | ||
|
|
||
| # Template ne (not equal) | ||
| template-ne: | ||
| cmds: | ||
| - cmd: echo "env is not dev" | ||
| if: '{{ ne .ENV "dev" }}' | ||
|
|
||
| # Template with boolean-like variable | ||
| template-bool-true: | ||
| cmds: | ||
| - cmd: echo "feature enabled" | ||
| if: '{{ eq .FEATURE_ENABLED "true" }}' | ||
|
|
||
| # Template with boolean-like variable (false) | ||
| template-bool-false: | ||
| cmds: | ||
| - cmd: echo "should not appear" | ||
| if: '{{ eq .FEATURE_DISABLED "true" }}' | ||
| - echo "feature was disabled" | ||
|
|
||
| # Direct true/false from template | ||
| template-direct-true: | ||
| cmds: | ||
| - cmd: echo "direct true works" | ||
| if: '{{ .FEATURE_ENABLED }}' | ||
|
|
||
| # Direct true/false from template (false case) | ||
| template-direct-false: | ||
| cmds: | ||
| - cmd: echo "should not appear" | ||
| if: '{{ .FEATURE_DISABLED }}' | ||
| - echo "direct false skipped correctly" | ||
|
|
||
| # Template with CLI variable override | ||
| template-cli-var: | ||
| cmds: | ||
| - cmd: echo "MY_VAR is yes" | ||
| if: '{{ eq .MY_VAR "yes" }}' | ||
|
|
||
| # Combined template conditions with and | ||
| template-and: | ||
| cmds: | ||
| - cmd: echo "both conditions met" | ||
| if: '{{ and (eq .ENV "prod") (eq .FEATURE_ENABLED "true") }}' | ||
|
|
||
| # Combined template conditions with or | ||
| template-or: | ||
| cmds: | ||
| - cmd: echo "at least one condition met" | ||
| if: '{{ or (eq .ENV "dev") (eq .ENV "prod") }}' | ||
|
|
||
| # Task-level if with template | ||
| task-level-template: | ||
| if: '{{ eq .ENV "prod" }}' | ||
| cmds: | ||
| - echo "task runs in prod" | ||
|
|
||
| # Task-level if with template (not met) | ||
| task-level-template-false: | ||
| if: '{{ eq .ENV "dev" }}' | ||
| cmds: | ||
| - echo "should not appear" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| this runs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| executed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| task: "if-in-for-loop" started | ||
| task: [if-in-for-loop] echo "processing a" | ||
| processing a | ||
| task: [if-in-for-loop] if condition not met - skipped | ||
| task: [if-in-for-loop] echo "processing c" | ||
| processing c | ||
| task: "if-in-for-loop" finished |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| task: "task-call-if-false" started | ||
| task: [task-call-if-false] if condition not met - skipped | ||
| task: [task-call-if-false] echo "continues after skipped task" | ||
| continues after skipped task | ||
| task: "task-call-if-false" finished |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| subtask ran | ||
| after task call |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| task: "task-if-false" if condition not met - skipped |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| task executed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| task: "task-level-template-false" if condition not met - skipped |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| task runs in prod |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| both conditions met |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| feature was disabled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| feature enabled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| MY_VAR is yes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| direct false skipped correctly |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| direct true works |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| task: "template-eq-false" started | ||
| task: [template-eq-false] if condition not met - skipped | ||
| task: [template-eq-false] echo "this runs" | ||
| this runs | ||
| task: "template-eq-false" finished |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| env is prod |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| env is not dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| at least one condition met |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes people ask to have Posix and Bash opt available on these commands. I don't know if that is a good or a bad idea, never the less please see #2538 where the idea is in draft form.