jenkins 构建php_在Jenkins中准备和构建PHP项目

jenkins 构建php

In a previous article, we went through the installation of Jenkins and prepared everything to get started. We will continue in this article by actually preparing our project. If you have a Jenkins setup ready from before, no need to go through the previous part – you can follow along with this one quite nicely.

上一篇文章中 ,我们经历了Jenkins的安装过程,并准备了一切入门。 我们将通过实际准备项目来继续本文。 如果您之前已经准备好了Jenkins设置,则无需遍历上一部分-您可以很好地遵循这一部分。

准备作曲家 (Prepare composer)

We are going to prepare our project first. We could use the project supplied by Sebastian Bergmann, but it’s rather a basic project and will run without problems in Jenkins. In this article, we will be using a different repository in which we will have to overcome some hurdles. We will be using a fork of Jumph as our basis.

我们将首先准备我们的项目。 我们可以使用塞巴斯蒂安·伯格曼(Sebastian Bergmann)提供的项目 ,但这是一个基本项目,并且在詹金斯(Jenkins)中不会有任何问题。 在本文中,我们将使用一个不同的存储库,其中必须克服一些障碍。 我们将使用Jumph的分支作为基础。

First and foremost, we have to make sure that we have tools like PHPUnit and PHP-CodeSniffer available. We can do this in two different ways. Either we install the packages on our Jenkins server through Pear or Composer globally, or we define the dependencies in our composer.json file. I decided to go with the latter method, so we can easily control the version of these tools on our own local machine as well as on Jenkins. So we start by adding the following lines to the composer.json file and run composer update.

首先,我们必须确保有可用的工具,例如PHPUnit和PHP-CodeSniffer。 我们可以通过两种不同的方式来做到这一点。 我们要么通过Pear或Composer全局将软件包安装在我们的Jenkins服务器上,要么在composer.json文件中定义依赖项。 我决定采用后一种方法,因此我们可以轻松地在自己的本地计算机以及Jenkins上控制这些工具的版本。 因此,我们首先将以下行添加到composer.json文件中,然后运行composer update

"require-dev": {
    "squizlabs/php_codesniffer": "~1.5.*",
    "phpmd/phpmd": "~2.1.*",
    "sebastian/phpcpd": "~2.0.*",
    "pdepend/pdepend": "~2.0.*",
    "phploc/phploc": "~2.0.*",
    "phpunit/phpunit": "~4.3.*",
    "theseer/phpdox": "~0.7.*",
},

If you want to know more about what each package does, have a look at Bruno’s article about PHP QA tools.

如果您想进一步了解每个软件包的功能,请参阅Bruno的有关PHP QA工具的文章。

准备蚂蚁 (Prepare Ant)

On our Jenkins server, we will be using Ant. So in case Ant is not yet installed on your server, make sure it is by running the following command.

在我们的Jenkins服务器上,我们将使用Ant 。 因此,如果您的服务器上尚未安装Ant,请通过运行以下命令来确保它已安装。

sudo apt-get install ant

Ant helps you automate the software build process. You define tasks, called targets, in the configuration file. Ant will read this file and perform the appropriate action. You can define dependencies to indicate in which order Ant should perform these tasks. The configuration file is the so called build.xml file. We are going to add this to our project, so Ant can perform these tasks on our Jenkins server. We are going to use the template provided by Sebastian Bergmann which can be found here. We add this to the root of our project.

Ant可帮助您自动化软件构建过程。 您可以在配置文件中定义称为目标的任务。 Ant将读取此文件并执行适当的操作。 您可以定义依赖关系以指示Ant应该按哪些顺序执行这些任务。 配置文件是所谓的build.xml文件。 我们将把它添加到我们的项目中,以便Ant可以在Jenkins服务器上执行这些任务。 我们将使用Sebastian Bergmann提供的模板,可以在这里找到。 我们将此添加到项目的根目录中。

To run Ant, you can kick off ant build on the command line. This means that Ant will run the build target. If no parameter is given, Ant will run the target as indicated in the configuration as default, which in this case is also build.

