-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Conditionals example improved with complex syntax (#5467)
Co-authored-by: Simon Behar <simbeh7@gmail.com>
- Loading branch information
Showing
3 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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,68 @@ | ||
# In this example we flip 2 times a coin. First time we | ||
# use the simple conditionals syntax. The second time we use | ||
# regex and a complex condition with logical AND and OR. | ||
# We also use of the parenthesis for defining the priority. | ||
apiVersion: argoproj.io/v1alpha1 | ||
kind: Workflow | ||
metadata: | ||
generateName: coinflip- | ||
spec: | ||
entrypoint: coinflip | ||
templates: | ||
- name: coinflip | ||
steps: | ||
# flip a coin | ||
- - name: flip-coin | ||
template: flip-coin | ||
# evaluate the result in parallel | ||
- - name: heads | ||
template: heads # call heads template if "heads" | ||
when: "{{steps.flip-coin.outputs.result}} == heads" | ||
- name: tails | ||
template: tails # call tails template if "tails" | ||
when: "{{steps.flip-coin.outputs.result}} == tails" | ||
- - name: flip-again | ||
template: flip-coin | ||
- - name: complex-condition | ||
template: heads-tails-or-twice-tails | ||
# call heads template if first flip was "heads" and second was "tails" OR both were "tails" | ||
when: >- | ||
( {{steps.flip-coin.outputs.result}} == heads && | ||
{{steps.flip-again.outputs.result}} == tails | ||
) || | ||
( {{steps.flip-coin.outputs.result}} == tails && | ||
{{steps.flip-again.outputs.result}} == tails ) | ||
- name: heads-regex | ||
template: heads # call heads template if ~ "hea" | ||
when: "{{steps.flip-again.outputs.result}} =~ hea" | ||
- name: tails-regex | ||
template: tails # call heads template if ~ "tai" | ||
when: "{{steps.flip-again.outputs.result}} =~ tai" | ||
|
||
# Return heads or tails based on a random number | ||
- name: flip-coin | ||
script: | ||
image: python:alpine3.6 | ||
command: [python] | ||
source: | | ||
import random | ||
result = "heads" if random.randint(0,1) == 0 else "tails" | ||
print(result) | ||
- name: heads | ||
container: | ||
image: alpine:3.6 | ||
command: [sh, -c] | ||
args: ["echo \"it was heads\""] | ||
|
||
- name: tails | ||
container: | ||
image: alpine:3.6 | ||
command: [sh, -c] | ||
args: ["echo \"it was tails\""] | ||
|
||
- name: heads-tails-or-twice-tails | ||
container: | ||
image: alpine:3.6 | ||
command: [sh, -c] | ||
args: ["echo \"it was heads the first flip and tails the second. Or it was two times tails.\""] |