# gt: General Translation CLI tool: 在 CD 中运行
URL: https://generaltranslation.com/zh/docs/cli/guides/continuous-translation/running-in-cd.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 如何在构建命令之前运行 gt translate，确保每次部署都带上最新的译文。

在 Vercel、Netlify 或任何部署流水线中，要让译文始终保持最新，最简单的方法就是在生产环境构建之前添加 [`npx gt translate`](/docs/cli/reference/commands/translate)。

## 初始化设置 [#setup]

1. 提交 `gt.config.json`，并将 `GT_API_KEY` 和 `GT_PROJECT_ID` 添加到托管平台或 CD 服务商的密钥环境变量中 (参见[通用要求](/docs/cli/guides/continuous-translation#credentials)) 。
2. 在构建脚本开头加上 [`npx gt translate`](/docs/cli/reference/commands/translate)：

```json title="package.json"
{
  "scripts": {
    "translate": "npx gt translate",
    "build": "npm run translate && <your build command>"
  }
}
```

如果你的部署流水线直接执行各个步骤，而不是通过 npm 脚本，请紧接在构建步骤之前运行 [`npx gt translate`](/docs/cli/reference/commands/translate)。

<Callout type="info">
  **已有的独立译文：** 如果你的代码仓库中已有需要保留的 JSON、YAML 或 MDX 译文文件，请在启用自动构建之前，先在本地运行一次 [`npx gt upload`](/docs/cli/reference/commands/upload)。
</Callout>

## 选项与防护措施 [#guardrails]

* **避免因计费上限导致构建失败：** 当你的组织达到**用量上限**或使用额度耗尽时，[`gt translate`](/docs/cli/reference/commands/translate) 会在将新任务入队时失败。[`gt download`](/docs/cli/reference/commands/download) 不受计费限制，因此回退到该命令即可下载源文件未更改部分的已完成的翻译 (仅跳过已修改但无法翻译的文件) ：

  ```bash
  npx gt translate || npx gt download
  ```

  *注意：对于使用内联内容且关闭了 `stageTranslations` 的框架应用，除非 [`omitConfigIds`](/docs/cli/reference/config#omit-config-ids) 为 `true`，否则 [`gt download`](/docs/cli/reference/commands/download) 要求 `gt.config.json` 中已存在先前的 `_versionId`。*

* **避免使用 `--force`：** `--force` 会重新翻译所有内容、覆盖手动编辑，且每次运行都会计费。

* **同步本地修改：** 如果你的团队会直接编辑已提交的翻译文件，请传入 `--save-local`，或将 [`options.saveLocal`](/docs/cli/reference/config#save-local) 设为 `true`。

* **分支、标签和 CDN：** 传入 `--enable-branching` 可隔离功能分支 (参见[按分支跟踪翻译](/docs/cli/guides/branching)) ；传入 `--tag` 可用 git commit 为每次运行添加标签；在 PR 检查中可使用 `--dry-run`；在 **项目 &gt; 设置** 中启用 **CDN** 后，可传入 `--publish`。

* **已应用的词汇表更新：** 执行 **项目 &gt; 上下文 &gt; Apply** 后，若输出已被 gitignore，全新构建会自动拉取更新。如果译文和 `gt-lock.json` 已提交到仓库，请运行 [`npx gt translate --force-download`](/docs/cli/reference/commands/translate)，以覆盖源内容未更改的文件。

## Next steps

- /docs/cli/guides/continuous-translation/using-github-actions
- /docs/cli/guides/continuous-translation/staging-in-ci
- /docs/cli/guides/continuous-translation/splitting-ci-stages
- /docs/cli/guides/branching

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
