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

运行你的第一个EAS工作流程任务

通过创建工作流文件、任务、触发器,并将它们串联起来,为 Expo 应用学习 EAS 工作流的基础知识。


在这一章里,我们创建第一个 EAS 工作流任务。

🌐 In this chapter, we create our first EAS Workflows job.

学习成果

🌐 Learning outcomes

  • 创建并运行一个自定义工作流,在 EAS 仪表板上打印消息
  • 设置一个自动触发器,这样每次推送到 main 都会运行工作流
  • 将多个任务串联起来,让一个任务的输出成为下一个任务的输入
  • 在EAS仪表板中查看工作流日志和任务图

工作流文件是如何运作的

🌐 How workflow files work

工作流文件是存储在我们项目的 .eas/workflows/ 目录下的 YAML 文件。每个文件定义了要运行的任务以及每个任务的步骤。

🌐 Workflow files are YAML files stored in our project's .eas/workflows/ directory. Each file defines the jobs to run and the steps each job follows.

一个工作流文件包含一个或多个任务以及一个可选的触发器。运行时,EAS 会读取该文件,配置环境,并在该环境中执行这些任务。

🌐 A workflow file contains one or more jobs and an optional trigger. When it runs, EAS reads the file, provisions the environment, and executes the jobs in that environment.

EAS 工作流程中的工作类型

🌐 Types of jobs in EAS Workflows

EAS 工作流程有两种类型的任务:

🌐 EAS Workflows has two types of jobs:

  • 预打包工作: 这个工作使用 type 字段。它告诉 EAS 要运行哪个操作,比如 build、submit 或 update。然后 EAS 就会运行为该操作预先配置的步骤。
  • 自定义任务: 这种类型的任务使用 steps 而不是 type 字段。我们用 run 为每一步编写自己的命令。自定义任务让我们可以控制工作流运行时的操作,以及什么时候运行特定命令。

创建我们的第一个工作流程

🌐 Create our first workflow

让我们使用自定义任务创建我们的第一个工作流,并在 EAS 上运行它。

🌐 Let's create our first workflow using a custom job and run it on EAS.

1

创建工作流目录

🌐 Create the workflow directory

在我们 Expo 项目的根目录下创建 .eas/workflows/ 目录。EAS 只会在这个位置查找工作流文件。

🌐 Create the .eas/workflows/ directory at the root of our Expo project. EAS looks for workflow files only in this location.

Terminal
- mkdir -p .eas/workflows

2

添加一个 hello.yml 文件

🌐 Add a hello.yml file

在 .eas/workflows/ 里面,创建一个名为 hello.yml 的文件。这个文件使用一个自定义作业定义了我们的第一个工作流。这个作业运行一个打印 "Hello, Workflows" 的 shell 命令:

🌐 Inside .eas/workflows/, create a file named hello.yml. This file defines our first workflow using a single custom job. The job runs a shell command that prints "Hello, Workflows":

.eas/workflows/hello.yml
name: Hello jobs: greet: steps: - run: echo "Hello, Workflows" # Any shell command works here

上面的工作流程使用了以下语法:

🌐 The above workflow uses the following syntax:

  • name 是 EAS 仪表板上显示的工作流名称。
  • jobs 包含一个或多个要运行的任务。每个任务都有一个唯一的 ID(在本例中为 greet)。
  • steps 是作业按顺序执行的一系列任务。拥有 steps 但没有 type 字段的作业是自定义作业。
  • run 是在一个步骤中执行的 shell 命令。这里,它运行 echo 来打印一条消息。任何 shell 命令都可以用。

3

手动运行工作流

🌐 Manually run the workflow

要在 EAS 仪表板上运行工作流,请在终端窗口中运行以下命令:

🌐 To run the workflow on the EAS dashboard, run the following command from a terminal window:

Terminal
- eas workflow:run .eas/workflows/hello.yml

eas workflow:run 命令会把工作流文件上传到 EAS。然后它会运行文件中定义的任何任务,EAS CLI 会打印一个 URL,带你去 EAS 仪表板观看任务的执行情况。

🌐 The eas workflow:run command uploads the workflow file to EAS. It then runs any job defined inside the file and the EAS CLI prints a URL to the EAS dashboard where we can watch the job.

4

查看作业日志

🌐 View the job logs

在浏览器窗口中打开上一步的 URL。在工作流页面上,我们可以看到 greet 任务及其状态。任务完成后,展开步骤输出以查看记录的消息:Hello, Workflows。

🌐 Open the URL from the previous step in a browser window. On the workflow page, we can see the greet job and its status. Once the job completes, expand the step output to see the logged message: Hello, Workflows.

EAS 仪表板是我们查看和调试任何工作流任务的地方。工作流图显示了是什么触发了我们的任务以及任务的唯一 ID。

🌐 The EAS dashboard is where we view and debug any workflow job. The Workflow graph shows what triggered our job and the job's unique ID.

切换到 Workflow file 标签。这就是我们在第2步写的工作流文件,它会随运行一起保存。

🌐 Switch to the Workflow file tab. This is the workflow file we wrote in Step 2, preserved with the run.

自动运行工作流

🌐 Automatically run a workflow

在终端运行 eas workflow:run 可以一次性使用,但大多数团队希望有在 GitHub 事件触发时自动执行的工作流。自动触发意味着没人需要记得去运行工作流。工作流的配置和它构建的代码一起存放在仓库里。

🌐 Running eas workflow:run from the terminal works for a one-off, but most teams want workflows that fire automatically on GitHub events. Automatic triggers mean no one has to remember to run a workflow. The workflow's configuration lives in the repository alongside the code it builds.

