Templates and Steps
- Templates are the building blocks of a workflow, defined under
spec.templates - Work templates:
container,script,resource,suspend(alsocontainerSet,http,plugin) - Invocators:
steps,dag- call other templates and control order
Template types
container
- Most common, runs an image
- name: whalesay
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["hello workflow"]
resources:
requests: {memory: 64Mi, cpu: 250m}
limits: {memory: 128Mi, cpu: 500m}
env:
- name: WORKFLOW_NAME
value: "{{workflow.name}}"
- Resources and env are set per template, and env supports interpolation
script
- Like container but with inline
source; the script is saved to a file and passed tocommand - stdout is captured as
outputs.result
- name: gen
script:
image: python:3.9
command: [python]
source: |
import random
print(random.randint(1, 100))
- Parameters get substituted into
sourcebefore it runs:data = {{inputs.parameters.input-data}}
resource
- Create / patch / apply / delete / replace k8s resources
- Needs RBAC for the workflow SA on those resources
successCondition/failureConditionmake the step wait on the resource status
- name: create-job
resource:
action: create
successCondition: status.succeeded > 0
failureCondition: status.failed > 3
manifest: |
apiVersion: batch/v1
kind: Job
metadata:
generateName: workflow-job-
spec:
template:
spec:
containers:
- name: worker
image: alpine:latest
command: [echo, done]
restartPolicy: Never
action: deletejust needs a manifest identifying the resource
suspend
- Pauses until resumed (
argo resume <wf>) or duration expires
- name: approval-gate
suspend: {}
- name: wait
suspend:
duration: "1h"
dag
- Tasks with
dependencies, see dag-parallel.md
Steps
- List of lists
- each outer item runs sequentially
- steps inside the same inner list run in parallel
steps:
- - name: initialize
template: init-task
- - name: parallel-1 # these two run together
template: worker-task
arguments:
parameters: [{name: id, value: "1"}]
- name: parallel-2
template: worker-task
arguments:
parameters: [{name: id, value: "2"}]
- - name: finalize
template: final-task
Invoking templates
template: <name>plusarguments- Parameters and artifacts can be passed
- - name: create-artifact
template: create-file
- - name: use-artifact
template: read-file
arguments:
artifacts:
- name: input-file
from: "{{steps.create-artifact.outputs.artifacts.result}}"
- Loops:
withItems/withParamwith{{item}} - Templates can also be referenced from a
WorkflowTemplatewithtemplateRef
Inputs and outputs
Parameters
- Input parameter can have
default(used if not passed) orvalue
inputs:
parameters:
- name: name
default: "World"
- Output parameters read from a file (or
outputs.result)
outputs:
parameters:
- name: result
valueFrom:
path: /tmp/output.txt
- Consumed with
{{steps.generate.outputs.parameters.result}}
Artifacts
- Input artifact can come from git/http/s3 etc, mounted at
path
inputs:
artifacts:
- name: source-code
path: /src
git:
repo: https://github.com/example/repo.git
revision: "main"
- Output artifacts are files/dirs uploaded to the artifact repo
- archived as tgz by default,
archive: none: {}disables it
- archived as tgz by default,
outputs:
artifacts:
- name: build-output
path: /tmp/output
archive:
none: {}
- Parameters = small values, artifacts = files/dirs. See variables-artifacts.md
Retry
retryStrategyon a template
retryStrategy:
limit: 5
retryPolicy: OnFailure
backoff:
duration: "1s"
factor: 2
maxDuration: "1m"
- Backoff: 1s, 2s, 4s, 8s, … capped at
maxDuration retryPolicyAlways- any failure or errorOnFailure- main container exits non-zero (default)OnError- system errors (pod deleted/evicted, etc), not user code failuresOnTransientError- only transient errors (e.g. network/timeouts)
expressiondecides if retry happens, has access tolastRetry.*- if
retryPolicyis also set, both must allow the retry
- if
retryStrategy:
limit: 3
expression: "asInt(lastRetry.exitCode) != 2" # don't retry exit code 2
- Use exit codes to separate transient (1) from permanent (2) failures
Timeouts
- Template level:
timeout: "5m", applies per attempt - Workflow level:
spec.activeDeadlineSeconds - Combine: timeout per attempt + retry with backoff
- name: resilient
timeout: "2m"
retryStrategy:
limit: 3
retryPolicy: OnError
backoff:
duration: "10s"
factor: 2
maxDuration: "1m"
- Always use backoff for external calls
- Workflow deadline should cover all steps plus a buffer
Remember
-
Same inner list = parallel, different = sequential
-
{{inputs.parameters.name}}for inputs,{{steps.<step>.outputs.parameters.<name>}}for outputs -
Parameters are values, artifacts are files