Skip to content

Commit 88b6881

Browse files
authored
Add ability to get task output parameters, more syntactic sugar around complex dependencies, and easy way to define env variables (#517)
Signed-off-by: Flaviu Vadan <[email protected]>
1 parent 28d5f43 commit 88b6881

21 files changed

+932
-20
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Complex Deps
2+
3+
4+
5+
6+
7+
8+
=== "Hera"
9+
10+
```python linenums="1"
11+
from hera.workflows import DAG, Workflow, script
12+
13+
14+
@script()
15+
def foo(p):
16+
if p < 0.5:
17+
raise Exception(p)
18+
print(42)
19+
20+
21+
with Workflow(generate_name="complex-deps-", entrypoint="d") as w:
22+
with DAG(name="d"):
23+
A = foo(name="a", arguments={"p": 0.6})
24+
B = foo(name="b", arguments={"p": 0.3})
25+
C = foo(name="c", arguments={"p": 0.7})
26+
D = foo(name="d", arguments={"p": 0.9})
27+
# here, D depends on A, B, and C. If A succeeds and one of B or C fails, D still runs
28+
A >> [B, C], [A, (B | C)] >> D
29+
```
30+
31+
=== "YAML"
32+
33+
```yaml linenums="1"
34+
apiVersion: argoproj.io/v1alpha1
35+
kind: Workflow
36+
metadata:
37+
generateName: complex-deps-
38+
spec:
39+
entrypoint: d
40+
templates:
41+
- dag:
42+
tasks:
43+
- arguments:
44+
parameters:
45+
- name: p
46+
value: '0.6'
47+
name: a
48+
template: foo
49+
- arguments:
50+
parameters:
51+
- name: p
52+
value: '0.3'
53+
depends: a
54+
name: b
55+
template: foo
56+
- arguments:
57+
parameters:
58+
- name: p
59+
value: '0.7'
60+
depends: a
61+
name: c
62+
template: foo
63+
- arguments:
64+
parameters:
65+
- name: p
66+
value: '0.9'
67+
depends: a && (b || c)
68+
name: d
69+
template: foo
70+
name: d
71+
- inputs:
72+
parameters:
73+
- name: p
74+
name: foo
75+
script:
76+
command:
77+
- python
78+
image: python:3.7
79+
source: "import os\nimport sys\nsys.path.append(os.getcwd())\nimport json\n\
80+
try: p = json.loads(r'''{{inputs.parameters.p}}''')\nexcept: p = r'''{{inputs.parameters.p}}'''\n\
81+
\nif p < 0.5:\n raise Exception(p)\nprint(42)\n"
82+
```
83+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Dag With Param Passing
2+
3+
4+
5+
6+
7+
8+
=== "Hera"
9+
10+
```python linenums="1"
11+
from hera.workflows import DAG, Container, Parameter, Task, Workflow
12+
13+
with Workflow(generate_name="param-passing-", entrypoint="d") as w:
14+
out = Container(
15+
name="out",
16+
image="docker/whalesay:latest",
17+
command=["cowsay"],
18+
outputs=Parameter(name="x", value=42),
19+
)
20+
in_ = Container(
21+
name="in",
22+
image="docker/whalesay:latest",
23+
command=["cowsay"],
24+
args=["{{inputs.parameters.a}}"],
25+
inputs=Parameter(name="a"),
26+
)
27+
with DAG(name="d"):
28+
t1 = Task(name="a", template=out)
29+
t2 = Task(name="b", template=in_, arguments=t1.get_parameter("x").with_name("a"))
30+
t1 >> t2
31+
```
32+
33+
=== "YAML"
34+
35+
```yaml linenums="1"
36+
apiVersion: argoproj.io/v1alpha1
37+
kind: Workflow
38+
metadata:
39+
generateName: param-passing-
40+
spec:
41+
entrypoint: d
42+
templates:
43+
- container:
44+
command:
45+
- cowsay
46+
image: docker/whalesay:latest
47+
name: out
48+
outputs:
49+
parameters:
50+
- name: x
51+
value: '42'
52+
- container:
53+
args:
54+
- '{{inputs.parameters.a}}'
55+
command:
56+
- cowsay
57+
image: docker/whalesay:latest
58+
inputs:
59+
parameters:
60+
- name: a
61+
name: in
62+
- dag:
63+
tasks:
64+
- name: a
65+
template: out
66+
- arguments:
67+
parameters:
68+
- name: a
69+
value: '{{tasks.a.outputs.parameters.x}}'
70+
depends: a
71+
name: b
72+
template: in
73+
name: d
74+
```
75+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Dag With Script Param Passing
2+
3+
4+
5+
6+
7+
8+
=== "Hera"
9+
10+
```python linenums="1"
11+
from hera.workflows import DAG, Parameter, Task, Workflow, script
12+
13+
14+
@script()
15+
def out():
16+
print(42)
17+
18+
19+
@script()
20+
def in_(a):
21+
print(a)
22+
23+
24+
with Workflow(generate_name="script-param-passing-", entrypoint="d") as w:
25+
with DAG(name="d"):
26+
t1: Task = out()
27+
t2 = in_(arguments=Parameter(name="a", value=t1.result))
28+
t1 >> t2
29+
```
30+
31+
=== "YAML"
32+
33+
```yaml linenums="1"
34+
apiVersion: argoproj.io/v1alpha1
35+
kind: Workflow
36+
metadata:
37+
generateName: script-param-passing-
38+
spec:
39+
entrypoint: d
40+
templates:
41+
- dag:
42+
tasks:
43+
- name: out
44+
template: out
45+
- arguments:
46+
parameters:
47+
- name: a
48+
value: '{{tasks.out.outputs.result}}'
49+
depends: out
50+
name: in-
51+
template: in-
52+
name: d
53+
- name: out
54+
script:
55+
command:
56+
- python
57+
image: python:3.7
58+
source: 'import os
59+
60+
import sys
61+
62+
sys.path.append(os.getcwd())
63+
64+
print(42)
65+
66+
'
67+
- inputs:
68+
parameters:
69+
- name: a
70+
name: in-
71+
script:
72+
command:
73+
- python
74+
image: python:3.7
75+
source: 'import os
76+
77+
import sys
78+
79+
sys.path.append(os.getcwd())
80+
81+
import json
82+
83+
try: a = json.loads(r''''''{{inputs.parameters.a}}'''''')
84+
85+
except: a = r''''''{{inputs.parameters.a}}''''''
86+
87+
88+
print(a)
89+
90+
'
91+
```
92+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Multi Env
2+
3+
4+
5+
6+
7+
8+
=== "Hera"
9+
10+
```python linenums="1"
11+
from hera.workflows import DAG, Workflow, script
12+
13+
14+
@script(env={"a": 1, "b": 2, "c": 3})
15+
def env():
16+
import os
17+
18+
# note that env params come in as strings
19+
assert os.environ["a"] == "1", os.environ["a"]
20+
assert os.environ["b"] == "2", os.environ["b"]
21+
assert os.environ["c"] == "3", os.environ["c"]
22+
23+
24+
with Workflow(generate_name="multi-env-", entrypoint="d") as w:
25+
with DAG(name="d"):
26+
env()
27+
```
28+
29+
=== "YAML"
30+
31+
```yaml linenums="1"
32+
apiVersion: argoproj.io/v1alpha1
33+
kind: Workflow
34+
metadata:
35+
generateName: multi-env-
36+
spec:
37+
entrypoint: d
38+
templates:
39+
- dag:
40+
tasks:
41+
- name: env
42+
template: env
43+
name: d
44+
- name: env
45+
script:
46+
command:
47+
- python
48+
env:
49+
- name: a
50+
value: '1'
51+
- name: b
52+
value: '2'
53+
- name: c
54+
value: '3'
55+
image: python:3.7
56+
source: 'import os
57+
58+
import sys
59+
60+
sys.path.append(os.getcwd())
61+
62+
import os
63+
64+
65+
# note that env params come in as strings
66+
67+
assert os.environ["a"] == "1", os.environ["a"]
68+
69+
assert os.environ["b"] == "2", os.environ["b"]
70+
71+
assert os.environ["c"] == "3", os.environ["c"]
72+
73+
'
74+
```
75+

0 commit comments

Comments
 (0)