> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tenderly.co/llms.txt
> Use this file to discover all available pages before exploring further.

# 通过 CLI 部署 Web3 Action

> 通过分步指南，开始使用 Tenderly CLI 构建 Web3 Actions。

在本指南中，您将学习如何通过 Tenderly CLI 部署 Web3 Actions，它监听区块被打包并简单地输出区块号。

### 前提条件

您需要在本地机器上安装并正确配置 npm 和 Tenderly CLI。查看 [Web3 Actions CLI 备忘单](/monitoring/web3-actions/references/cli-cheatsheet) 以获取安装说明。

### 初始化 Web3 Actions 项目

导航到系统中您要放置 Web3 Action 代码的文件夹，运行以下命令初始化一个新的 Web3 Actions 项目：

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tenderly actions init
```

默认情况下，项目将配置为使用 TypeScript。如果您更喜欢纯 JavaScript，请添加 language 标志并指定 javascript：

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tenderly actions init --language javascript
```

出现提示时，从您的账户中选择现有的 Tenderly 项目或创建新的项目。

<Frame caption="创建新的 Tenderly 项目">
  <img src="https://mintcdn.com/tenderly/XsEZlaGXYskrtN68/images/Screenshot_2022-08-04_at_10.50.42%20(1).png?fit=max&auto=format&n=XsEZlaGXYskrtN68&q=85&s=5191970c6b2a63a0ee869429dbeaf725" alt="Creating a new Tenderly project" width="434" height="65" data-path="images/Screenshot_2022-08-04_at_10.50.42 (1).png" />
</Frame>

接下来，输入将存储代码的根目录名称。如果您希望保留默认的 actions，按 Enter 键；或者输入自定义目录名称。

<Frame caption="添加目录名称">
  <img src="https://mintcdn.com/tenderly/XsEZlaGXYskrtN68/images/Screenshot_2022-08-04_at_10.51.55.png?fit=max&auto=format&n=XsEZlaGXYskrtN68&q=85&s=bec5b2d4ea93181226d4bd4fb4b4a86a" alt="Adding a directory name" width="553" height="55" data-path="images/Screenshot_2022-08-04_at_10.51.55.png" />
</Frame>

您项目的文件夹结构应该如下所示：

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
/actions
|--- example.ts
|--- package.json
|--- package-lock.json
|--- tsconfig.json
|--- /node_modules
      |--- @tenderly
      |--- /typescript
tenderly.yaml
```

### 构建和部署 Web3 Action

以下是构建和部署 Web3 Action 需要采取的步骤：

* 创建 action 函数
* 配置 block 触发器以运行该函数
* 使用 CLI 将代码部署到 Tenderly

#### 创建 action 函数

将以下代码复制到 **example.ts** 文件中：

```tsx title="example.tsx" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}

export const blockHelloWorldFn: ActionFn = async (context: Context, event: Event) => {
  let blockEvent = event as BlockEvent;
  console.log('Block number is: ', blockEvent.blockNumber);
};
```

这定义并导出了一个 action 函数。在下一步中，我们将配置它在打包 10 个区块后运行。

**注意：**

请注意，该函数是异步的，接受两个参数 —— 一个 `Context` 对象和一个 `Event` 对象。

* `Context` 对象允许访问 Storage 和 Secrets。您可以在此阅读更多相关信息。
* `event` 参数是一个包含有关触发 action 的外部（链上）事件信息的对象。因为我们希望在与区块相关的事件上运行此 action，所以 `event` 参数将是 `BlockEvent` 类型。`BlockEvent` 对象包含被打包区块的相关信息，例如区块号、区块哈希和区块时间戳。

#### 在 **tenderly.yaml** 中为新 action 配置触发器

**步骤 1**：将 `tenderly.yaml` 文件的内容替换为以下内容：

```yaml title="example.yaml" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
account_id: ''
project_slug: ''
actions:
  username/project-slug:
    runtime: v1
    sources: actions
    specs:
      block-number-printer:
        description: Example Web3 Action that logs block number once it is mined.
        function: example:blockHelloWorldFn
        trigger:
          type: block
          block:
            network:
              - 1
            blocks: 10
```

**步骤 2**：将 `username` 和 `project-slug` 替换为您的用户名和项目 slug。

查看本指南以了解如何 [查找组织名称、用户名和项目 slug](/platform/account/projects/slug)。

**注意：**

**tenderly.yaml** 文件包含项目中每个 action 的触发器规范，并将触发器与 action 函数关联。在此，我们创建了一个名为 `block-number-printer` 的 action。

* `function` 属性引用了 **example.ts** 文件中的 `blockHelloWorldFn` 函数。
* 按照 `trigger` 属性，此 action 将在网络 1 上每被打包 10 个区块时触发。

了解更多有关 [外部事件和触发器类型](/monitoring/web3-actions/references/functions-events-triggers#external-events-and-trigger-types) 的信息。

#### 构建和部署 actions

返回终端并运行以下命令来部署您定义的所有 Web3 Actions。成功部署后，actions 会出现在 Tenderly UI 中。

```bash title="example" showLineNumbers theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tenderly actions deploy
```

<Note>
  **Web3 Actions 的零停机升级**\ 要 **升级** Web3 Action，只需编辑源代码
  和/或 **tenderly.yaml** 并运行 `tenderly actions deploy.`
</Note>

### 结束之前

请记住：

* 您必须从包含 **tenderly.yaml** 文件的文件夹运行 `deploy` 命令。
* 如果您在同一目录下为多个 Tenderly 项目初始化 actions，运行 `deploy` 时将要求您选择一个项目。仅会部署所选项目下的 actions。
* 属于同一个项目的所有 actions 会同时部署。
* 从 JavaScript/TypeScript 引用的任何文件都必须位于 *actions root* 中。不要引用此文件夹之外的文件。

### 资源

* 了解更多关于 [Web3 Actions 函数、事件和触发器](/monitoring/web3-actions/references/functions-events-triggers)。
* 了解更多关于 [Context、Storage 和 Secrets](/monitoring/web3-actions/references/context)。
* 了解更多关于 [项目结构和 **tenderly.yaml** 配置](/monitoring/web3-actions/references/project-structure)。
