Run a Workflow Template per Environment
A Workflow holds steps without running them. Each WorkflowRun that names it in workflowRef runs those steps, with its own context. Here one template configures an application for either environment, and if on context.env picks the steps that apply.
Needs: the vela-workflow controller (Install).
The template
apiVersion: core.oam.dev/v1alpha1
kind: Workflow
metadata:
name: configure-env
namespace: default
steps:
- name: dev-config
type: apply-object
if: context.env == "dev"
properties:
value:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-dev
namespace: default
data:
logLevel: debug
- name: prod-config
type: apply-object
if: context.env == "prod"
properties:
value:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-prod
namespace: default
data:
logLevel: warn
- name: done
type: print-message-in-status
properties:
message: configured
A Workflow has steps at the top level, not under spec, and an optional mode.
Two runs
apiVersion: core.oam.dev/v1alpha1
kind: WorkflowRun
metadata:
name: configure-dev
namespace: default
spec:
context:
env: dev
workflowRef: configure-env
---
apiVersion: core.oam.dev/v1alpha1
kind: WorkflowRun
metadata:
name: configure-prod
namespace: default
spec:
context:
env: prod
workflowRef: configure-env
Run it
kubectl apply -f configure-env.yaml
kubectl apply -f configure-runs.yaml
kubectl get workflowrun configure-dev configure-prod
NAME PHASE AGE
configure-dev succeeded 4s
configure-prod succeeded 4s
Each run took its own branch and skipped the other:
for r in configure-dev configure-prod; do
echo "== $r"
kubectl get workflowrun $r -o jsonpath='{range .status.steps[*]}{.name}{"\t"}{.phase}{"\t"}{.message}{"\n"}{end}'
done
== configure-dev
dev-config succeeded
prod-config skipped
done succeeded configured
== configure-prod
dev-config skipped
prod-config succeeded
done succeeded configured
A skipped step doesn't stop the ones after it: done ran in both. Each run wrote its own ConfigMap, app-config-dev and app-config-prod.
Changing the template
Each reconcile reads the steps from the template, so an edit to configure-env reaches every WorkflowRun that hasn't finished, such as one waiting at a suspend step, as well as the next. A finished one keeps what it ran; to run it again with the new steps, use vela workflow restart configure-dev.
Clean up
kubectl delete workflowrun configure-dev configure-prod
kubectl delete workflow configure-env
kubectl delete configmap app-config-dev app-config-prod
Read more
- Context: the values a run carries, and how
spec.contextadds to them. - Run a Workflow without an Application:
workflowRef, and whichmodewins when both set one.