詹金斯搭建_安装和固定詹金斯

詹金斯搭建

Earlier this year, I wrote an article about PHP-CI, which you can use as a continuous integration tool for your PHP projects. Within this article I indicated I still liked Jenkins the most as a CI tool. Time to dive into Jenkins and see how we can set this up for our PHP project.

今年早些时候,我写了一篇有关PHP-CI的文章 ,您可以将其用作PHP项目的持续集成工具。 在本文中,我表示我仍然最喜欢Jenkins作为CI工具。 是时候深入Jenkins了,看看我们如何为我们PHP项目进行设置。

詹金斯介绍 (Introduction to Jenkins)

The list of things that Jenkins has to offer is huge due to the rich plugin system it has. Basically, Jenkins is just a tool which connects all kinds of different tools and plugins together to create a report for you. For example, it can run PHPUnit and show you the results in a graph over time. It can check your PHP code for errors by running php -l. However, you can also let Jenkins build a project and output a zip file, which you can use to deploy your application. The advantage of this is that you get a complete zip file back to upgrade your production application without having to run tools like Composer or NPM on your production servers.

由于Jenkins具有丰富的插件系统,因此它必须提供的功能之多。 基本上,Jenkins只是一个工具,它将各种不同的工具和插件连接在一起,可以为您创建报告。 例如,它可以运行PHPUnit并在一段时间内以图形形式显示结果。 它可以通过运行php -l检查您PHP代码是否有错误。 但是,您也可以让Jenkins构建项目并输出一个zip文件,您可以使用该zip文件来部署您的应用程序。 这样做的好处是,您可以获得完整的zip文件来升级生产应用程序,而不必在生产服务器上运行Composer或NPM之类的工具。

Jenkins can be used for many different applications. You can use it for web applications written in PHP, but also for software and mobile applications written in Java or any other language. This makes Jenkins a very versatile tool and very interesting for companies handling many different projects.

Jenkins可用于许多不同的应用程序。 您可以将其用于用PHP编写的Web应用程序,也可以用于用Java或任何其他语言编写的软件和移动应用程序。 这使Jenkins成为一种非常通用的工具,对于处理许多不同项目的公司来说非常有趣。

Because Jenkins has so many options and possibilities, it looks overwhelming for people to start using it. Within this series of articles, we will slowly work our way into Jenkins. We will start with the installation and setup. After that we will continue with analyzing a project. In the end, we will take a close look at numerous other plugins we can use to check the quality of our product.

由于詹金斯(Jenkins)有很多选择和可能性,因此人们开始使用它似乎不知所措。 在本系列文章中,我们将逐步研究詹金斯。 我们将从安装和设置开始。 之后,我们将继续分析项目。 最后,我们将仔细研究可用于检查产品质量的众多其他插件。

We will mainly focus on the quality of the PHP code, but in the end we will also take a short look at analyzing our HTML, CSS and JavaScript which work in close harmony with PHP.

我们将主要关注PHP代码的质量,但最后,我们还将简要分析与PHP紧密协作HTML,CSS和JavaScript。

安装詹金斯 (Installing Jenkins)

You can install Jenkins on any popular operating system. For this article, we are going to install Jenkins on Ubuntu Server 14.04 LTS. If you are using a different operating system, you can check here for the installation instructions per operating system. To install Jenkins on Ubuntu the easiest way would be by running this command.

您可以在任何流行的操作系统上安装Jenkins。 对于本文,我们将在Ubuntu Server 14.04 LTS上安装Jenkins。 如果您使用的是其他操作系统,则可以在此处查看每个操作系统的安装说明。 要在Ubuntu上安装Jenkins,最简单的方法是运行此命令。

sudo apt-get install jenkins

However, you are now installing Jenkins which is located in Ubuntu’s repository. Jenkins itself suggests to install directly from the Jenkins repositories. First, we add the key to our system for the repository.

但是,您现在正在安装位于Ubuntu存储库中的Jenkins。 Jenkins本身建议直接从Jenkins信息库安装。 首先,我们将密钥添加到系统的存储库中。

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