要运行Ant,您可以在命令行上启动ant build 。 这意味着Ant将运行build目标。 如果未提供任何参数,Ant将按照配置中的指示运行目标(默认情况下,在本例中也是build

Let’s take a closer look at the build target.

让我们仔细看看build目标。

<target name="build" depends="prepare,lint,phploc-ci,pdepend,phpmd-ci,phpcs-ci,phpcpd-ci,phpunit,phpdox" description=""/>

The target is empty, however, it depends on a lot of other targets. It will first run the prepare target and then continue with the lint target until every target has been run. Let’s have a look at the phploc-ci target for example.

目标为空,但是,它取决于许多其他目标。 它将首先运行prepare目标,然后继续执行lint目标,直到运行完每个目标为止。 让我们看看例如phploc-ci目标。

<target name="phploc-ci" depends="prepare" description="Measure project size using PHPLOC and log result in CSV and XML format. Intended for usage within a continuous integration environment.">
    <exec executable="${toolsdir}phploc">
        <arg value="--count-tests"/>
        <arg value="--log-csv"/>
        <arg path="${basedir}/build/logs/phploc.csv"/>
        <arg value="--log-xml"/>
        <arg path="${basedir}/build/logs/phploc.xml"/>
        <arg path="${basedir}/src"/>
        <arg path="${basedir}/tests"/>
    </exec>
</target>

This target is fairly easy to understand. You will notice it depends on the prepare target. Since that was already run by the build target, it won’t run again. Then we get the executable, which is phploc, which will be executed by Ant. You installed this earlier with Composer. Lastly, we will see the PHPLOC specific arguments. Ant will run the following command line input based on this target configuration.

这个目标相当容易理解。 您会注意到这取决于prepare目标。 由于已经由build目标运行,因此不会再运行。 然后,我们获得了可执行文件phploc ,它将由Ant执行。 您之前在Composer中安装了此软件。 最后,我们将看到特定于PHPLOC的参数。 Ant将基于此目标配置运行以下命令行输入。

phploc --count-tests --log-csv /build/logs/phploc.csv --log-xml /build/logs/phploc.xml /src /tests

The ${toolsdir} and ${basedir} are 2 variables. Depending on how they are configured, they might have an effect on the overall command line output

${toolsdir}${basedir}是2个变量。 根据它们的配置方式,它们可能会对整体命令行输出产生影响

As indicated above, if you only wanted to trigger this target, you should run ant phploc-ci on the command line.

如上所述,如果只想触发此目标,则应在命令行上运行ant phploc-ci

Notice that this PHPLOC command will output a phploc.csv file and a phploc.xml file. The plugins installed in Jenkins earlier will read these files and convert them to graphs, for example. In the next article we will dive deeper into this topic.

注意,此PHPLOC命令将输出phploc.csv文件和phploc.xml文件。 例如,早先在Jenkins中安装的插件将读取这些文件并将其转换为图形。 在下一篇文章中,我们将深入探讨该主题。

If you look through the whole build.xml file, you will notice that it’s depending on 3 other separate config files for certain tools; build/phpmd.xml, build/phpdox.xml and build/phpunit.xml. These configuration files are requested by the actual tools. To find out more, look at their respective docs.

如果浏览整个build.xml文件,您会发现它取决于某些工具的其他3个单独的配置文件。 build / phpmd.xmlbuild / phpdox.xmlbuild / phpunit.xml 。 这些配置文件是实际工具所要求的。 要了解更多信息,请查看各自的文档。

Note that phpunit.xml is heavily inspired by the original phpunit.xml.dist file from Symfony2 since we are dealing with a Symfony2 project.

请注意,由于我们正在处理Symfony2项目,因此phpunit.xml受到Symfony2原始phpunit.xml.dist文件的启发。

更改build.xml文件 (Changing the build.xml file)

So, are we done? Unfortunately, not yet. The build.xml file is very generic and not completely suited for our project. First we need to define where tools like PHPUnit can be found. Our composer installed these automatically in the bin directory in the root of our project. We need to change the toolsdir property to the correct value.

那么,我们完成了吗? 不幸的是,还没有。 build.xml文件非常通用,并不完全适合我们的项目。 首先,我们需要定义在哪里可以找到PHPUnit之类的工具。 我们的作曲家会自动将它们安装在项目根目录下的bin目录中。 我们需要将toolsdir属性更改为正确的值。

<property name="toolsdir" value="bin/" />

We can also clean up several commands. The current build.xml file defines that we on one hand have a src directory and on the other hand a tests directory. Since Symfony2 includes tests in the src directory, we can remove all paths to the tests directory like this example.

我们还可以清理几个命令。 当前的build.xml文件定义我们一方面具有src目录,另一方面具有tests目录。 由于Symfony2在src目录中包含测试,因此可以像本例一样删除测试目录的所有路径。

<arg path="${basedir}/tests"/>

Although it’s not required, I prefer to keep everything as clean as possible so it matches our project correctly.

尽管这不是必需的,但我希望尽可能使所有内容保持干净,以使其正确匹配我们的项目。

Lastly, the current configuration for PHPDox as indicated in the build.xml file while writing this article does not work with the latest version in my case. I changed the target like below, so it correctly reads our configuration file.

最后,在撰写本文时, build.xml文件中指示PHPDox的当前配置不适用于最新版本。 我更改了目标,如下所示,因此它可以正确读取我们的配置文件。

<target name="phpdox" depends="phploc-ci,phpcs-ci,phpmd-ci" description="Generate project documentation using phpDox">
  <exec executable="${toolsdir}phpdox">
   <arg value="--file" />
   <arg value="${basedir}/build/phpdox.xml" />
  </exec>
 </target>

建立新目标 (Creating new targets)

Everything has been set up to make sure all our QA tools will run as expected. However, our project is just a bit more complicated. If you analyzed the repository, you will notice that dependencies are regulated by Composer and Bower. Since it’s a Symfony project, we will also need a default parameters.yml file. We need to deal with these requirements to be able to set up the project correctly on Jenkins.

已经进行了所有设置,以确保我们所有的质量检查工具都能按预期运行。 但是,我们的项目稍微复杂一点。 如果分析了存储库,您会注意到依赖关系由Composer和Bower进行管理。 由于这是一个Symfony项目,因此我们还需要一个默认的parameters.yml文件。 我们需要处理这些要求,以便能够在Jenkins上正确设置项目。

Let’s start off with the parameters.yml file. We can work with the default content of parameters.yml.dist, so we are just going to copy that file. We start by creating a new target named copy-parameters which executes the cp command.

让我们从parameters.yml文件开始。 我们可以使用parameters.yml.dist的默认内容,因此我们将复制该文件。 我们首先创建一个名为copy-parameters的新目标,该目标copy-parameters执行cp命令。

<target name="copy-parameters" description="Copy parameters.yml file">
  <exec executable="cp" failonerror="true">
   <arg path="app/config/parameters.yml.dist" />
   <arg path="app/config/parameters.yml" />
  </exec>
 </target>

Next up are Composer and Bower. I decided to install these globally on the Jenkins server by executing the following commands.

接下来是Composer和Bower。 我决定通过执行以下命令将它们全局安装在Jenkins服务器上。

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/bin/composer

sudo apt-get install nodejs
sudo apt-get install npm
sudo npm install -g bower
sudo ln -s /usr/bin/nodejs /usr/bin/node #This is needed on certain linux distro's.

[Ed: Note that if you’re running all this in a VM hosted on Windows, BowerPHP will be a much better option than Bower.]

[Ed:请注意,如果要在Windows托管的VM中运行所有这些功能, 那么BowerPHP将是比Bower更好的选择。]

Now lets create a target for Composer.

现在让我们为Composer创建一个目标。

<target name="composer" description="Installing composer dependencies">
 <exec executable="composer" failonerror="true">
  <arg value="install" />
  <arg value="--dev" />
  <arg value="--prefer-dist" />
  <arg value="--no-progress" />

  <env key="SYMFONY_ENV" value="test"/>
 </exec>
</target>

It’s important to set SYMFONY_ENV to test so symfony knows it has to clear its test cache directory.

设置SYMFONY_ENV进行测试非常重要,因此symfony知道必须清除其测试缓存目录。

Let’s continue with Bower.

让我们继续鲍尔。

<target name="bower" description="Installing bower dependencies">
 <exec executable="bower" failonerror="true">
  <arg value="install" />
 </exec>
</target>

The final thing we need to do, is to make sure these targets are executed. So we change the build target and add the 3 newly created targets in the order we want to execute them.

我们需要做的最后一件事是确保执行这些目标。 因此,我们更改了build目标,并按照要执行的顺序添加了3个新创建的目标。

<target name="build" depends="prepare,copy-parameters,bower,composer,lint,phploc-ci,pdepend,phpmd-ci,phpcs-ci,phpcpd-ci,phpunit,phpdox" description=""/>

吉特 (Git)

The last thing we need to do is to prepare our .gitignore file. If you ever want to run an Ant target on your local machine, your build directory will be filled with log files which you don’t want to commit to your repository. So we add the following lines to the .gitignore file.

我们需要做的最后一件事是准备我们的.gitignore文件。 如果您想在本地计算机上运行Ant目标,则构建目录中将填充您不想提交到存储库的日志文件。 因此,我们.gitignore添加到.gitignore文件。

# build
!build/phpmd.xml
!build/phpunit.xml
!build/phpdox.xml
build/*

Perhaps you got a little confused by all the changes we made. For your convenience, here is a direct link to the commit with all the changes we described above.

也许您对我们所做的所有更改感到有些困惑。 为了您的方便, 是带有上述所有更改的提交的直接链接。

在詹金斯创建项目 (Create the project in Jenkins)

Our project is ready. Let’s head back to Jenkins and configure it. This will be the easiest part in this article.

我们的项目准备好了。 让我们回到Jenkins进行配置。 这将是本文中最简单的部分。

Since our project is located on Github, I suggest you install the Github plugin within Jenkins. If you are using your own git server, you can just install the regular Git plugin. If you are uncertain how to install a plugin, check the previous article.

由于我们的项目位于Github上,因此建议您在Jenkins中安装Github插件。 如果您使用自己的git服务器,则只需安装常规的Git插件即可。 如果不确定如何安装插件,请查看上一篇文章。

When you return to the overview, you have to click new item in the left menu. As item name, we fill in Jumph. Next, we choose the option copy existing item and fill in php-template as the project to copy from.

返回概览时,您必须单击左侧菜单中的new item 。 作为项目名称,我们填写Jumph。 接下来,我们选择copy existing item的选项,并填写php-template作为要从中复制的项目。

Jenkins

We will immediately be taken to the configuration page of this project. First we uncheck Disable build so our build will be enabled on save. Within the Github project input field, we fill in the URL to the Github project.

我们将立即被带到该项目的配置页面。 首先,我们取消选中“ Disable build以便在保存时启用我们的构建。 在Github项目输入字段中,我们填写Github项目的URL。

The last thing we have to do is choose the source management. In our case, this is git. Next, you have to fill in the repository link. If you use the HTTPS link, you don’t have to provide any additional credentials. We can decide which branches should be build, but since we only have a master branch, we will leave it as it is.

我们要做的最后一件事是选择源管理。 在我们的例子中,这是git。 接下来,您必须填写存储库链接。 如果使用HTTPS链接,则无需提供任何其他凭据。 我们可以决定应该建立哪个分支,但是由于我们只有一个主分支,因此将其保持原样。

The configuration is done – it’s so easy because we used a predefined template. If you scroll down, you will exactly see what the template configured for us. Don’t forget to save your configuration before continuing.

完成配置–如此简单,因为我们使用了预定义的模板。 如果向下滚动,您将确切地看到为我们配置的模板。 在继续操作之前,请不要忘记保存您的配置。

开始构建 (Start a build)

If you followed all the steps so far, you should now be able to start a build. You can start a build by clicking build now in the left side menu of a project. You will notice at the bottom left table that a build appears.

如果遵循了到目前为止的所有步骤,则现在应该可以开始构建了。 您可以通过单击项目左侧菜单中的“ build now构建”来开始构建。 您会在左下方表格中看到一个构建版本。

Jenkins

If you click on the build itself, you will get a new side menu with options for this particular build. Click on console output to see exactly what’s going on. In my case, the first build failed due to missing some PHP extensions.

如果单击构建本身,则将获得一个新的侧边菜单,其中包含此特定构建的选项。 单击console output以查看发生了什么。 就我而言,由于缺少一些PHP扩展,首次构建失败。

Jenkins

The console output comes in very handy for finding out why your project is failing. In my case, I just had to run sudo apt-get install php5-curl php5-xsl php5-sqlite php5-xdebug to install some needed packages. It took me a couple of builds to figure out what was missing, but with the console output, it’s easy to figure out what is going wrong. In the end, this is what my build history looks like.

控制台输出非常方便,可以找出项目失败的原因。 就我而言,我只需要运行sudo apt-get install php5-curl php5-xsl php5-sqlite php5-xdebug即可安装一些所需的软件包。 我花了一些时间来找出丢失的内容,但是通过控制台输出,很容易弄清楚出了什么问题。 最后,这就是我的构建历史。

Jenkins

Red means the build has failed, where blue means the build was successful. A failure can mean something went wrong with the configuration as we saw before, but it could also mean that unit tests have failed. If you open up your build.xml file again, you will notice that some targets got an attribute named failonerror which is set to true. If for some reason, that target fails, Ant will automatically stop and will report the build as broken.

红色表示构建失败,蓝色表示构建成功。 失败可能意味着我们之前看到的配置出了点问题,但是也可能意味着单元测试失败了。 如果再次打开build.xml文件,您会注意到某些目标获得了名为failonerror的属性,该属性设置为true。 如果由于某种原因该目标失败,Ant将自动停止并报告构建已损坏。

Perhaps you expected a green ball instead of a blue ball when a build succeeds. There is a plugin available to show green balls instead of blue ones.

也许当构建成功时,您期望的是绿色球而不是蓝色球。 有一个插件可以显示绿色的球,而不是蓝色的球。

结论 (Conclusion)

In this article, we worked our way to a successful build. We had to prepare our project and create a job within Jenkins. Finally we had to debug our first couple of builds since we forgot to install some libraries.

在本文中,我们努力实现了成功的构建。 我们必须准备我们的项目,并在詹金斯内部创造工作。 最后,由于我们忘记安装一些库,因此我们不得不调试我们的前几个版本。

In the next part, we will take a look at all the results Jenkins gives back.

在下一部分中,我们将看一下詹金斯提供的所有结果。

翻译自: https://www.sitepoint.com/preparing-building-php-project-jenkins/

jenkins 构建php

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页