与PHP-CI的持续集成

Creating an application is one thing. Keeping it to a certain quality level is another thing entirely. These days, you can find many tools which can help you to keep the quality of your application in shape. Running these tools one by one can be very time consuming. For that, you can install so called continuous integration (CI) services. PHPCI is one of those and in this article, we will dive into it.

创建应用程序是一回事。 将其保持在一定的质量水平完全是另一回事。 如今,您可以找到许多工具来帮助您保持应用程序质量。 一对一地运行这些工具可能非常耗时。 为此,您可以安装所谓的持续集成(CI)服务。 PHPCI就是其中之一,在本文中,我们将深入探讨它。

持续整合服务 (Continuous integration service)

A continuous integration service is an application which runs certain quality check tools against your code. For example, a CI could pull in your git repository. When done, it runs unit tests, checks your code for validations and generates reports based on it. In general, a CI runs on certain time intervals or on every push. The most seen situation is right after creating a merge request. By checking the merge request, the code is checked before being merged, making sure you are not accepting code which could break functionality. So integrating CI within your development procedure can make sure bad code is kept out of your main repository and you can validate automatically if everything meets your requirements before accepting changes.

持续集成服务是一种针对您的代码运行某些质量检查工具的应用程序。 例如,配置项可以引入您的git存储库。 完成后,它将运行单元测试,检查您的代码以进行验证并基于该代码生成报告。 通常,配置项按特定的时间间隔或每次推送运行。 最常见的情况是在创建合并请求之后。 通过检查合并请求,可以在合并之前检查代码,确保您不接受可能破坏功能的代码。 因此,将CI集成到开发过程中可以确保将错误代码排除在主存储库之外,并且在接受更改之前,您可以自动验证所有内容是否都满足您的要求。

安装 (Installation)

Installation can be done in two different ways. Either you download the latest release or you follow the installation guide. I decided to go with the installation guide and install it through Composer. After running composer update you can run the install script. You will be questioned for database credentials and your email address. When done, a user is created with the given email address.

安装可以通过两种不同的方式完成。 您可以下载最新版本,也可以遵循安装指南 。 我决定使用安装指南,并通过Composer进行安装。 运行composer update您可以运行安装脚本。 您将被问及数据库凭据和您的电子邮件地址。 完成后,将使用给定的电子邮件地址创建一个用户。

You also need to set a cronjob so builds are run automatically.

您还需要设置cronjob,以便自动运行构建。

建立 (Setup)

Depending on what you want to do, you have to install some tools. After logging in, you can go to admin manage plugins and install any necessary plugins. By installing a plugin, you are updating the composer.json file with new requirements. So right after you installed the plugins you need to run composer update to actually install these plugins.

根据您要执行的操作,您必须安装一些工具。 登录后,您可以转到admin manage plugins并安装任何必要的插件。 通过安装插件,您将以新的要求更新composer.json文件。 因此,在安装插件之后,您需要运行composer update来实际安装这些插件。

Plugins

新增专案 (Add a project)

By clicking the add project button in the header, you can create a new project. You have to fill in a simple form, which indicates were the code is located. You can choose between different kinds of services like Github and Bitbucket, but also for your own remote or local URLs. If you don’t have a phpci.yml configuration file within your repository, you need to provide the build configuration. Within his configuration, you define how the project needs to be set up, which tools you want to run and how to finalize the build.

通过单击标题中的add project按钮,可以创建一个新项目。 您必须填写一个简单的表单,该表单指示代码所在的位置。 您可以选择各种服务(例如Github和Bitbucket),也可以选择自己的远程或本地URL。 如果您的存储库中没有phpci.yml配置文件,则需要提供构建配置。 在他的配置中,您可以定义如何设置项目,要运行哪些工具以及如何完成构建。

Each build process consists of 5 phases.

每个构建过程包括5个阶段。

  1. Setup. The phase were everything is initialized

    建立。 一切都已初始化的阶段
  2. Test. The phase were all tests are executed

    测试。 该阶段所有测试均已执行
  3. Complete. Success or failure, this part will always run

    完成。 成功或失败,这部分将始终运行
  4. Success. Will only be run in case of success

    成功。 仅在成功的情况下运行
  5. Failure. Will only be run in case of failure

    失败。 仅在失败的情况下运行

I will be using this project as our project. We want to ignore default directories like app and vendor. Installation will be done through Composer. The project should be PSR2 compliant, has unit tests and contains decent docblocks. We also want to check if the overall quality is fine by running PHPMD, PHPCPD and PHPLoc.

