为 PHP 项目设置部署/构建/CI 周期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2180460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up a deployment / build / CI cycle for PHP projects
提问by Pekka
I am a lone developer most of my time, working on a number of big, mainly PHP-based projects. I want to professionalize and automate how changes to the code base are handled, and create a Continuous Integration process that makes the transition to work in a team possible without having to make fundamental changes.
我大部分时间都是一个孤独的开发人员,从事许多大型项目,主要是基于 PHP 的项目。我想专业化和自动化处理代码库更改的方式,并创建一个持续集成流程,使在团队中工作的过渡成为可能,而无需进行根本性的更改。
What I am doing right now is, I have a local test environment for every project; I use SVN for each project; changes are tested locally, and then transferred to the on-line version, usually via FTP. API documentation is generated manually from the source code; Unit tests are something I am getting into slowly, and it's not yet part of my daily routine.
我现在正在做的是,我为每个项目都有一个本地测试环境;我为每个项目使用 SVN;更改在本地进行测试,然后通常通过 FTP 传输到在线版本。API 文档是从源代码手动生成的;单元测试是我慢慢接触的东西,它还不是我日常工作的一部分。
The "build cycle" I am envisioning would do the following:
我设想的“构建周期”将执行以下操作:
A changeset gets checked into SVN after having been tested locally.
I start the build process. The SVN HEAD revision gets checked out, modified if necessary, and made ready for upload.
API Documentation gets generated automatically - if I haven't set it up in detail yet, using a default template, scanning the whole code base.
The new revision is deployed to the remote location via FTP (Including some directory renaming, chmodding, importing databases, and the likes.) This is something I already like phingfor very much, but I'm open for alternatives of course.
Unit tests residing in a predefined location are run. I am informed about their failure or success using E-Mail, RSS or (preferably) HTML output that I can grab and put into a web page.
(optionally) a end-user "changelog" text file in a pre-defined location gets updated with a pre-defined part of the commit message ("It is now possible to filter for both "foo" and "bar" at the same time). This message is not necessarily identical with the SVN commit message, which probably contains much more internal information.
Stuff like code metrics, code style checking and so on are not my primary focus right now, but on the long run, they certainly will. Solutions that bring this out-of-the-box are very kindly looked upon.
在本地测试后,变更集会被签入 SVN。
我开始构建过程。SVN HEAD 修订版被检出,必要时进行修改,并准备好上传。
API 文档是自动生成的——如果我还没有详细设置它,使用默认模板,扫描整个代码库。
新版本通过 FTP 部署到远程位置(包括一些目录重命名、chmodding、导入数据库等。)这是我已经非常喜欢phing的东西,但我当然愿意接受其他选择。
运行驻留在预定义位置的单元测试。我使用电子邮件、RSS 或(最好)HTML 输出,我可以获取并放入网页中,了解他们的失败或成功。
(可选)预定义位置中的最终用户“更改日志”文本文件将使用提交消息的预定义部分进行更新(“现在可以同时过滤“foo”和“bar”时间)。这个消息不一定与 SVN 提交消息相同,它可能包含更多的内部信息。
像代码度量、代码样式检查等内容现在不是我的主要关注点,但从长远来看,它们肯定会。非常友好地看待带来这种开箱即用的解决方案。
I am looking for
我在寻找
Feedback and experiencesfrom people who are or were in a similar situation, and have successfully implemented a solution for this
Especially, good step-by-step tutorials and walkthroughson how to set this up
Solutions that provide as much automation as possible, for example by creating a skeleton API, test cases and so on for each new project.
来自现在或曾经处于类似情况并已成功实施解决方案的人的反馈和经验
特别是有关如何设置的良好分步教程和演练
提供尽可能多的自动化的解决方案,例如通过为每个新项目创建框架 API、测试用例等。
and also
并且
- Product recommendations. What I know so far is phing/ant for building, and phpUnderControlor Hudsonfor the reporting part. I like them all as far as I can see, but I have of course no detailed experience with them.
- 产品推荐。到目前为止,我所知道的是phing/ant 用于构建,以及phpUnderControl或Hudson用于报告部分。就我所见,我都喜欢它们,但我当然对它们没有详细的经验。
I am swampedwith work, so I have a strong inclination towards simple solutions. On the other hand, if a feature is missing, I'll cry about it being too limited. :) Point-and-click solutions are welcome, too. I am also to commercial product recommendations that can work with PHP projects.
我忙于工作,所以我非常倾向于简单的解决方案。另一方面,如果缺少某个功能,我会为它太有限而哭泣。:) 也欢迎点击式解决方案。我还推荐可以与 PHP 项目一起使用的商业产品。
My setup
我的设置
I am working on Windows locally (7, to be exact) and most client projects are run on a LAMP stack, often on shared hosting (= no remote SSH). I am looking for solutions that I can run in my own environment. I am ready to set up a Linux VM for this, no problem. Hosted solutions are interesting for me only if they provide all of the aspects described, or are flexible enough to interact with the other parts of the process.
我在本地使用 Windows(准确地说是 7),并且大多数客户端项目都在 LAMP 堆栈上运行,通常在共享主机上运行(= 没有远程 SSH)。我正在寻找可以在我自己的环境中运行的解决方案。我已准备好为此设置 Linux VM,没问题。只有当托管解决方案提供所描述的所有方面,或者足够灵活以与流程的其他部分进行交互时,托管解决方案才对我感兴趣。
BountyI am accepting the answer that I feel will give me the most mileage. There is a lot of excellent input here, I wish I could accept more than one answer. Thanks everyone!
赏金我接受我认为会给我最大里程的答案。这里有很多优秀的输入,我希望我能接受不止一个答案。谢谢大家!
采纳答案by Steen
I've been through buildbot, CruiseControl.net, CruiseControland Hudson. All though I really liked CruiseControl*, it was just too much of a hassle with really complex dependency cases. buildbot is not easy to set up, but it's got a nice aura (I just like python, that's all). But hudson won over the former three because:
我已经通过buildbot、CruiseControl.net、CruiseControl和Hudson。虽然我真的很喜欢 CruiseControl*,但对于非常复杂的依赖情况来说,它实在是太麻烦了。buildbot 不容易设置,但它有一个很好的光环(我只是喜欢 python,仅此而已)。但是哈德森战胜了前三个,因为:
- It's just easy to set up
- It's easy to customize
- It looks good and got nice overview functionality
- It got point-and-click updates, for itself and all installed plugins. This is a really nice feature, that I appreciate more and more
- 设置简单
- 很容易定制
- 它看起来不错并且有很好的概览功能
- 它为自身和所有已安装的插件提供了点击式更新。这是一个非常好的功能,我越来越欣赏它
Caveat: I only ever used linux as base for the above mentioned build servers (CC.net ran on mono), but they should all - according to the docs - run cross-platform.
警告:我只使用 linux 作为上述构建服务器的基础(CC.net 在mono 上运行),但它们都应该 - 根据文档 - 跨平台运行。
Setting up a hudson server
设置哈德森服务器
Prerequisites:
先决条件:
- Java (1.5 will serve you just fine)
- Read access to the subversion server (I have a separate account for the hudson user)
- Java(1.5 会很好地为您服务)
- 对 subversion 服务器的读取权限(我有一个单独的 hudson 用户帐户)
From here, it's just:
从这里开始,它只是:
java -jar hudson.war
This will run a small server instance right off your console, and you should be able to browse the installation at your http://localhost:8080, if you don't have anything else running on that port in advance (you can specify another port by passing the --httpPort=ANOTHER_HTTP_PORToption to the above command) and everything went well in the 'installation' process.
这将运行一个小的服务器实例马上你的控制台,你应该能够浏览安装在你的http://localhost:8080,如果你没有别的事先在该端口上运行(您可以通过传递指定另一个端口--httpPort=ANOTHER_HTTP_PORT选项给以上命令)并且在“安装”过程中一切顺利。
If you go to the available plugins directory (http://localhost:8080/pluginManager/available), you'll find plugins for supporting your above mentioned tasks (subversion support is installed per default).
如果您转到可用的插件目录 ( http://localhost:8080/pluginManager/available),您将找到支持上述任务的插件(默认情况下会安装 subversion 支持)。
If that has whet you appetite, you should install a java application server, such as tomcator jetty. Installation instructionsare available for all major application servers
如果这让你胃口大开,你应该安装一个 java 应用服务器,比如tomcat或jetty。适用于所有主要应用程序服务器的安装说明
Update: Kohsuke Kawaguchihas constructed a windows service installerfor hudson
更新:Kohsuke Kawaguchi为 hudson构建了一个Windows 服务安装程序
Setting up a project in hudson
在哈德森建立一个项目
The links in the following walk-through assumes a running instance of hudson located at http://localhost:8080
以下演练中的链接假设 hudson 的运行实例位于 http://localhost:8080
- Select new Job (
http://localhost:8080/view/All/newJob) from the menu on the left - Give the job a name and tick
Build a free-style software projecton the list - Pressing 'ok' will take you to the configuration page of the job. All the options have a little question mark besides them. Pressing this will bring up a help text regarding the option.
- Under the option group 'Source Code Management' you would be using Subversion. Hudson accepts both url access as well as local module access
- Under the option group 'Build Triggers', you would use 'Poll SCM'. The syntax used here is that of cron, so polling the subversion repository every 5 minutes would be
*/5 * * * * - The process of building the project is specified under the option group 'Build'. If you already have an ant build file with all the targets you need, you're in luck. Just choose 'Invoke ant' and write the name of the target. The option group supports maven and shell commands as well out of the box, but there is also a plugin available for phing.
- Tick off additional build actions in 'Post Build Actions', such as e-mail notifications or archiving of build artefacts.
http://localhost:8080/view/All/newJob从左侧菜单中选择新作业 ( )- 为工作命名并
Build a free-style software project在列表上打勾 - 按“确定”将带您进入作业的配置页面。除了它们之外,所有选项都有一个小问号。按此将显示有关该选项的帮助文本。
- 在选项组“源代码管理”下,您将使用 Subversion。Hudson 接受 url 访问以及本地模块访问
- 在选项组“构建触发器”下,您将使用“轮询 SCM”。这里使用的语法是 cron 的语法,因此每 5 分钟轮询一次 subversion 存储库将是
*/5 * * * * - 构建项目的过程在选项组“构建”下指定。如果您已经有一个包含您需要的所有目标的 ant 构建文件,那么您很幸运。只需选择“调用蚂蚁”并写下目标的名称。选项组也支持开箱即用的 maven 和 shell 命令,但也有一个插件可用于 phing。
- 在“构建后操作”中勾选其他构建操作,例如电子邮件通知或构建工件存档。
For setting up processes for which hudson have no plugins, you can either call them directly through a shell script from within the build setup, or you could write you own plugin
对于设置 hudson 没有插件的进程,您可以直接通过构建设置中的 shell 脚本调用它们,或者您可以编写自己的插件
Pitfalls:
陷阱:
- If you have it produce build artefacts, remember to have hudson clean up after itself in regular intervals.
- If you have more than 20 projects set up, consider notdisplaying their build status as the default main page on hudson
- 如果您让它产生构建工件,请记住让 hudson 定期对其进行清理。
- 如果您设置了 20 个以上的项目,请考虑不要将它们的构建状态显示为 hudson 的默认主页
Good luck!
祝你好运!
回答by Michiel
The term you are looking for is "continous integration."
您正在寻找的术语是“持续集成”。
Here is an example of someone who uses GIT + phpundercontrol: http://maff.ailoo.net/2009/09/continuous-integration-phpundercontrol-git/
下面是一个使用 GIT + phpundercontrol 的例子:http://maff.ailoo.net/2009/09/continuous-integration-phpundercontrol-git/
CruiseControl (which is a CI server), can use Hosted SVN/GIT as a source. So you can even use it with GitHub or Beanstalk or something else.
CruiseControl(这是一个 CI 服务器),可以使用 Hosted SVN/GIT 作为源。所以你甚至可以将它与 GitHub 或 Beanstalk 或其他东西一起使用。
Then you can integrate that with the following kind of software:
然后,您可以将其与以下类型的软件集成:
- PHPUnit
- php-codesniffer
- phpdocumentor
- PHP Gcov
- PHPXref
- Yasca
- etc.
- PHP单元
- php代码嗅探器
- php文档
- PHP Gcov
- PHP外部参照
- 雅斯卡
- 等等。
You could also try this hosted CI: http://www.php-ci.net/hosting/create-project
你也可以试试这个托管 CI:http: //www.php-ci.net/hosting/create-project
Keep in mind though, that those tools need custom support if you integrate them yourself.
但请记住,如果您自己集成这些工具,则需要自定义支持。
Have you also thought about project management and patch management?
您是否也考虑过项目管理和补丁管理?
You can use Redmine for project management. It has integrated continuous integration support, but only as client side (not as CI server).
您可以使用 Redmine 进行项目管理。它集成了持续集成支持,但仅作为客户端(而不是 CI 服务器)。
Try using a hosted SVN/GIT/etc. solution, because they will cover your backups and keep their servers running, so you can focus on development.
尝试使用托管的 SVN/GIT/等。解决方案,因为它们将覆盖您的备份并保持其服务器运行,因此您可以专注于开发。
For a tutorial on how to setup Hudson, see: http://toptopic.wordpress.com/2009/02/26/php-and-hudson/
有关如何设置 Hudson 的教程,请参阅:http: //toptopic.wordpress.com/2009/02/26/php-and-hudson/
回答by Steven Rosato
I use Atlassian's Bamboocontinous integration server for my main PHP project (along with their other products such as fisheye(repository browsing), jira(issue tracker) and clover(code coverage)).
我将 Atlassian 的Bamboo连续集成服务器用于我的主要 PHP 项目(以及他们的其他产品,如鱼眼(存储库浏览)、jira(问题跟踪器)和三叶草(代码覆盖率))。
It supports SVN and now supports Git and it has a great user interface. It is available for linux, windows and mac and can run standalone on its own tomcat server which is great for people (like me) who does not like to take days to setup their tools). Although it may look expensive, being a lone developer myself I purchased the starter kit license for 10$ (10$ by software). This is great for small teams and it is worth the look.
它支持 SVN,现在支持 Git,并且它有一个很棒的用户界面。它适用于 linux、windows 和 mac,并且可以在自己的 tomcat 服务器上独立运行,这非常适合不喜欢花几天时间设置工具的人(比如我)。虽然它可能看起来很贵,但作为一个单独的开发人员,我以 10 美元(软件为 10 美元)的价格购买了入门套件许可证。这对小团队来说非常棒,值得一看。
回答by Ajay Patel
PHPTesting PHPCIThis is nice, continuous integration server built in php.
PHPTesting PHPCI这是用 php 构建的不错的持续集成服务器。
Plus, its free and open source. :)
另外,它是免费和开源的。:)
it has number of plugins..
它有许多插件..
PHPCI includes integration plugins for:
PHPCI 包括以下集成插件:
- Atoum
- Behat
- Campfire
- Codeception
- Composer
- Grunt
- IRC
- PHP
- Lint
- MySQL
- PDepend
- PostgreSQL
- PHP Code Sniffer
- PHP Copy/Paste Detector
- PHP Spec
- PHP Unit
- Shell Commands
- Tar / Zip
- 原子
- 贝哈特
- 营火
- 代码接收
- 作曲家
- 电子邮件
- 咕噜声
- IRC
- PHP
- 皮棉
- MySQL
- 依赖
- PostgreSQL
- PHP代码嗅探器
- PHP 复制/粘贴检测器
- PHP规范
- PHP单元
- 外壳命令
- 焦油/邮编
回答by AgileDeveloper
I would suggest using Jenkins http://jenkins-ci.org/it's free and it's open source.
我建议使用 Jenkins http://jenkins-ci.org/它是免费的并且是开源的。
It's pretty straight forward to setup, works on multiple platforms and integrates well with other continuous integration tools like SonarQube (+ SQUALE) to measure technical debt and Thucydides for testing automation.
它的设置非常简单,可在多个平台上运行,并与其他持续集成工具(如 SonarQube (+ SQUALE))很好地集成以测量技术债务和修昔底德以进行自动化测试。
I would highly suggest using GIT or GIT Hub for version control instead of SVN. From my point of view it's just a better version control system that will help you scale your development efforts later.
我强烈建议使用 GIT 或 GIT Hub 而不是 SVN 进行版本控制。在我看来,它只是一个更好的版本控制系统,可以帮助您以后扩展开发工作。
Since you're working mostly with PHP project there are some other tools you can use.
由于您主要使用 PHP 项目,因此您可以使用其他一些工具。
PHPUnit - For unit testing
PHPUnit - 用于单元测试
PHP CodeSniffer - Check for coding standards
PHP CodeSniffer - 检查编码标准
PHP Depend - Shows your PHP code dependencies
PHP Depend - 显示您的 PHP 代码依赖项
XDEBUG - For performance testing
XDEBUG - 用于性能测试
All of these tools and be triggered with a Jenkins job and helps with the quality and performance of your code.
所有这些工具都可以通过 Jenkins 作业触发,并有助于提高代码的质量和性能。
Good luck and Enjoy!
祝你好运和享受!
回答by Phil Wallach
I do not use many of the products, or even types of products that you use, but I will give you my experience.
我不使用很多产品,甚至你使用的产品类型,但我会给你我的经验。
I run a TEST environment in parrallel with my PROD environment. I have no local testing per se. If it is too hard to get soemthing up into a real TEST environment, then I fix my build process. I don't see the point in testing locally, as the environments are different. UPDATE: The only thing I do locally is run "php -l" before I upload anything. Stops the stupid mistakes.
我运行与我的 PROD 环境并行的 TEST 环境。我本身没有本地测试。如果很难进入真正的 TEST 环境,那么我会修复我的构建过程。我认为在本地测试没有意义,因为环境不同。更新:我在本地做的唯一一件事就是在上传任何东西之前运行“php -l”。停止愚蠢的错误。
The build process works with whatever is in the current workspace, which includes uncommitted code. This is not everyone's cup of tea, but I am going to TEST very often. Everything gets committed before going to PROD.
构建过程适用于当前工作区中的任何内容,包括未提交的代码。这不是每个人的一杯茶,但我会经常测试。在进入 PROD 之前,一切都会被提交。
Part of my build process (similar to yours) creates two META files. One contains the last (typically) 100 changes and also gives me the current changelist number. The shows me what changes are installed. The other contains the CLIENTSPEC (in Perforce terms) which shows me exactly what branches were used in this build. Together these give me reproducible builds.
我的构建过程的一部分(类似于你的)创建了两个 META 文件。一个包含最后(通常)100 个更改,还给我当前的更改列表编号。显示了安装了哪些更改。另一个包含 CLIENTSPEC(在 Perforce 术语中),它准确地向我展示了在这个构建中使用了哪些分支。这些一起为我提供了可重复的构建。
I do not build straight to the target environment, but to a staging area on the server. I use SSH so this makes sense. This gives me a few advantages. Most importantly it avoids dying half way through a large upload. It also gives me a place to store META files, and all the build files are automatically archived (so I can go straight back to any build). The script also logs the update (so there is an entry in the log stream and I can see pre- and post-) and kicks all daemons (I use daemontools so "svc -t"). All of these are better off on the target machine.
我不会直接构建到目标环境,而是构建到服务器上的临时区域。我使用 SSH,所以这是有道理的。这给了我一些好处。最重要的是,它可以避免在大型上传过程中途死亡。它还为我提供了一个存储 META 文件的地方,并且所有构建文件都会自动存档(因此我可以直接返回到任何构建)。该脚本还会记录更新(因此日志流中有一个条目,我可以看到 pre- 和 post-)并启动所有守护进程(我使用 daemontools 所以“svc -t”)。所有这些在目标机器上都更好。
One other issue is DB changes. I keep a master script of the DB schema, which I update every time the schema changes. Each of the changes also go into a changes.sql script, which is uploaded with the build to the staging area. The script is run as part of the install script.
另一个问题是数据库更改。我保留了数据库架构的主脚本,每次架构更改时都会更新该脚本。每个更改也会进入一个 changes.sql 脚本,该脚本与构建一起上传到暂存区。该脚本作为安装脚本的一部分运行。
回答by Stephan
I am mostly a sys admin but sometimes I code PHP as well. As a side project I created some scripts that will make it simple and painless to set up a full blown PHP CI environment using Jenkins. It also runs a sample project for you so you can see how each build step is configured.
我主要是系统管理员,但有时我也会编写 PHP 代码。作为一个附带项目,我创建了一些脚本,这些脚本将使使用 Jenkins 设置完整的 PHP CI 环境变得简单而轻松。它还为您运行一个示例项目,以便您可以查看每个构建步骤的配置方式。
If you want to try it out all you need is a Debian/Ubuntu box and shell access.
如果您想尝试一下,您只需要一个 Debian/Ubuntu 盒子和 shell 访问。
http://yauh.de/articles/379/setting-up-a-ci-environment-for-php-projects-using-jenkins-ci
http://yauh.de/articles/379/setting-up-a-ci-environment-for-php-projects-using-jenkins-ci
UpdateTo add some content to my answer:
更新在我的答案中添加一些内容:
You can simply set up a Jenkins CI for PHP using Ansible. Since v1.4 it supports roles which you can download from their galaxy.ansibleworks.com community site and it will do the heavy lifting for you. It is called jenkins-php.
您可以使用 Ansible 简单地为 PHP 设置 Jenkins CI。从 v1.4 开始,它支持您可以从他们的galaxy.ansibleworks.com 社区站点下载的角色,它将为您完成繁重的工作。它被称为jenkins-php。
回答by yegor256
Consider fazend.com, a free hosted CI platform, which automates configuration and installation procedures. You don't need to setup version control, bug tracking, CI server, test environment, etc. Everything is done on-demand.
考虑一下fazend.com,这是一个免费的托管 CI 平台,它可以自动化配置和安装过程。您不需要设置版本控制、错误跟踪、CI 服务器、测试环境等。一切都是按需完成的。
回答by Adam Hopkinson
I've recently begun the same kind of process, and am using Beanstalkfor svn hosting.
我最近开始了同样的过程,并且正在使用Beanstalk进行 svn 托管。
There are two nifty features in the paid accounts (start at $15pm i think):
付费帐户有两个很棒的功能(我认为从下午 15 美元开始):
- deployment allows the user to create ftp targets for staging and production servers, which can be deployed at the click of a button (inc specifying a revision and branch)
- webhooks allow the user to set up a url that is called on each commit/deploy, passing across things like revision number, description and user. This could be used to update docs, run unit tests and update changelogs.
- 部署允许用户为临时和生产服务器创建 ftp 目标,可以通过单击按钮进行部署(包括指定修订和分支)
- webhooks 允许用户设置在每次提交/部署时调用的 url,传递诸如修订号、描述和用户之类的内容。这可用于更新文档、运行单元测试和更新变更日志。
I'm sure there are other hosted or self-hosting svn servers with these two features, but beanstalk is the one i have experience of and it's working very, very well
我确信还有其他具有这两个功能的托管或自托管 svn 服务器,但 beanstalk 是我有经验的一种,并且它运行得非常非常好
There's also an API, which I imagine could be used to integrate deployment further in to your process.
还有一个 API,我想它可以用于将部署进一步集成到您的流程中。

