

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 任務套件的任務範本元素
<a name="build-job-bundle-template"></a>

任務範本會定義執行時間環境，以及做為截止日期雲端任務一部分執行的程序。您可以在範本中建立參數，以便用來建立只在輸入值中不同的任務，就像程式設計語言中的函數一樣。

當您將任務提交至截止日期雲端時，該任務會在套用至佇列的任何佇列環境中執行。佇列環境是使用 Open Job Description (OpenJD) 外部環境規格建置。如需詳細資訊，請參閱 OpenJD GitHub 儲存庫中的[環境範本](https://github.com/OpenJobDescription/openjd-specifications/wiki/2023-09-Template-Schemas#12-environment-template)。

如需使用 OpenJD 任務範本建立任務的簡介，請參閱在 OpenJD GitHub 儲存庫中[建立任務的簡介](https://github.com/OpenJobDescription/openjd-specifications/wiki/Introduction-to-Creating-a-Job)。如需其他資訊，請參閱[執行任務的方式](https://github.com/OpenJobDescription/openjd-specifications/wiki/How-Jobs-Are-Run)。OpenJD GitHub 儲存庫`samples`目錄中的 中有任務範本範例。

您可以 YAML 格式 (`template.yaml`) 或 JSON 格式 () 定義任務範本`template.json`。本節中的範例會以 YAML 格式顯示。

例如，`blender_render`範例的任務範本將輸入參數定義為`BlenderSceneFile`檔案路徑：

```
- name: BlenderSceneFile
  type: PATH
  objectType: FILE
  dataFlow: IN
  userInterface:
    control: CHOOSE_INPUT_FILE
    label: Blender Scene File
    groupLabel: Render Parameters
    fileFilters:
    - label: Blender Scene Files
      patterns: ["*.blend"]
    - label: All Files
      patterns: ["*"]
  description: >
    Choose the Blender scene file to render. Use the 'Job Attachments' tab
    to add textures and other files that the job needs.
```

`userInterface` 屬性會使用 命令在 Autodesk Maya 等應用程式的任務提交外掛程式中，定義`deadline bundle gui-submit`命令列自動產生之使用者介面的行為。

在此範例中，用於輸入 `BlenderSceneFile` 參數值的 UI 小工具是僅顯示`.blend`檔案的檔案選擇對話方塊。

![用於輸入 OpenJD 任務範本場景檔案參數的使用者介面小工具。](http://docs.aws.amazon.com/zh_tw/deadline-cloud/latest/developerguide/images/blender_submit_scene_file_widget.png)


如需使用 `userInteface`元素的更多範例，請參閱 GitHub 上 [deadline-cloud-samples](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline) 儲存庫中的 [gui\_control\_showcase](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline/job_bundles/gui_control_showcase) 範例。

當您從任務套件提交任務時， `objectType`和 `dataFlow` 屬性會控制任務附件的行為。在此情況下， `objectType: FILE` 值`dataFlow:IN`表示 `BlenderSceneFile` 是任務附件的輸入檔案。

相反地， `OutputDir` 參數的定義具有 `objectType: DIRECTORY`和 `dataFlow: OUT`：

```
- name: OutputDir
  type: PATH
  objectType: DIRECTORY
  dataFlow: OUT
  userInterface:
    control: CHOOSE_DIRECTORY
    label: Output Directory
    groupLabel: Render Parameters
  default: "./output"
  description: Choose the render output directory.
```

任務附件會使用 `OutputDir` 參數的值做為任務寫入輸出檔案的目錄。

如需 `objectType`和 `dataFlow` 屬性的詳細資訊，請參閱 [Open Job Description 規格](https://github.com/OpenJobDescription/openjd-specifications)中的 [JobPathParameterDefinition](https://github.com/OpenJobDescription/openjd-specifications/wiki/2023-09-Template-Schemas#22-jobpathparameterdefinition) 

`blender_render` 任務範本範例的其餘部分會將任務的工作流程定義為單一步驟，動畫中的每個影格都會轉譯為個別任務：

```
steps:
- name: RenderBlender
  parameterSpace:
    taskParameterDefinitions:
    - name: Frame
      type: INT
      range: "{{Param.Frames}}"
  script:
    actions:
      onRun:
        command: bash
        # Note: {{Task.File.Run}} is a variable that expands to the filename on the worker host's
        # disk where the contents of the 'Run' embedded file, below, is written.
        args: ['{{Task.File.Run}}']
    embeddedFiles:
      - name: Run
        type: TEXT
        data: |
          # Configure the task to fail if any individual command fails.
          set -xeuo pipefail

          mkdir -p '{{Param.OutputDir}}'

          blender --background '{{Param.BlenderSceneFile}}' \
                  --render-output '{{Param.OutputDir}}/{{Param.OutputPattern}}' \
                  --render-format {{Param.Format}} \
                  --use-extension 1 \
                  --render-frame {{Task.Param.Frame}}
```

例如，如果 `Frames` 參數的值為 `1-10`，則會定義 10 個任務。每個 都有不同的 `Frame` 參數值。若要執行任務：

1. 內嵌檔案 `data` 屬性中的所有變數參考都會展開，例如 `--render-frame 1`。

1. `data` 屬性的內容會寫入磁碟上工作階段工作目錄中的檔案。

1. 任務的`onRun`命令會解析為 `bash {{location of embedded file}}`，然後執行。

如需內嵌檔案、工作階段和路徑映射位置的詳細資訊，請參閱 [Open Job Description 規格](https://github.com/OpenJobDescription/openjd-specifications/wiki/How-Jobs-Are-Run)中的[任務執行方式](https://github.com/OpenJobDescription/openjd-specifications/wiki/How-Jobs-Are-Run)。

在 [deadline-cloud-samples/job\_bundles](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline/job_bundles) 儲存庫中有更多任務範本的範例，以及 Open Job Descriptions 規格提供的[範本範例](https://github.com/OpenJobDescription/openjd-specifications/tree/mainline/samples)。