Call an HTTP API and Use the Answer
This WorkflowRun asks the GitHub API about the kubevela/workflow repository, builds a sentence from the reply, and hands it to the next step. If the call fails, a different step runs instead. The same pattern sends the result on to Slack or a webhook: swap the last step for a notification step.
Needs: the vela-workflow controller (Install), and a cluster that can reach api.github.com.
The WorkflowRun
apiVersion: core.oam.dev/v1alpha1
kind: WorkflowRun
metadata:
name: github-stars
namespace: default
spec:
workflowSpec:
steps:
- name: request
type: request
properties:
url: https://api.github.com/repos/kubevela/workflow
timeout: 10s
outputs:
- name: stars
valueFrom: '"\(response.full_name) has \(response.stargazers_count) stars"'
- name: report
type: print-message-in-status
inputs:
- from: stars
parameterKey: message
- name: report-failure
type: print-message-in-status
if: status.request.failed
properties:
message: "Could not read the repository from GitHub"
requestsends a GET and waits for the reply. It parses a JSON body intoresponse, whichvalueFromreads: here, CUE string interpolation over two of its fields.reporttakes the output as itsmessage, throughinputs.report-failurehas anif, so it runs when the condition holds rather than only when everything before it succeeded.
Run it
kubectl apply -f github-stars.yaml
kubectl get workflowrun github-stars
NAME PHASE AGE
github-stars succeeded 6s
Each step's phase and message:
kubectl get workflowrun github-stars \
-o jsonpath='{range .status.steps[*]}{.name}{"\t"}{.phase}{"\t"}{.message}{"\n"}{end}'
request succeeded
report succeeded kubevela/workflow has 127 stars
report-failure skipped
The star count is whatever GitHub says when you run it.
When the call fails
Point the URL at a repository that doesn't exist, https://api.github.com/repos/kubevela/no-such-repo, and name the run github-stars-missing. GitHub answers 404, the request step fails, report is skipped since it needs request to succeed, and report-failure runs:
NAME PHASE AGE
github-stars-missing failed 6s
request failed request of https://api.github.com/repos/kubevela/no-such-repo is fail: 404
report skipped
report-failure succeeded Could not read the repository from GitHub
The run is failed because one of its steps failed, even though the fallback ran.
Clean up
kubectl delete workflowrun github-stars github-stars-missing
Read more
- Data Passing:
outputs,inputsandvalueFrom. - If Conditions: every variable an
ifcan read. - Workflow Outbound HTTP Protection: which destinations an HTTP step may call, and the timeout and size limits.