Now we run the following command to add Jenkins to the sources.list file.

现在,我们运行以下命令以将Jenkins添加到sources.list文件。

deb http://pkg.jenkins-ci.org/debian binary/

Now we are ready to install Jenkins.

现在我们准备安装Jenkins。

sudo apt-get update
sudo apt-get install jenkins

When finished, Jenkins is successfully installed on your server.

完成后,Jenkins已成功安装在您的服务器上。

You can now reach Jenkins through your browser by adding port 8080 to the end of the URL. So imagine my server is named ci.myserver.com you can reach Jenkins on ci.myserver.com:8080.

现在,您可以通过在URL末尾添加端口8080来通过浏览器访问Jenkins。 因此,假设我的服务器名为ci.myserver.com您可以通过ci.myserver.com:8080 Jenkins ci.myserver.com:8080

Jenkins

.

保护詹金斯 (Securing Jenkins)

The first thing you should notice is that Jenkins is completely open. Anyone now going to this URL can do all kinds of different tasks, so the first thing we are going to do is secure it. We start by clicking on Manage Jenkins in the sidebar menu. where we will be greeted by a notification from Jenkins, recommending to secure the CI server.

您应该注意的第一件事是詹金斯完全开放。 现在使用此URL的任何人都可以执行各种不同的任务,因此我们要做的第一件事就是保护它的安全。 我们首先在侧边栏菜单中单击Manage Jenkins 。 詹金斯(Jenkins)发出通知,向我们表示欢迎,建议您保护CI服务器。

Jenkins

Click the Setup Security button to continue. On the next page, mark enable security checkbox as checked. The following steps are crucial to follow correctly, because it’s easy to lock yourself out of Jenkins. If this happens, you can follow these steps to gain access again.

单击Setup Security按钮继续。 在下一页上,将“ enable security复选框标记为已选中。 以下步骤对于正确执行至关重要,因为很容易将自己与詹金斯隔离。 如果发生这种情况,您可以按照以下步骤再次获得访问权限。

The first thing you need to do, is set your security realm. The easiest one is to have Jenkins have his own user database by marking the checkbox in front of Jenkins' own user database. Make sure the allow users to sign up option is also enabled. Now save your configuration by clicking save.

您需要做的第一件事就是设置安全领域。 最简单的方法是,通过标记Jenkins' own user database前面的复选框,使Jenkins' own user database 。 确保allow users to sign up选项也已启用。 现在,单击save保存您的配置。

You should now be able to see a sign up link at the top right corner of the page. Click this link and fill in the form to sign up. When done, log in with your newly created account.

现在,您应该可以在页面右上角看到一个注册链接。 单击此链接并填写表格进行注册。 完成后,使用您新创建的帐户登录。

Go back to the security settings by clicking Manage Jenkins and then clicking Configure Global Security. You should now uncheck the allow users to sign up checkbox to make sure no new accounts can be created without your permission.

通过单击“ Manage Jenkins ,然后单击“ Configure Global Security性”,返回到安全性设置。 现在,您应该取消选中“ allow users to sign up复选框,以确保未经您的许可不能创建任何新帐户。

Next we are going to set the authorization. The best options for you are either matrix based security or Project-based Matrix Authorization Strategy. This allows you to set per user which actions they can and cannot perform. If you choose the latter option, you will be able to even set this per project. For now, I am going to pick the matrix based security.

接下来,我们将设置授权。 最好的选择是matrix based securityProject-based Matrix Authorization Strategy 。 这使您可以为每个用户设置他们可以执行和不能执行的操作。 如果选择后一个选项,则甚至可以针对每个项目进行设置。 现在,我将选择matrix based security

A table with permissions is now displayed. You will only see one table row in which you can define what an anonymous user can do. However, we want to set permissions for our own user account. So fill in your username in the box below and click Add. To make sure you have access to everything, you can check them all.

