argo workflows step之间传参

步骤间参数传递

  • 将当前 step 的结果导出为Output Parameter
  • 将前一个 step 的Output Parameter导入为当前步骤的Input Parameter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: output-parameter-
spec:
entrypoint: output-parameter
templates:
- name: output-parameter
steps:
- - name: generate-parameter
template: hello-world-to-file
- - name: consume-parameter
template: print-message
arguments:
parameters:
# 将第一步写入到文件中的信息读取出来,传递给下一个步骤
- name: message
value: "{{steps.generate-parameter.outputs.parameters.hello-param}}"

- name: hello-world-to-file
container:
image: busybox
command: [sh, -c]
args: ["echo -n Output And Input Test > /tmp/hello_world.txt"] # 将信息输出到文件中
outputs:
parameters:
- name: hello-param # name of output parameter
valueFrom:
path: /tmp/hello_world.txt # set the value of hello-param to the contents of this hello-world.txt

- name: print-message
inputs:
parameters:
- name: message
container:
image: busybox
command: [echo]
args: ["{{inputs.parameters.message}}"]

arguments.parameters中直接引用了之前步骤的Output Parameter,语法为 {{steps.$stepName.outputs.parameters.$parameterName}}

内置的 result 参数

除了手动导出的参数之外,ArgoWorkflow还会默认生成一个Output Parameter,就是result。和其他Output Parameter一样,可以通过 {{steps.$stepName.outputs.parameters.$parameterName}} 语法进行引用。这个result参数会捕获最大256KB的标准输出作为value

因此result包含以下内容:

  • 1)script 的运行结果
  • 2)容器的标准输出

只要是在容器中输出到标准输出的,内容都可以被result捕获。

script 的运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: scripts-bash-
spec:
entrypoint: bash-script-example
templates:
- name: bash-script-example
steps:
- - name: generate
template: gen-random-int-python
- - name: print
template: print-message
arguments:
parameters:
- name: message
value: "{{steps.generate.outputs.result}}" # 这里会将pythond的print信息收集,然后传递给下一个步骤作为输入信息

- name: gen-random-int-python
script:
image: python:alpine3.6
command: [python]
source: |
import random
i = random.randint(1, 100)
print(i)

- name: print-message
inputs:
parameters:
- name: message
container:
image: alpine:latest
command: [sh, -c]
args: ["echo result was: {{inputs.parameters.message}}"]

容器的标准输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: scripts-bash-
spec:
entrypoint: bash-script-example
templates:
- name: bash-script-example
steps:
- - name: generate
template: whalesay-logs
- - name: print
template: print-message
arguments:
parameters:
- name: message
value: "{{steps.generate.outputs.result}}" #将whalesay容器打印在控制台的logs收集并传递给下一个步骤作为输入

- name: whalesay-logs
container:
image: docker/whalesay
command: [sh, -c]
args: ["cowsay test container logs"]

- name: print-message
inputs:
parameters:
- name: message
container:
image: alpine:latest
command: [sh, -c]
args: ['echo result was: "{{inputs.parameters.message}}"'] #特殊处理上面容器打印的换行log信息

相关具体信息见,官方文档链接