我将把这个项目用作我们的项目。 我们要忽略默认目录,例如appvendor 。 安装将通过Composer完成。 该项目应符合PSR2,具有单元测试并包含适当的docblock。 我们还想通过运行PHPMD,PHPCPD和PHPLoc检查整体质量是否良好。

PHPCI is capable of handling a test database. However, the project we are using in our example does not have any functional tests, so we will leave out the MySQL connection.

PHPCI能够处理测试数据库。 但是,我们在示例中使用的项目没有任何功能测试,因此我们将省略MySQL连接。

Let’s take a look at what the configuration would look like.

让我们看一下配置的外观。

build_settings:
    ignore:
        - "vendor"
        - "bin"
        - "app"

setup:
    composer:
        action: "install"

test:
    php_unit:
        config:
            - "app/phpunit.xml.dist"
        coverage: "coverage"
        args: "--stderr"
    php_mess_detector:
        allow_failures: true
    php_code_sniffer:
        standard: "PSR2"
    php_cpd:
        allow_failures: true
    php_docblock_checker:
        allowed_warnings: 10
        skip_classes: true
    php_loc:
        directory: "src"

After creating the project, you have to make sure that PHPCI is capable of retrieving the project from Github. In this case, I added the SSH key which PHPCI supplied me to my Github account. When done, you can click the build button to start building the project.

创建项目后,必须确保PHPCI能够从Github检索项目。 在这种情况下,我将PHPCI提供给我的SSH密钥添加到我的Github帐户中。 完成后,您可以单击构建按钮以开始构建项目。

建立结果 (Build results)

When the build is completed, the project will get a color matching the status on the overview page. You get a clear view of the previous build, the latest commit and the current branch. By clicking on the ID of a build, you can see the in depth results for the build.

构建完成后,项目将获得与概述页面上的状态匹配的颜色。 您可以清楚地了解以前的版本,最新的提交和当前分支。 通过单击构建的ID,您可以查看构建的深入结果。

Overview

Time to take a closer look on the results of a single build. Near the top of the page, you should be able to see two small graphs. One will indicate the lines of code while the other one will display the quality trend. The quality trend graph contains an overview of all the tools like PHPMD and PHPCPD.

是时候仔细研究一下单个构建的结果了。 在页面顶部附近,您应该能够看到两个小图。 一个将指示代码行,而另一个将显示质量趋势。 质量趋势图包含PHPMD和PHPCPD等所有工具的概述。

Graph

Unfortunately, I was unable to get PHPLoc working correctly

不幸的是,我无法使PHPLoc正常工作

Beneath the graphs, we can see in depth results per tool. For example we can have a look at the unit tests. In this case, I was aware of the fact that they should succeed. Within PHPCI, this was also the case. PHPCI indicates this, by leaving the table empty.

在图表下方,我们可以看到每个工具的深​​度结果。 例如,我们可以看一下单元测试。 在这种情况下,我知道他们应该成功。 在PHPCI中,情况也是如此。 PHPCI通过将表保留为空来表明这一点。

PHPUnit

In case of a failure, PHPCI will clearly indicate what is wrong by retrieving feedback from the tool. In my case, PHPMD had some lines which they think could be improved. PHPCI shows me which file and what the message is. Next to that, I am able to click the file to go straight to my Github repository.

万一发生故障,PHPCI将从工具中获取反馈,以明确指出问题所在。 就我而言,PHPMD有一些可以改进的地方。 PHPCI向我显示了哪个文件和消息是什么。 紧接着,我可以单击该文件直接进入我的Github存储库。

PHPUnit

All the tools I used gave the same way feedback to me. Tables which are empty where successful, tables which were filled had some improvements.

我使用的所有工具都以相同的方式反馈给我。 成功的地方为空的表,已填充的表都有一些改进。

结论 (Conclusion)

Of course, PHPCI can do more than what was described above. If you are interested in all its features, have a look at their documentation. Basically, it can use any PHP QA tool you know.

当然,PHPCI可以做的比上述更多。 如果您对其所有功能都感兴趣,请参阅其文档 。 基本上,它可以使用您知道的任何PHP QA工具。

So, what’s my final verdict? I have been using lots of tools in the past. A year ago, I was convinced that there was no single tool that could beat Jenkins. Till today, I still believe Jenkins is one of the best CI tools out there. Jenkins is capable of running all kinds of projects, not only PHP based ones. For me, that is a huge advantage when you are dealing with JS, iOS and Java applications. Jenkins is full featured, contains a lot of plugins and is capable of creating nice graphs about the quality of your project. It only lacks a nice and structured design and easy configuration.

