This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

EAS 工作流中的自定义函数

学习如何用自定义函数定义可重用的步骤序列。


自定义函数为自定义作业和钩子定义可重用的步骤序列。你可以像调用内置函数一样,通过 uses 使用相对路径调用它们。每个自定义函数在 function.yml 或 function.yaml 文件的 runs.steps 下定义它的步骤。这些步骤使用和 自定义作业步骤 相同的格式。

🌐 Custom functions define reusable step sequences for custom jobs and hooks. You can call them with a relative path using uses, like built-in functions. Each custom function defines its steps under runs.steps in a function.yml or function.yaml file. These steps use the same format as custom job steps.

关于 function.yml 的模式,请参阅语法参考中的 自定义函数。

🌐 For the function.yml schema, see Custom functions in the syntax reference.

工作原理

🌐 How it works

为你的自定义函数创建一个目录,并在其中放一个 function.yml 文件。EAS 工作流也接受 function.yaml。EAS 工作流会把你传给 uses 的路径相对于你的 EAS 项目目录进行解析:

🌐 Create a directory for your custom function with a function.yml file inside it. EAS Workflows also accepts function.yaml. EAS Workflows resolves the path you pass to uses relative to your EAS project directory:

my-app
 .eas
  workflows
   publish-preview-update.yml
  functions
   setup
    function.yml
 eas.json

上面例子中的 .eas/functions/<name> 路径只是其中一个选项。uses 的值必须以 ./ 或 ../ 开头,并指向包含 function.yml 或 function.yaml 文件的目录。以 ../ 开头的路径可以指向 EAS 项目目录之外,但自定义函数必须保留在仓库内。

🌐 The .eas/functions/<name> path in the example above is only one option. The uses value must start with ./ or ../ and point to a directory containing a function.yml or function.yaml file. A path that starts with ../ can point outside the EAS project directory, but the custom function must remain within the repository.

自定义函数内部某个步骤上的相对路径 working_directory 是从作业的默认工作目录解析的,而不是从包含 function.yml 的目录解析的。

🌐 A relative working_directory on a step inside the custom function resolves from the job's default working directory, not the directory containing function.yml.

你可以在以下地方使用自定义函数:

🌐 You can use custom functions in:

输入、输出和范围

🌐 Inputs, outputs and scope

在调用位置用 with: 传入输入。在函数内部,用 ${{ inputs.<name> }} 读取输入。

在 function.yml 中使用 outputs 定义函数输出。EAS 工作流会将每个输出作为字符串暴露出来。把输出读取为 ${{ steps.<call_id>.outputs.<name> }}。使用调用步骤中的 id。调用者无法引用函数的内部步骤标识符。

表达式使用以下范围:

🌐 Expressions use the following scopes:

  • 在函数内部,steps.* 的引用会针对函数自身的步骤进行解析。
  • EAS 工作流程会在调用者的作用域中评估调用步骤的 with、env 和 if 值。
  • 函数中的每一步都会继承调用步骤的 env 值。在嵌套调用中,内部的值会覆盖同名的外部值。
  • EAS 工作流会在函数的作用域内评估输入默认值和函数里的表达式。
  • 调用步骤的 if 把整个函数当作一个整体来控制。

筑Nest

🌐 Nesting

自定义函数可以调用其他自定义函数和内置的 eas/* 函数。目前,自定义函数不能调用 eas/build 或 eas/maestro_test。EAS 工作流会将每个嵌套路径解析为相对于 EAS 项目目录,而不是包含调用函数的目录。

🌐 A custom function can call other custom functions and built-in eas/* functions. Currently, custom functions cannot call eas/build or eas/maestro_test. EAS Workflows resolves every nested path relative to the EAS project directory, not the directory containing the calling function.

你最多可以将自定义函数嵌套到10层。自定义函数不能调用自身,无论是直接调用还是通过其他自定义函数调用。

🌐 You can nest custom functions up to 10 levels deep. A custom function cannot call itself, either directly or through other custom functions.

示例

🌐 Example

.eas/functions/greet/function.yml 定义了一个自定义函数,包含一个输入、一个输出和一个步骤:

.eas/functions/greet/function.yml
name: Greet inputs: - name: who default_value: world outputs: message: value: '${{ steps.make.outputs.message }}' runs: steps: - id: make run: set-output message "Hello, ${{ inputs.who }}!"

一个工作流用 uses: ./.eas/functions/greet 调用它,传入一个带有 with: 的输入,然后从 ${{ steps.greet.outputs.message }} 读取输出:

🌐 A workflow calls it with uses: ./.eas/functions/greet, passes an input with with:, and reads the output from ${{ steps.greet.outputs.message }}:

.eas/workflows/hello.yml
name: Hello jobs: greet: steps: - id: greet uses: ./.eas/functions/greet with: who: Expo - run: echo "${{ steps.greet.outputs.message }}"

你也可以从一个 hook 调用自定义函数:

🌐 You can also call a custom function from a hook:

.eas/workflows/build.yml
jobs: build_ios: type: build params: platform: ios hooks: before_install_node_modules: - uses: ./.eas/functions/setup