我们在工作流文件中用 on 键定义触发器。EAS 会监控匹配的 GitHub 事件,并自动运行工作流。

🌐 We define a trigger with the on key in the workflow file. EAS watches for the matching GitHub event and runs the workflow automatically.

1

添加自动触发器

🌐 Add an automatic trigger

让我们在现有的工作流文件中添加一个 on.push.branches 触发器。

🌐 Let's add an on.push.branches trigger to our existing workflow file.

  • on 键定义了哪个 GitHub 事件会触发工作流
  • push 字段定义何时触发
  • branches 字段允许我们指定,每当代码推送到 main 分支时,工作流程应该自动运行
.eas/workflows/hello.yml
name: Hello on: push: branches: ['main'] # Fires on every push to main branch jobs: greet: steps: - run: echo "Hello, Workflows"

branches 字段可以包含 GitHub 仓库中存在的任何分支名称。我们可以指定多个分支,比如 ['main', 'develop', 'staging'],当我们推送到这些分支中的任意一个时,就会触发该工作流。

🌐 The branches field can include any branch name that exists in a GitHub repository. We can specify multiple branches such as ['main', 'develop', 'staging'] to trigger the workflow when we push to any of those branches.

2

提交更改并推送到 GitHub

🌐 Commit the change and push to GitHub

提交工作流文件并推送到 main 分支:

🌐 Commit the workflow file and push it to the main branch:

Terminal
- git add .eas/workflows/hello.yml
- git commit -m "Add hello workflow"
- git push origin main

3

验证自动触发

🌐 Verify the automatic trigger

让我们在 EAS 仪表板上打开我们项目的 工作流页面 来看看新的任务。

🌐 Let's open our project's Workflows page on the EAS dashboard to see the new job.

在 EAS 仪表板中,注意 Workflow graph 标签显示了此次推送的提交信息和分支引用。在工作流标题下的状态中,Trigger 和 Triggered by 也反映了相同的信息。

🌐 In the EAS dashboard, notice that the Workflow graph tab shows the commit message and branch reference for this push. In the status under the workflow header, the Trigger and Triggered by reflect the same information.

其他触发类型

🌐 Other trigger types

在本教程后面我们会使用的其他触发类型:

🌐 Other trigger types we use later in this tutorial:

  • on.pull_request:当针对匹配分支打开或更新拉取请求时运行
  • on.push 与 tags:当匹配的标签模式推送到 GitHub 仓库时运行
  • on.pull_request_labeled:当给拉取请求添加特定标签时运行

把工作串起来

🌐 Chain jobs together

到目前为止,我们在工作流文件中只实现了一个作业。一个工作流文件可以包含多个作业。一个作业的输入可以依赖于前一个作业的输出。让我们在工作流文件中将多个作业串联起来。

🌐 So far, we have implemented a single job in our workflow file. A workflow file can have multiple jobs. A job's input can depend on the output of the previous job. Let's chain multiple jobs together in a workflow file.

1

更新 hello.yml

🌐 Update hello.yml

让我们更新 hello.yml 文件,添加一个自定义作业和一个预打包作业。这个预打包作业叫做 doc,它会在 EAS 仪表板上渲染 Markdown:

🌐 Let's update the hello.yml file to add a custom job and a pre-packaged job. This pre-packaged job is called doc and it renders Markdown on the EAS dashboard:

.eas/workflows/hello.yml
name: Hello jobs: greet: outputs: greeting: ${{ steps.set_greeting.outputs.greeting }} # Expose the step output at the job level steps: - id: set_greeting run: set-output greeting "Hello from EAS Workflows" show_info: needs: [greet] # Wait for greet to finish, then read its outputs type: doc # Pre-packaged job that renders Markdown on the dashboard params: md: | # Workflow Output **${{ needs.greet.outputs.greeting }}**

上面的工作流文件做了以下事情:

🌐 The above workflow file does the following:

  • 定义了一个 greet 作业,它是一个自定义作业。它使用 set-output shell 函数来设置一个叫做 greeting 的值。作业级别的 outputs 字段会暴露步骤输出,以便其他作业可以引用它。
  • 定义一个 show_info 任务,它使用 needs: [greet] 来等待 greet 任务完成并获取其输出。然后,它使用预打包的 doc 任务类型在 EAS 仪表板中渲染一个 Markdown 文档。Markdown 内容包含在前一个任务中设置的 greeting 的值。

2

运行链式工作流

🌐 Run the chained workflow

在终端窗口中运行以下命令来查看连锁作业的运行效果:

🌐 Run the following command from a terminal window to see the chained jobs in action:

Terminal
- eas workflow:run .eas/workflows/hello.yml

从 EAS CLI 打开 EAS 仪表板链接。在 Logs 下,注意 greet 任务先运行。它完成后,show_info 任务会运行,并使用上一个任务的输出渲染 Markdown 内容。

🌐 Open the EAS dashboard link from the EAS CLI. Under Logs, notice that the greet job runs first. Once it completes, the show_info job runs and renders the Markdown content using the output from the previous job.

清理一下

🌐 Clean up

在本章之后,删除 hello.yml 文件,因为在接下来的章节中我们不会使用它。如果我们想保留它,移除 on.push 触发器,这样它就不会在每次推送到 main 时触发。

🌐 After this chapter, delete the hello.yml file because we do not use it in the next chapters. If we prefer to keep it, remove the on.push trigger so it does not fire on every push to main.

概括

🌐 Summary

Chapter 1: First EAS Workflows job

We created a custom job workflow, triggered it manually and automatically, chained jobs using needs and outputs, and used the EAS dashboard to verify job execution.

In the next chapter, learn how to automate development builds with fingerprinting to skip unnecessary rebuilds.

下一个:Chapter 2: Development builds