{"meta":{"title":"创建第三方 CLI 操作","intro":"了解如何开发操作以在 GitHub Actions 运行器上设置 CLI。","product":"GitHub Actions","breadcrumbs":[{"href":"/zh/actions","title":"GitHub Actions"},{"href":"/zh/actions/how-tos","title":"操作方法"},{"href":"/zh/actions/how-tos/create-and-publish-actions","title":"创建和发布操作"},{"href":"/zh/actions/how-tos/create-and-publish-actions/create-a-cli-action","title":"创建 CLI 操作"}],"documentType":"article"},"body":"# 创建第三方 CLI 操作\n\n了解如何开发操作以在 GitHub Actions 运行器上设置 CLI。\n\n## 简介\n\n您可以编写一个操作，使用户能够通过 GitHub Actions 运行器上已配置的 CLI 环境访问您的服务器。\n\n您的操作应：\n\n* 使用户能够轻松指定要安装的 CLI 版本\n* 支持多种操作系统\n* 以高效的方式运行，以最大限度地减少运行时间和相关成本\n* 跨 GitHub 托管和自托管运行器工作\n* 尽可能利用社区工具\n\n本文将演示如何编写一个操作来检索特定版本的 CLI、安装它、将其添加到路径以及（可选）缓存它。 这种类型的操作（设置工具的操作）通常命名为 `setup-$TOOL`。\n\n## 先决条件\n\n您应该了解如何编写自定义操作。 有关详细信息，请参阅“[管理自定义操作](/zh/actions/how-tos/create-and-publish-actions/manage-custom-actions)”。\n\n## 示例\n\n以下脚本演示如何获取用户指定的版本作为输入，下载并提取 CLI 的特定版本，然后将 CLI 添加到路径中。\n\nGitHub 提供 [`actions/toolkit`](https://github.com/actions/toolkit)，这是一组有助于创建操作的包。 此示例使用 [`actions/core`](https://github.com/actions/toolkit/tree/main/packages/core) 和 [`actions/tool-cache`](https://github.com/actions/toolkit/tree/main/packages/tool-cache) 包。\n\n```javascript copy\nconst core = require('@actions/core');\nconst tc = require('@actions/tool-cache');\n\nasync function setup() {\n  // Get version of tool to be installed\n  const version = core.getInput('version');\n\n  // Download the specific version of the tool, e.g. as a tarball\n  const pathToTarball = await tc.downloadTool(getDownloadURL());\n\n  // Extract the tarball onto the runner\n  const pathToCLI = await tc.extractTar(pathToTarball);\n\n  // Expose the tool by adding it to the PATH\n  core.addPath(pathToCLI)\n}\n\nmodule.exports = setup\n```\n\n若要使用此脚本，请将 `getDownloadURL` 替换为下载 CLI 的函数。 还需要创建一个接受 `action.yml` 输入并运行此脚本的操作元数据文件 (`version`)。 有关如何创建操作的完整详细信息，请参阅“[创建 JavaScript 操作](/zh/actions/tutorials/create-actions/create-a-javascript-action)”。\n\n## 其他阅读材料\n\n此模式用于多个操作。 有关更多示例，请参阅：\n\n* [`ruby/setup-ruby`](https://github.com/ruby/setup-ruby)\n* [`google-github-actions/setup-gcloud`](https://github.com/google-github-actions/setup-gcloud)\n* [`hashicorp/setup-terraform`](https://github.com/hashicorp/setup-terraform)"}