那么,我的最终判决是什么? 过去我一直在使用许多工具。 一年前,我坚信没有哪个工具可以击败詹金斯。 直到今天,我仍然相信Jenkins是目前最好的CI工具之一。 Jenkins能够运行各种项目,不仅是基于PHP的项目。 对我来说,当您处理JS,iOS和Java应用程序时,这是一个巨大的优势。 Jenkins功能齐全,包含许多插件,并且能够创建有关项目质量的漂亮图表。 它仅缺少一个很好的结构化设计和易于配置的内容。

I also love Travis in combination with Scrutinizer and SensioLabs Insight. Together they can give me a lot of feedback on my code and run all kinds of checks. However, if you are not working with open source projects, you have to pay for all three services to keep them running.

我也喜欢Travis结合ScrutinizerSensioLabs Insight 。 他们在一起可以给我很多有关我的代码的反馈,并可以进行各种检查。 但是,如果您不使用开源项目,则必须为这三项服务付费,以使其保持运行状态。

So, is PHPCI a nice addition in this line of tools? Yes, I believe so. They are really trying to target the PHP community and are offering one, single place, to run all kinds of checks. Next to that, it can already produce some graphs to give a quick overview of the quality of your project.

那么,PHPCI是这一系列工具的不错的补充吗? 是的,我相信。 他们确实在尝试针对PHP社区,并提供了一个单一的位置来运行各种检查。 除此之外,它已经可以生成一些图形,以快速概述项目的质量。

Don’t forget, PHPCI is not even a year old yet and that’s something you will notice. Installing a plugin requires you to run composer update, which means you still have to log in to your server. Also, the design sometimes is really off, especially when you are installing a package and you need to pick a version.

不要忘记,PHPCI甚至还不到一岁,您会注意到这一点。 安装插件需要您运行composer update ,这意味着您仍然必须登录到服务器。 另外,有时设计有时会停止运行,尤其是在安装软件包并需要选择版本时。

Plugins

I would like to see some more helpful information on certain topics. It’s nice to know you have to fill in a configuration file, but if it could show you the possibilities or at least add a link to the documentation, that would help a lot. You might even wonder why the documentation is not integrated within the tool itself.

我希望看到有关某些主题的更多有用信息。 很高兴知道您必须填写配置文件,但是如果它可以向您显示可能的情况或至少添加了指向文档的链接,那将会很有帮助。 您甚至可能想知道为什么文档未集成在工具本身中。

The most frustrating part for me was the fact that I received messages like Plugin Status: Success and Plugin Status: Failed in my console and the build was giving me an empty table.

对我来说,最令人沮丧的部分是,我在控制台中收到了诸如“ Plugin Status: Success和“ Plugin Status: Failed ,并且构建给我一个空表。

PHPUnit

I believed it meant that it somehow failed to run a tool like PHPUnit. Yet it tells you if PHPUnit actually succeeded or failed. A success also leads to an empty table, so you basically have no feedback in PHPCI itself. Would be nice if it just said something like “everything is ok…. moving on!” or at least gave me the results of PHPUnit.

我相信这意味着它以某种方式无法运行PHPUnit之类的工具。 但是它告诉您PHPUnit是成功还是失败。 成功也将导致一个空表,因此您基本上对PHPCI本身没有任何反馈。 如果它只是说“一切都好...”,那将是很好的。 继续!” 或至少给了我PHPUnit的结果。

Finally, it’s good to see the developers are also extending it with custom tools. For example, they created a docblock plugin. For now, I believe it only checks if a docblock is present or not. Would be nice if they could check all docblocks in the future to match the upcoming PSR-5 standard.

最后,很高兴看到开发人员也在使用自定义工具对其进行扩展。 例如,他们创建了一个docblock插件 。 就目前而言,我相信它只会检查是否存在文档块。 如果他们将来可以检查所有文档块以匹配即将推出的PSR-5标准,那就太好了。

If you don’t want to set up Jenkins or you have a private project and you are only developing PHP projects, PHPCI could be the tool you are looking for. Personally I would wait a bit longer until they improve the design of the application before using it on a real project. It sometimes feels a bit clunky because the design is unclear or there is no correct description.

如果您不想设置Jenkins,或者您有一个私人项目,而您仅在开发PHP项目,则可以使用PHPCI。 就我个人而言,我将等待更长的时间,直到他们改进应用程序的设计,然后再在实际项目中使用它。 有时感觉有点笨拙,因为设计不清楚或没有正确的描述。

Are you currently using a CI tool? Do you think PHPCI could replace your current CI? I would love to hear from you in the comments below.

您当前正在使用CI工具吗? 您认为PHPCI可以代替您当前的CI吗? 我希望在下面的评论中听到您的意见。

翻译自: https://www.sitepoint.com/continuous-integration-php-ci/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值