K8S API for Pipeline

    1. It can manage multiple KubeVela Applications across multiple environments.
    2. It is not bound to Applications and can be used independently. For example, it can expand or shrink a set of resources, perform process-oriented canary publishing for an Application, and perform a set of operation and maintenance operations in batches.
    3. It is one-time and does not manage resources. Even if the pipeline is deleted, the created resources will not be deleted.
    4. It uses the same execution engine as the Application Workflow, which completely inherits the features of KubeVela’s lightweight workflow. Compared with the traditional container-based CI pipeline, KubeVela’s pipeline does not depend on containers, No additional computing resources are required.

    tip

    In order to better reuse the existing capabilities and ensure technical consistency, we split the workflow engine part of the original application workflow. Both in-application workflow and pipeline use this workflow engine as the underlying technology implementation. The application workflow is represented by the field in the application, and the pipeline is represented by the resource.

    This means that most of the workflow steps are common between the two, such as: suspend, notification, send HTTP request, read configuration, etc.

    However, in WorkflowRun, there is only the configuration of steps, and no configuration of components, traits, and policies. Therefore, steps related to components/traits/policy can only be used in in-app workflows, such as: deploying/updating components, traits, etc.

    WorkflowRun is the K8S API for pipeline. You can choose to execute an external Workflow template in the WorkflowRun or execute the steps in the WorkflowRun spec (if you declare both, the step in the WorkflowRun spec will override the content in the template). A WorkflowRun consists of the following:

    Status

    WorkflowRun has the following status:

    WorkflowRun Step Status

    WorkflowRun steps have the following status:

    The Failed Reason of WorkflowRun Step

    For steps that fail to execute, the message of the step status will display the failed message, and the reason will display the failed reason, which is divided into the following types:

    You can define execution mode in WorkflowRun or Workflow templates:

    1. mode:
    2. steps: <DAG or StepByStep>
    3. subSteps: <DAG or StepByStep>

    If not explicitly specified, the WorkflowRun will execute the steps sequentially (StepByStep) and execute sub-steps in parallel (DAG) by default.

    K8S API for Pipeline - 图2caution

    If you specify the execution mode in both WorkflowRun and Workflow, the mode in WorkflowRun will override the mode in the Workflow template.

    Built-in Steps

    You can use KubeVela that without label: custom.definition.oam.dev/scope: Application in WorkflowRun.

    You can refer to the to customize your steps.

    caution

    You cannot use application operations.

    Core Features

    Operate WorkflowRun

    The vela workflow command can operate both Application Workflow and WorkflowRun. By default, it will look for the application with the same name first, and if it is not found, it will look for WorkflowRun. You can also use --type=workflow to indicate that the operation object is WorkflowRun.

    Suspend

    If you have an executing WorkflowRun, you can use vela workflow suspend to suspend the workflow.

    1. vela workflow suspend <name>

    K8S API for Pipeline - 图5tip

    If the workflow has executed completely, using the vela workflow suspend command has no effect.

    Resume

    When the WorkflowRun is suspended, you can use vela workflow resume command to manually resume the workflow.

    1. vela workflow resume <name>

    Terminate

    If you have an executing WorkflowRun, you can use vela workflow terminate to terminate the workflow.

    Check the Logs

    If you want to view the WorkflowRun logs, you can use vela workflow logs command to view the logs.

    tip

    Only steps configured with in its definition will have log output.

    1. vela workflow logs <name>

    Suspend Manually

    Please refer to Operate WorkflowRun.

    Suspend Automatically(using suspend step)

    1. apiVersion: core.oam.dev/v1alpha1
    2. kind: WorkflowRun
    3. metadata:
    4. name: suspend
    5. namespace: default
    6. spec:
    7. workflowSpec:
    8. steps:
    9. - name: step1
    10. type: apply-deployment
    11. properties:
    12. image: nginx
    13. - name: step2-suspend
    14. type: suspend
    15. - name: step2
    16. type: apply-deployment
    17. image: nginx

    The WorkflowRun will automatically suspend when the first step is completed, and the third step will not be executed until you continue the WorkflowRun.

    Resume Manually

    Please refer to .

    Resume Automatically

    Configure duration: <duration> in the suspend type of step, when the duration time expires, WorkflowRun will automatically continue to execute.

    1. apiVersion: core.oam.dev/v1alpha1
    2. kind: WorkflowRun
    3. metadata:
    4. name: suspend
    5. namespace: default
    6. spec:
    7. workflowSpec:
    8. steps:
    9. - name: step1
    10. type: apply-deployment
    11. properties:
    12. image: nginx
    13. - name: step2-suspend
    14. type: suspend
    15. properties:
    16. duration: 10s
    17. - name: step2
    18. type: apply-deployment
    19. properties:
    20. image: nginx

    When the first step is completed, the WorkflowRun will suspend, and after ten seconds, the WorkflowRun will automatically continue to execute the third step.

    Sub Steps

    There is a special step type called step-group. When using a step-group type of step, you can declare sub steps in it.

    Dependency

    You can specify dependencies between steps with dependsOn.

    1. kind: WorkflowRun
    2. metadata:
    3. name: dependency
    4. namespace: default
    5. spec:
    6. mode:
    7. steps: DAG
    8. workflowSpec:
    9. steps:
    10. - name: step1
    11. type: apply-deployment
    12. dependsOn:
    13. - step2
    14. - step3
    15. properties:
    16. image: nginx
    17. - name: step2
    18. type: apply-deployment
    19. properties:
    20. image: nginx
    21. - name: step3
    22. type: apply-deployment
    23. properties:
    24. image: nginx

    Data passing between steps can be done through inputs and outputs. For details, please refer to Input and output between steps.

    1. apiVersion: core.oam.dev/v1alpha1
    2. kind: WorkflowRun
    3. metadata:
    4. name: request-http
    5. namespace: default
    6. spec:
    7. workflowSpec:
    8. steps:
    9. - name: request
    10. properties:
    11. url: https://api.github.com/repos/kubevela/workflow
    12. outputs:
    13. - name: stars
    14. valueFrom: |
    15. import "strconv"
    16. "Current star count: " + strconv.FormatInt(response["stargazers_count"], 10)
    17. - name: notification
    18. type: notification
    19. inputs:
    20. - from: stars
    21. parameterKey: slack.message.text
    22. properties:
    23. slack:
    24. url:
    25. value: <your slack url>

    In this WorkflowRun, the first step will request the GitHub API to get the number of stars in the workflow repository as Output, and then use this Output as Input in the next step to send the star number as the message to Slack.

    Timeout

    You can specify timeout for a step to indicate the timeout for that step.

    timeout follows the duration format, e.g. 30s, 1m, etc. You can refer to Golang’s parseDuration.

    1. apiVersion: core.oam.dev/v1alpha1
    2. kind: WorkflowRun
    3. name: timeout
    4. namespace: default
    5. spec:
    6. workflowSpec:
    7. steps:
    8. - name: suspend
    9. type: suspend
    10. timeout: 3s

    If the above WorkflowRun is not resumed within three seconds, the suspend step will fail with timeout.

    If Conditions

    You can use if in a step to determine whether to execute the step.

    No If specified

    If the step does not specify if, if the step before the step fails to execute, then the step will be skipped and will not be executed.

    if: always

    With if: always specified in a step, the step will be executed no matter what.

    Custom If

    You can also write your own judgment logic to determine whether the step should be executed. Note: The value in if will be executed as CUE code. WorkflowRun provides some built-in variables in if, these are:

    • statusstatus contains status information for all workflow steps. You can use status.<step-name>.phase == "succeeded" to determine the status of a step, or you can use the simplified status.<step-name>.succeeded to determine.
    • inputsinputs contains all the inputs parameters of the step. You can use inputs.<input-name> == "value" to get input for the step.
    • context: context contains all the context data of WorkflowRun. You can use context.<context-name> == "value" to get the context of the WorkflowRun.

    K8S API for Pipeline - 图7tip

    Note that if your step name or inputs name is not a valid CUE variable name (eg: contains -, or starts with a number, etc.), you can refer to it as follows: status["invalid-name"].failed.

    In the above WorkflowRun, if the suspend step fails due to a timeout, then the my-step step will be executed, otherwise the my-step2 step will be executed.

    Steps in WorkflowRun have some built-in context data, and you can also declare your custom context parameters in context.

    tip

    If your custom context data has the same name as a built-in context data, the built-in context parameter will be overridden by the custom parameter.

    You can control the execution of WorkflowRun in different situations through the combination of conditional if and custom data.

    1. apiVersion: core.oam.dev/v1alpha1
    2. kind: WorkflowRun
    3. metadata:
    4. name: deploy-run
    5. namespace: default
    6. spec:
    7. context:
    8. env: test
    9. workflowRef: deploy-template
    10. ---
    11. apiVersion: core.oam.dev/v1alpha1
    12. kind: Workflow
    13. metadata:
    14. name: deploy-template
    15. namespace: default
    16. steps:
    17. - name: apply
    18. type: apply-deployment
    19. if: context.env == "dev"
    20. properties:
    21. image: nginx
    22. - name: apply-test
    23. type: apply-deployment
    24. if: context.env == "test"
    25. image: crccheck/hello-world

    Built-in Context Data

    The built-in context data in WorkflowRun are as follows: