# Capistrano（软件）

**Capistrano** 是一个用于构建自动化部署脚本的框架，以 [Ruby](https://www.edgepedia.org/zh/ruby-programming-language) 编写，但可用于任何语言或框架的项目，包括 Rails、Java 和 PHP。它构建在 Ruby、Rake 和 SSH 之上：当开发者运行 `cap` 命令时，Capistrano 通过 SSH 连接到目标服务器并执行部署项目所需的步骤，这些步骤可以是用户编写的 Rake 任务，也可以是社区以 gem 形式分发的任务库。<sup>[1](https://github.com/capistrano/capistrano)</sup> Debian 手册页更宽泛地将其描述为一种通过 SSH 在多台远程机器上并行执行命令的工具和框架。<sup>[2](https://manpages.debian.org/unstable/capistrano/cap.1.en.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个构建在 Ruby、Rake 和 SSH 之上的部署自动化框架，由 `cap` 命令行工具驱动<sup>[1](https://github.com/capistrano/capistrano)</sup> |
| 核心模型 | 每个发布版本放在各自带时间戳的目录中；`current` 符号链接切换到新版本，旧版本被清理<sup>[3](https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake)</sup> |
| 执行方式 | 任务在多台服务器上并发运行，支持并行、分组和顺序执行模式，并使用 SSH 连接池以提高速度<sup>[1](https://github.com/capistrano/capistrano)</sup><sup> • </sup><sup>[4](https://capistranorb.com/2013/06/01/release-announcement.html)</sup> |
| 底层库 | SSHKit 提供每个 `on()` 块内部使用的 SSH 行为、命令映射、日志记录和连接管理<sup>[5](https://github.com/capistrano/sshkit)</sup><sup> • </sup><sup>[4](https://capistranorb.com/2013/06/01/release-announcement.html)</sup> |
| 历史 | 2005 年 8 月 5 日以 SwitchTower 之名推出，2006 年 3 月 5 日更名为 Capistrano；3.0 版本于 2013 年 6 月 1 日发布<sup>[6](https://weblog.jamisbuck.org/2005/8/5/introducing-switchtower.html)</sup><sup> • </sup><sup>[7](https://weblog.jamisbuck.org/2006/3/5/switchtower-renamed.html)</sup><sup> • </sup><sup>[4](https://capistranorb.com/2013/06/01/release-announcement.html)</sup> |
| 当前版本 | 3.20.1，发布于 2026 年 5 月 16 日；自 2006 年 3 月 6 日以来共有 131 个 gem 版本，在 RubyGems 上总下载量约 9020 万次<sup>[8](https://rubygems.org/gems/capistrano/versions)</sup> |
| 环境要求 | 本地机器需 Ruby 2.5 或更高版本，服务器需 POSIX shell（如 Bash 或 Sh），以及一个用于部署的非特权 SSH 用户<sup>[1](https://github.com/capistrano/capistrano)</sup> |

## 部署的工作方式

Capistrano 定义了一套标准部署流程，每个启用了 Capistrano 的项目默认都遵循该流程，因此用户不必决定部署文件放在哪里或常见任务如何执行。服务器被标记为一个或多个角色，如 database、app、web 或 worker，任务声明自己在哪些角色上运行；任务还可以跨多个阶段（如 `qa`、`staging` 和 `production`）参数化，只需指定不同之处，比如 IP 地址。<sup>[1](https://github.com/capistrano/capistrano)</sup>

**发布目录结构。** 每次部署在服务器上创建一个新的发布目录，从共享路径将共享文件和目录链接进去，然后以原子方式将 `current` 符号链接重新指向新版本。内置的 `deploy:cleanup` 任务列出发布目录，保留最近的 `keep_releases` 个，其余分批删除，同时绝不删除 `current` 指向的那个版本。<sup>[3](https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake)</sup> 回滚流程将符号链接恢复到上一个版本，并将被回滚的版本打包为 tar 归档。<sup>[3](https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake)</sup>

**部署流程。** 在 Capistrano v3 中运行 `cap production deploy` 会调用一个固定的任务序列：`deploy:starting` 检查一切就绪，`deploy:updating` 将新版本放到服务器上，`deploy:publishing` 发布它，`deploy:finishing` 进行清理。`:started`、`:updated` 和 `:published` 等钩子任务允许通过 `before()` 和 `after()` 挂载自定义任务。回滚流程共享其中许多任务，但运行自己的 `:finishing_rollback` 任务，因为回滚清理与部署清理不同。<sup>[9](https://capistranorb.com/documentation/getting-started/flow/)</sup>

**执行模式。** 任务可以在所有服务器上并行运行，可以按设定大小分组、组间等待以实现滚动重启，也可以严格按顺序、服务器间等待以避免压垮共享资源。<sup>[4](https://capistranorb.com/2013/06/01/release-announcement.html)</sup>

## SSHKit

Capistrano 3 中 `on()` 块内的一切都由 SSHKit 执行，这是一个从 Capistrano v3 开发过程中诞生的底层工具包。SSHKit 以结构化方式在一台或多台服务器上运行命令，位于 Net::SSH 之上一个层级，但没有 Capistrano 的角色、环境和回滚概念，也可以脱离 Capistrano，在 Rakefile 中独立使用。<sup>[4](https://capistranorb.com/2013/06/01/release-announcement.html)</sup><sup> • </sup><sup>[5](https://github.com/capistrano/sshkit)</sup>

SSHKit 在所有后端上提供 `execute`、`test` 和 `capture` 方法，一个命令映射表将裸命令名扩展为 `/usr/bin/env ...` 以便找到正确的二进制文件，以及上下文方法，如 `within` 指定工作目录、`as` 指定用户、`with` 指定环境变量。默认情况下它并行运行命令，并使用连接池避免为每个 `on()` 块协商新的 SSH 连接；在一次测试中，这使基本的 `cap deploy` 运行快了 15 到 20 秒。空闲超过 30 秒的池化连接会被替换，超时时间可配置，也可设为零以禁用连接池。<sup>[5](https://github.com/capistrano/sshkit)</sup>

命令默认在非登录、非交互式 SSH 会话中运行，这一选择旨在将部署环境与服务器端变更隔离；这也是需要交互的命令需要特殊处理的原因。<sup>[5](https://github.com/capistrano/sshkit)</sup><sup> • </sup><sup>[10](https://capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/)</sup>

## 历史

**Rails 中的起源。** 贾米斯·巴克（Jamis Buck）当时是 37signals 的程序员和 Rails 核心贡献者，于 2005 年 8 月 5 日以 SwitchTower 之名推出该工具，作为 Rails 应用的部署工具。<sup>[6](https://weblog.jamisbuck.org/2005/8/5/introducing-switchtower.html)</sup><sup> • </sup><sup>[11](https://rubyonrails.org/2005/6/2/jamis-and-jeremy-bestowed-with-commit-rights)</sup> 因 SwitchTower 名称引发停止使用要求方面的顾虑，巴克于 2006 年 3 月 5 日为项目更名；Capistrano 这个名字由马塞尔·莫利纳（Marcel Molina）提议，典故是圣胡安-卡皮斯特拉诺的燕子，其他候选名如 [Roundabout](https://www.edgepedia.org/zh/roundabout)、Switchyard、Metro 和 Sherpa 被弃用，Roundabout 因为已是注册商标。Capistrano 1.1 于 2006 年 3 月 6 日跟进发布。<sup>[7](https://weblog.jamisbuck.org/2006/3/5/switchtower-renamed.html)</sup><sup> • </sup><sup>[12](https://weblog.jamisbuck.org/announcements/)</sup> 巴克早期写道，Capistrano 不只是为 Rails 而生，该工具很快被应用于其他类型的项目。<sup>[13](https://weblog.jamisbuck.org/2006/9/21/capistrano-not-just-for-rails.html)</sup>

**巴克的离开。** 2009 年 2 月 24 日，巴克宣布 Net::SSH 2.0.11 和 Capistrano 2.5.5 为最终版本，并以身心俱疲为由停止 Capistrano 及其相关库的开发，将项目的未来交给社区。<sup>[14](https://weblog.jamisbuck.org/2009/2/24/net-ssh-capistrano-and-saying-goodbye.html)</sup>

**版本 3。** Capistrano 团队于 2013 年 6 月 1 日宣布版本 3，这是近五年来第一个大版本。重新设计用 Rake 取代了 Capistrano 自己的 DSL，增加了内置阶段支持，改进了并行性和流式输出，在任务块内暴露了 `host` 对象和每主机属性，并将 SSH、连接池和格式化移入新的 SSHKit 库。该版本移除了 [Mercurial](https://www.edgepedia.org/zh/mercurial)、[Subversion](https://www.edgepedia.org/zh/subversion) 和 CVS 支持，代之以 Git 实现，并放弃了旧的 `deploy:cold` 任务，原则是服务器任务应当是幂等的。<sup>[4](https://capistranorb.com/2013/06/01/release-announcement.html)</sup> 当前的 README 将 Git、Mercurial 和 Subversion 支持列为内置功能。<sup>[1](https://github.com/capistrano/capistrano)</sup>

## 用法与生态

项目通过 `cap install` 进行准备，该命令创建 `Capfile`、全局的 `config/deploy.rb`，以及按阶段划分的文件，如 `config/deploy/production.rb` 和 `config/deploy/staging.rb`。在命令行中，`cap -T` 列出可用任务，`cap staging deploy` 部署到 staging，`--dry-run`、`--prereqs` 和 `--trace` 等标志分别用于模拟部署、列出任务依赖或追踪任务调用。<sup>[1](https://github.com/capistrano/capistrano)</sup>

Capistrano 通过 RubyGems 扩展：存在针对 Rails、WordPress 和 [Laravel](https://www.edgepedia.org/zh/laravel) 等框架的任务库，许多 Ruby 项目也自带 Capistrano 任务。相关仓库包括用于 Rails 部署任务的 capistrano/rails、用于 Bundler 集成的 capistrano/bundler、用于默认日志格式化的 mattbrictson/airbrussh，以及用于 SSH 层的 capistrano/sshkit。<sup>[1](https://github.com/capistrano/capistrano)</sup><sup> • </sup><sup>[15](https://github.com/capistrano/bundler)</sup> 项目采用 MIT 许可证，其 GitHub 仓库约有 13,000 个星标和 1,700 个复刻。<sup>[1](https://github.com/capistrano/capistrano)</sup>

**设计边界。** Capistrano 以可重复、可扩展的方式自动化开发者原本通过 SSH 手动完成的工作，但它不负责服务器置备；项目自己的文档推荐使用 Chef 或 [Puppet](https://www.edgepedia.org/zh/puppet-software) 等工具进行置备，并将 Capistrano 定位为在其之上进行部署。它设计为以单个非特权 SSH 用户在非交互式会话中部署，需要 `sudo`、交互式提示或以不同于认证用户的身份运行命令的部署虽然可行，但很困难。<sup>[1](https://github.com/capistrano/capistrano)</sup><sup> • </sup><sup>[4](https://capistranorb.com/2013/06/01/release-announcement.html)</sup>

## Capistrano 与类似工具

Capistrano 介于简单的 rsync shell 脚本和复杂的容器化工具链之间。[Ansible](https://www.edgepedia.org/zh/ansible-software) 是另一个基于 SSH 的无代理工具，通过 YAML playbook 涵盖配置管理、云置备和应用部署，而 Capistrano 专注于 Ruby 中的发布与回滚部署工作流。<sup>[1](https://github.com/capistrano/capistrano)</sup><sup> • </sup><sup>[16](https://docs.ansible.com/projects/ansible/latest/getting_started/introduction.html)</sup> 部署实践调查将 Capistrano 与 Fabric、Func 和 Ansible 并列为通过 SSH 编写部署脚本的工具，它仍在积极开发中，3.20.1 版本于 2026 年 5 月发布。<sup>[17](https://www.theseus.fi/bitstream/10024/58164/1/Automated%20DeploymentV8final.pdf)</sup><sup> • </sup><sup>[8](https://rubygems.org/gems/capistrano/versions)</sup>

## References

1. [Capistrano: A deployment automation tool built on Ruby, Rake, and SSH, GitHub README](https://github.com/capistrano/capistrano)
2. [cap(1), Debian Manpages, capistrano package](https://manpages.debian.org/unstable/capistrano/cap.1.en.html)
3. [capistrano/lib/capistrano/tasks/deploy.rake, GitHub](https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake)
4. [Capistrano Version 3 Release Announcement, 1 June 2013, capistranorb.com](https://capistranorb.com/2013/06/01/release-announcement.html)
5. [SSHKit: A toolkit for deploying code and assets to servers, GitHub README](https://github.com/capistrano/sshkit)
6. [Introducing SwitchTower, The Buckblog, 5 August 2005](https://weblog.jamisbuck.org/2005/8/5/introducing-switchtower.html)
7. [SwitchTower: Renamed, The Buckblog, 5 March 2006](https://weblog.jamisbuck.org/2006/3/5/switchtower-renamed.html)
8. [All versions of capistrano, RubyGems.org](https://rubygems.org/gems/capistrano/versions)
9. [Flow, Capistrano documentation](https://capistranorb.com/documentation/getting-started/flow/)
10. [Why does something work in my SSH session, but not in Capistrano?, Capistrano FAQ](https://capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/)
11. [Jamis and Jeremy bestowed with commit rights, Ruby on Rails weblog, 2 June 2005](https://rubyonrails.org/2005/6/2/jamis-and-jeremy-bestowed-with-commit-rights)
12. [Announcements category, The Buckblog](https://weblog.jamisbuck.org/announcements/)
13. [Capistrano: Not just for Rails, The Buckblog, 21 September 2006](https://weblog.jamisbuck.org/2006/9/21/capistrano-not-just-for-rails.html)
14. [Net::SSH, Capistrano, and Saying Goodbye, The Buckblog, 24 February 2009](https://weblog.jamisbuck.org/2009/2/24/net-ssh-capistrano-and-saying-goodbye.html)
15. [capistrano/bundler: Bundler support for Capistrano 3.x, GitHub](https://github.com/capistrano/bundler)
16. [Introduction to Ansible, Ansible Community Documentation](https://docs.ansible.com/projects/ansible/latest/getting_started/introduction.html)
17. [Selecting a Deployment Automation Tool, Theseus thesis](https://www.theseus.fi/bitstream/10024/58164/1/Automated%20DeploymentV8final.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling › Deployment and continuous integration

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/capistrano-software

English original: https://www.edgepedia.org/capistrano-software