现在显示具有权限的表。 您只会看到一个表格行,您可以在其中定义匿名用户可以执行的操作。 但是,我们要为自己的用户帐户设置权限。 因此,在下面的框中填写您的用户名,然后点击Add 。 为了确保您可以访问所有内容,可以检查所有内容。

In the end, this is what it looks like.

最后,这是它的外观。

Jenkins

Note: Be careful with capitalized letters. Peter and peter are 2 different user accounts, so make sure you fill in the correct username in both cases else you will be locked out of the system.

注意:请注意大写字母。 彼得和彼得是2个不同的用户帐户,因此在两种情况下请确保输入正确的用户名,否则您将被锁定在系统之外。

When saving, you will automatically get logged out. Note that you don’t see anything anymore except a login form. After logging in, you should have access to everything like before. If you get a permission error, you made a mistake and you either have to change the settings or you got yourself locked out of Jenkins. In the latter case, see the link above.

保存时,您将自动注销。 请注意,除了登录表单之外,您什么都看不到。 登录后,您应该可以像以前一样访问所有内容。 如果遇到权限错误,则说明您犯了一个错误,您必须更改设置,或者将自己锁定在詹金斯之外。 在后一种情况下,请参见上面的链接。

准备詹金斯 (Preparing Jenkins)

So far, we have been busy installing and securing Jenkins. Now it’s time to configure Jenkins so we can start building and analyzing our PHP projects. Sebastian Bergmann created an excellent website showing you how to set up Jenkins for PHP. We are going to do the same steps as documented, but in a slightly different order.

到目前为止,我们一直在忙于安装和保护Jenkins。 现在是时候配置Jenkins,这样我们就可以开始构建和分析我们PHP项目。 Sebastian Bergmann创建了一个出色的网站,向您展示了如何为PHP设置Jenkins。 我们将执行与记录相同的步骤,但顺序略有不同。

安装插件 (Installing plugins)

First we need to install several plugins into Jenkins. Plugins are small extensions to Jenkins which can perform tasks for you like analyzing a report and converting it to a graph. We will need the following list of plugins to get started.

首先,我们需要在Jenkins中安装几个插件。 插件是Jenkins的小扩展,可以为您执行任务,例如分析报告并将其转换为图形。 我们将需要以下插件列表才能上手。

  • checkstyle

    格斗风格
  • cloverphp

    cloverphp
  • crap4j

    废话
  • dry

    干燥
  • htmlpublisher

    html发布者
  • jdepend

    依赖
  • plot

    情节
  • pmd

    PMD
  • violations

    违规
  • xunit

    单位

We are going to install these plugins through the interface. If you feel more experienced or comfortable with the command line, you can check out this documentation to see how to install them via the command line. Also, that page will give you a short explanation about what every plugin will do.

我们将通过界面安装这些插件。 如果您对命令行更加有经验或比较满意,可以查看此文档以了解如何通过命令行进行安装。 另外,该页面还会为您简要介绍每个插件的功能。

Within Jenkins, go to Manage Jenkins and then to Manage Plugins. Click the tab named available. Here you will see a complete list of all available plugins. With the filter at the top right corner, you can quickly search for every plugin. Make sure you install the complete list of the plugins mentioned above by checking the checkboxes.

在Jenkins中,转到Manage Jenkins ,然后到Manage Plugins 。 单击名为available的选项卡。 在这里,您将看到所有可用插件的完整列表。 使用右上角的过滤器,您可以快速搜索每个插件。 通过选中复选框,确保安装上述插件的完整列表。

Jenkins

Check the checkbox in front of Restart Jenkins when installation is complete and no jobs are running to restart Jenkins when everything is done. After the restart, all the required plugins have been installed correctly.

Restart Jenkins when installation is complete and no jobs are running选中“ Restart Jenkins when installation is complete and no jobs are running前面的复选框, Restart Jenkins when installation is complete and no jobs are running以重新启动Jenkins。 重新启动后,所有必需的插件均已正确安装。

创建模板 (Creating a template)

