# YAML语法说明

## **相关概念**

**Job**是Action工作流的基本组成单位，通过定义多个job，并且设置job的依赖关系，可以生成一个[**DAG**](https://en.wikipedia.org/wiki/Directed_acyclic_graph)，TensorBay Workflow Engine通过执行这个DAG来实现任务的自动化。通过以下YAML文件为例：

```
tasks:
  hello-world:
    container:
      image: alpine:latest
      command: [echo]
      args: ["hello world!"]
```

1. 【tasks】: 定义该workflow下所有的job
2. 【tasks.hello-world】: 定义了一个task，task的名字叫做"hello-world"
3. 【tasks.hello-world.container】: 这个job执行环境是docker container
4. 【tasks.hello-world.container.image】: docker container使用的image是alpine:latest
5. 【tasks.hello-world.container.command】: docker container里执行的命令是echo
6. 【tasks.hello-world.container.args】: echo的参数是"hello world"

## **1  Job的运行环境**

job支持以下3种运行环境，每个job有且仅有一个运行环境：

**1.Container：**&#x5728;docker container里执行任意命令，使用指定的image，生成一个container并执行，任务内容由image定义。

```
tasks:
  hello-world:
    container:
      image: alpine:latest
      command: [echo]
      args: ["hello world!"]
      env:
        - name: GAS-URL
          value: https://gas.graviti.cn
```

**2.Script：**&#x5728;docker container里执行指定的脚本

```
tasks:
  hello-script-task:
    script:
      image: python:alpine3.8
      command: [python]
      source: |
        import logging
        import os
        import sys
        import time
        logging.basicConfig(level=logging.INFO)
        logging.info('environment was:')
        for key, value in os.environ.items():
            logging.info(f'\t{key} = {value}')
        args = sys.argv
        count = 20
        if len(args) > 1:
            count = int(args[1])
        logging.info(f'count was {count}')
        idx = 0
        while idx < count:
            logging.info(f'loop at index {idx}.')
            idx += 1
            time.sleep(1)
        logging.info('complete')
```

**3.Git：**&#x5728;docker container内执行一个git repo的代码

```
tasks:
  hello:
    git:
      image: python:3.9.5-buster
      command: ["python3"]
      args: ["main.py"]
      repo: https://github.com/username/repository.git@reversion
```

## **2  Job的依赖关系**

**Dependencies：**&#x6A;ob可以通过dependencies关键字来定义他们的依赖关系

```
tasks:
  taskA:
    container:
      image: johngraviti/count_down:v0.1
      command: [python]
      args: [/root/main.py, 40]
  taskB:
    dependencies:
      - taskA
    container:
      image: johngraviti/count_down:v0.1
      command: [python]
      args: [/root/main.py, 60]
  taskC:
    dependencies:
      - taskA
    container:
      image: johngraviti/count_down:v0.1
      command: [python]
      args: [/root/main.py, 120]
  taskD:
    dependencies:
      - taskB
      - taskC
    container:
      image: johngraviti/count_down:v0.1
      command: [python]
      args: [/root/main.py, 70]
```

**执行效果：**&#x83F1;形DAG图，taskA先执行，然后taskB和taskC并发执行，taskD等到taskB和taskC都结束才开始运行

![](https://lh4.googleusercontent.com/3KTGAgiHlwu254Si1CwWddPwQ5dT8bWMvXGp4k32hZ_th7wqhE0-vwdmylSHA7in3zWOHlFyQXG78M0hHv9DUrSRk-oY3PV5sKxuLUaNbWhdSEimvExwRS6KCRi0zTNmyaXqQWS7)

## **3  Job的参数设置**

### **3-1  参数的属性**

**Name:** 参数名称，用来在workflow代码中引用该参数

**Type:** 参数类型，指定该参数的数据类型，支持三种简单类型

* string: 字符串
* number: 数字，包括整型，浮点型
* boolean：布尔值，true/false

**Default:** 参数的默认值，启动workflow时，如果没有显式指定，则会取这里预定义的默认值

### **3-2  参数的定义**

支持通过TensorBay控制台设置及查看详细参数

![](https://lh5.googleusercontent.com/V6KvIY9eHZMM4AkBFbrF6V-EvWFxNXl7lYvUyvQspvNBQhGnJHLsIJ54KF4wkyVmffYgj23n41jZLivdZIKEI7Vunt3SBpTr3gIIBAXhr3fwpXJjuWGuDsDxo7C98pkUhx5xdlMd)

### **3-3  参数的设定**

**手动式：**&#x5982;果workflow启动方式为手动式，那么value可以通过TensorBay控制台动态设置

**自动式：**&#x5982;果workflow启动方式为自动式，那么value取默认值

![](https://lh6.googleusercontent.com/kq226beTUPI_YCKew-m26Cz3JaNek7Rg1VxZKSV6wAoIW8ak1sMZsikbcgsPl98rotgTK6C6Y_5XgfuUHatj08Jj31h__RTISL3BVXa9s0akFCV4M0e2OI0yukn1mCfNHg4x_JwY)

### **3-4  参数的引用方式**

在workflow中通过参数**name**使用参数

```
tasks:
  hello-world:
    container:
      image: alpine:latest
      command: [echo]
      args: ["{{workflow.parameters.foo}}" "{{workflow.parameters.bar}}"]
```

1. 引用的参数要&#x7528;**{{}}**&#x5305;围
2. 参数的前缀必须是**workflow\.parameters**.
3. 参数的名称必须跟**预定义**的一致

## **4  Job的环境设置**

**Environment**定义了**container**的环境变量，环境变量的引用方式跟普通的**docker container**环境变量一致


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.graviti.cn/guide/tensorbay/action/action-syntax.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