When Jenkins is set up, we normally start by creating a project, sometimes called a job. A project is typically one PHP project you got. Within a project, you will have several builds. You can configure Jenkins to analyze (build) your code at certain moments. This can be scheduled intervals or on pull requests from contributors, for example. So a project contains several builds over time, each being a separate analysis of your code. Thanks to the plugins we installed, you will be able to see the results of those builds in nice graphs. Within these graphs you can quickly spot if a certain build increased or decreased the quality of your project. We can also see where we need to improve our code according to all of our tools.

设置詹金斯(Jenkins)后,我们通常从创建一个项目(有时称为工作)开始。 一个项目通常是您获得的一个PHP项目。 在一个项目中,您将拥有多个版本。 您可以配置Jenkins在特定时刻分析(构建)代码。 例如,这可以是预定的时间间隔,也可以来自贡献者的请求请求。 因此,随着时间的推移,一个项目包含多个构建,每个构建都是对代码的单独分析。 多亏了我们安装的插件,您将能够在漂亮的图形中查看这些构建的结果。 在这些图中,您可以快速发现某个特定版本是增加还是降低了项目的质量。 我们还可以根据所有工具查看需要改进代码的地方。

We could now create a new project. However, we also would need to configure this project. For example, we could configure that each time PHPUnit is run, a code coverage page should be created so we can see which code is tested and which code is untested. There’s a lot of tests we would need to configure on a new project, and this would likely be time consuming. Luckily, Sebastian Bergmann also created a template which we’re going to use.

现在,我们可以创建一个新项目。 但是,我们还需要配置该项目。 例如,我们可以配置为每次运行PHPUnit时,都应该创建一个代码覆盖率页面,以便我们可以查看测试了哪些代码和未经测试的代码。 我们需要在一个新项目上配置许多测试,这可能会很耗时。 幸运的是,塞巴斯蒂安·伯格曼(Sebastian Bergmann)还创建了一个模板 ,供我们使用。

We need to open up the command line to be able to install this template. The easiest way would be to download the CLI tool Jenkins provided. The problem however is the fact that we secured our Jenkins installation earlier. This would mean we have to open up security back to anonymous or connect public/private key to our account.

我们需要打开命令行才能安装此模板。 最简单的方法是下载Jenkins提供的CLI工具。 但是,问题在于我们较早地保护了我们的Jenkins安装。 这意味着我们必须将安全性放回匿名状态,或者将公钥/私钥连接到我们的帐户。

In this case, we are going for an alternative method of installing the template. First, log in as user Jenkins on the command line and go to the home directory.

在这种情况下,我们将寻找一种替代方法来安装模板。 首先,以用户Jenkins的身份在命令行上登录并转到主目录。

sudo su jenkins
cd ~

Within the home directory, go into the jobs directory and create a new directory named php-template.

在主目录中,进入jobs目录并创建一个名为php-template的新目录。

cd jobs
mkdir php-template

Now we are going to enter this directory and add the configuration file.

现在,我们将进入此目录并添加配置文件。

cd php-template
wget https://raw.github.com/sebastianbergmann/php-jenkins-template/master/config.xml

Now, we need Jenkins to reload its configuration. You can do this by going back to the web interface, click Manage Jenkins and click on Reload Configuration from Disk. Jenkins will now reload its configuration. When done, you should see the php-template project on the main overview page.

现在,我们需要Jenkins重新加载其配置。 您可以通过返回Web界面,单击Manage Jenkins并单击Reload Configuration from Disk 。 Jenkins现在将重新加载其配置。 完成后,您应该在主要概述页面上看到php-template项目。

Jenkins

Jenkins is now ready. We can create new projects based on this template.

詹金斯现在准备好了。 我们可以基于此模板创建新项目。

结论 (Conclusion)

Within this article we installed, secured and prepared Jenkins to start analyzing our PHP project. In the next part, we will add our project, prepare it, and of course analyze it.

在本文中,我们安装,保护并准备了Jenkins,以开始分析我们PHP项目。 在下一部分中,我们将添加项目,进行准备并进行分析。

翻译自: https://www.sitepoint.com/installing-securing-jenkins/

詹金斯搭建

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值