如何在 linux 上配置持续集成服务 - Drone

如果你对一次又一次的克隆、构建、测试和部署代码感到厌倦了,可以考虑一下持续集成。持续集成简称 CI,是一种像我们一样的频繁提交的代码库,构建、测试和部署的软件工程实践。CI 可以帮助我们快速的集成新代码到已有的代码库。如果这个过程是自动化进行的,那么就会提高开发的速度,因为这可以减少开发人员手工构建和测试的时间。Drone 是一个自由开源项目,用来提供一个非常棒的持续集成服务的环境,采用 Apache 2.0 协议发布。它已经集成近很多代码库提供商,比如 Github、Bitbucket 以及 Google Code,它可以从代码库提取代码,使我们可以对包括 PHP, Node, Ruby, Go, Dart, Python, C/C++, JAVA 等等在内的各种语言编译构建。它是如此一个强大的平台,它使用了容器和 docker 技术,这让用户每次构建都可以在保证隔离的条件下完全控制他们自己的构建环境。

如何在 linux 上配置持续集成服务 - Drone

 

1. 安装 Docker

首先,我们要安装 docker,因为这是 Drone 的工作流的最关键的元素。Drone 合理的利用了 docker 来构建和测试应用。容器技术提高了应用部署的效率。要安装 docker ,我们需要在不同的 linux 发行版本运行下面对应的命令,我们这里会说明 Ubuntu 14.04 和 CentOS 7 两个版本。

 

Ubuntu

要在 Ubuntu 上安装 Docker ,我们只需要运行下面的命令。

  1. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> update</span>
  2. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> install docker</span><span class="pun">.</span><span class="pln">io</span>

安装之后我们需要使用service 命令重启 docker 引擎。

  1. <span class="com">#</span><span class="pln"> service docker restart</span>

然后我们让 docker 在系统启动时自动启动。

  1. <span class="com">#</span><span class="pln"> update</span><span class="pun">-</span><span class="pln">rc</span><span class="pun">.</span><span class="pln">d docker defaults</span>
  2. <span class="typ">Adding</span><span class="pln"> system startup </span><span class="kwd">for</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker </span><span class="pun">...</span>
  3. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc0</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">K20docker </span><span class="pun">-></span><span class="pun">../</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span>
  4. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc1</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">K20docker </span><span class="pun">-></span><span class="pun">../</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span>
  5. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc6</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">K20docker </span><span class="pun">-></span><span class="pun">../</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span>
  6. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc2</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">S20docker </span><span class="pun">-></span><span class="pun">../</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span>
  7. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc3</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">S20docker </span><span class="pun">-></span><span class="pun">../</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span>
  8. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc4</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">S20docker </span><span class="pun">-></span><span class="pun">../</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span>
  9. <span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc5</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">S20docker </span><span class="pun">-></span><span class="pun">../</span><span class="kwd">init</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">docker</span>

 

CentOS

第一,我们要更新机器上已经安装的软件包。我们可以使用下面的命令。

  1. <span class="com">#</span><span class="kwd">sudo</span><span class="kwd">yum</span><span class="pln"> update</span>

要在 centos 上安装 docker,我们可以简单的运行下面的命令。

  1. <span class="com">#</span><span class="pln"> curl </span><span class="pun">-</span><span class="pln">sSL https</span><span class="pun">:</span><span class="com">//get.docker.com/ | sh</span>

安装好 docker 引擎之后我么只需要简单使用下面的systemd 命令启动 docker,因为 centos 7 的默认初始化系统是 systemd。

  1. <span class="com">#</span><span class="kwd">systemctl</span><span class="pln"> start docker</span>

然后我们要让 docker 在系统启动时自动启动。

  1. <span class="com">#</span><span class="kwd">systemctl</span><span class="pln"> enable docker</span>
  2. <span class="kwd">ln</span><span class="pun">-</span><span class="pln">s </span><span class="str">'/usr/lib/systemd/system/docker.service'</span><span class="str">'/etc/systemd/system/multi-user.target.wants/docker.service'</span>

 

2. 安装 SQlite 驱动

Drone 默认使用 SQlite3 数据库服务器来保存数据和信息。它会在 /var/lib/drone/ 自动创建名为 drone.sqlite 的数据库来处理数据库模式的创建和迁移。要安装 SQlite3 我们要完成以下几步。

 

Ubuntu 14.04

因为 SQlite3 存在于 Ubuntu 14.04 的默认软件库,我们只需要简单的使用 apt 命令安装它。

  1. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> install libsqlite3</span><span class="pun">-</span><span class="pln">dev</span>

 

CentOS 7

要在 Centos 7 上安装需要使用下面的 yum 命令。

  1. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> install sqlite</span><span class="pun">-</span><span class="pln">devel</span>

 

3. 安装 Drone

最后,我们安装好依赖的软件,我们现在更进一步的接近安装 Drone。在这一步里我们只简单的从官方链接下载对应的二进制软件包,然后使用默认软件包管理器安装 Drone。

 

Ubuntu

我们将使用 wget 从官方的 Debian 文件下载链接下载 drone 的 debian 软件包。下面就是下载命令。

  1. <span class="com">#</span><span class="kwd">wget</span><span class="pln"> downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io</span><span class="pun">/</span><span class="pln">master</span><span class="pun">/</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">deb</span>
  2. <span class="typ">Resolving</span><span class="pln"> downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io </span><span class="pun">(</span><span class="pln">downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io</span><span class="pun">)...</span><span class="lit">54.231</span><span class="pun">.</span><span class="lit">48.98</span>
  3. <span class="typ">Connecting</span><span class="pln"> to downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io </span><span class="pun">(</span><span class="pln">downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io</span><span class="pun">)|</span><span class="lit">54.231</span><span class="pun">.</span><span class="lit">48.98</span><span class="pun">|:</span><span class="lit">80.</span><span class="pun">..</span><span class="pln"> connected</span><span class="pun">.</span>
  4. <span class="pln">HTTP request sent</span><span class="pun">,</span><span class="pln"> awaiting response</span><span class="pun">...</span><span class="lit">200</span><span class="pln"> OK</span>
  5. <span class="typ">Length</span><span class="pun">:</span><span class="lit">7722384</span><span class="pun">(</span><span class="lit">7.4M</span><span class="pun">)</span><span class="pun">[</span><span class="pln">application</span><span class="pun">/</span><span class="pln">x</span><span class="pun">-</span><span class="pln">debian</span><span class="pun">-</span><span class="kwd">package</span><span class="pun">]</span>
  6. <span class="typ">Saving</span><span class="pln"> to</span><span class="pun">:</span><span class="str">'drone.deb'</span>
  7. <span class="lit">100</span><span class="pun">%[======================================>]</span><span class="lit">7</span><span class="pun">,</span><span class="lit">722</span><span class="pun">,</span><span class="lit">384</span><span class="lit">1.38MB</span><span class="pun">/</span><span class="pln">s </span><span class="kwd">in</span><span class="lit">17s</span>
  8. <span class="lit">2015</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">06</span><span class="lit">14</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">28</span><span class="pun">(</span><span class="lit">456</span><span class="pln"> KB</span><span class="pun">/</span><span class="pln">s</span><span class="pun">)</span><span class="pun">-</span><span class="str">'drone.deb'</span><span class="pln"> saved </span><span class="pun">[</span><span class="lit">7722384</span><span class="pun">/</span><span class="lit">7722384</span><span class="pun">]</span>

下载好之后,我们将使用 dpkg 软件包管理器安装它。

  1. <span class="com">#</span><span class="pln"> dpkg </span><span class="pun">-</span><span class="pln">i drone</span><span class="pun">.</span><span class="pln">deb</span>
  2. <span class="typ">Selecting</span><span class="pln"> previously unselected </span><span class="kwd">package</span><span class="pln"> drone</span><span class="pun">.</span>
  3. <span class="pun">(</span><span class="typ">Reading</span><span class="pln"> database </span><span class="pun">...</span><span class="lit">28077</span><span class="pln"> files </span><span class="kwd">and</span><span class="pln"> directories currently installed</span><span class="pun">.)</span>
  4. <span class="typ">Preparing</span><span class="pln"> to unpack drone</span><span class="pun">.</span><span class="pln">deb </span><span class="pun">...</span>
  5. <span class="typ">Unpacking</span><span class="pln"> drone </span><span class="pun">(</span><span class="lit">0.3</span><span class="pun">.</span><span class="lit">0</span><span class="pun">-</span><span class="pln">alpha</span><span class="pun">-</span><span class="lit">1442513246</span><span class="pun">)</span><span class="pun">...</span>
  6. <span class="typ">Setting</span><span class="pln"> up drone </span><span class="pun">(</span><span class="lit">0.3</span><span class="pun">.</span><span class="lit">0</span><span class="pun">-</span><span class="pln">alpha</span><span class="pun">-</span><span class="lit">1442513246</span><span class="pun">)</span><span class="pun">...</span>
  7. <span class="typ">Your</span><span class="pln"> system ubuntu </span><span class="lit">14</span><span class="pun">:</span><span class="kwd">using</span><span class="pln"> upstart to control </span><span class="typ">Drone</span>
  8. <span class="pln">drone start</span><span class="pun">/</span><span class="pln">running</span><span class="pun">,</span><span class="pln"> process </span><span class="lit">9512</span>

 

CentOS

在 CentOS 机器上我们要使用 wget 命令从下载链接下载 RPM 包。

  1. <span class="com">#</span><span class="kwd">wget</span><span class="pln"> downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io</span><span class="pun">/</span><span class="pln">master</span><span class="pun">/</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">rpm</span>
  2. <span class="pun">--</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">06</span><span class="lit">11</span><span class="pun">:</span><span class="lit">06</span><span class="pun">:</span><span class="lit">45</span><span class="pun">--</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//downloads.drone.io/master/drone.rpm</span>
  3. <span class="typ">Resolving</span><span class="pln"> downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io </span><span class="pun">(</span><span class="pln">downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io</span><span class="pun">)...</span><span class="lit">54.231</span><span class="pun">.</span><span class="lit">114.18</span>
  4. <span class="typ">Connecting</span><span class="pln"> to downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io </span><span class="pun">(</span><span class="pln">downloads</span><span class="pun">.</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">io</span><span class="pun">)|</span><span class="lit">54.231</span><span class="pun">.</span><span class="lit">114.18</span><span class="pun">|:</span><span class="lit">80.</span><span class="pun">..</span><span class="pln"> connected</span><span class="pun">.</span>
  5. <span class="pln">HTTP request sent</span><span class="pun">,</span><span class="pln"> awaiting response</span><span class="pun">...</span><span class="lit">200</span><span class="pln"> OK</span>
  6. <span class="typ">Length</span><span class="pun">:</span><span class="lit">7763311</span><span class="pun">(</span><span class="lit">7.4M</span><span class="pun">)</span><span class="pun">[</span><span class="pln">application</span><span class="pun">/</span><span class="pln">x</span><span class="pun">-</span><span class="pln">RedHat</span><span class="pun">-</span><span class="kwd">package</span><span class="pun">-</span><span class="pln">manager</span><span class="pun">]</span>
  7. <span class="typ">Saving</span><span class="pln"> to</span><span class="pun">:</span><span class="pun">‘</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">rpm</span><span class="pun">’</span>
  8. <span class="lit">100</span><span class="pun">%[======================================>]</span><span class="lit">7</span><span class="pun">,</span><span class="lit">763</span><span class="pun">,</span><span class="lit">311</span><span class="lit">1.18MB</span><span class="pun">/</span><span class="pln">s </span><span class="kwd">in</span><span class="lit">20s</span>
  9. <span class="lit">2015</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">06</span><span class="lit">11</span><span class="pun">:</span><span class="lit">07</span><span class="pun">:</span><span class="lit">06</span><span class="pun">(</span><span class="lit">374</span><span class="pln"> KB</span><span class="pun">/</span><span class="pln">s</span><span class="pun">)</span><span class="pun">-</span><span class="pun">‘</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">rpm</span><span class="pun">’</span><span class="pln"> saved </span><span class="pun">[</span><span class="lit">7763311</span><span class="pun">/</span><span class="lit">7763311</span><span class="pun">]</span>

然后我们使用 yum 安装 rpm 包。

  1. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> localinstall drone</span><span class="pun">.</span><span class="pln">rpm</span>

 

4. 配置端口

安装完成之后,我们要先进行配置才能工作起来。drone 的配置文件在/etc/drone/drone.toml 。默认情况下 drone 的 web 接口使用的是80,而这也是 http 默认的端口,如果我们修改它,请按下面所示的修改配置文件里 server 块对应的值。

  1. <span class="pun">[</span><span class="pln">server</span><span class="pun">]</span>
  2. <span class="pln">port</span><span class="pun">=</span><span class="str">":80"</span>

 

5. 集成 Github

为了运行 Drone 我们必须设置最少一个和 GitHub、GitHub 企业版,Gitlab,Gogs,Bitbucket 关联的集成点。在本文里我们只集成了 github,但是如果我们要集成其他的服务,我们可以在配置文件做修改。为了集成 github 我们需要在github 的设置里创建一个新的应用:https://github.com/settings/developers

如何在 linux 上配置持续集成服务 - Drone

Registering App Github

要创建一个应用,我们需要在 New Application 页面点击 Register,然后如下所示填表。

如何在 linux 上配置持续集成服务 - Drone

Registering OAuth app github

我们应该保证在应用的配置项里设置了授权回调链接,链接看起来类似 http://drone.linoxide.com/api/auth/github.com。然后我们点击注册应用。所有都做好之后我们会看到我们需要在我们的 Drone 配置文件里配置的客户端 ID 和客户端密钥。

如何在 linux 上配置持续集成服务 - Drone

Client ID and Secret Token

在这些都完成之后我们需要使用文本编辑器编辑 drone 配置文件,比如使用下面的命令。

  1. <span class="com">#</span><span class="kwd">nano</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">drone</span><span class="pun">/</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">toml</span>

然后我们会在 drone 的配置文件里面找到[github] 部分,紧接着的是下面所示的配置内容

  1. <span class="pun">[</span><span class="pln">github</span><span class="pun">]</span>
  2. <span class="pln">client</span><span class="pun">=</span><span class="str">"3dd44b969709c518603c"</span>
  3. <span class="pln">secret</span><span class="pun">=</span><span class="str">"4ee261abdb431bdc5e96b19cc3c498403853632a"</span>
  4. <span class="com">#</span><span class="pln"> orgs</span><span class="pun">=[]</span>
  5. <span class="com">#</span><span class="pln"> open</span><span class="pun">=</span><span class="kwd">false</span>

如何在 linux 上配置持续集成服务 - Drone

Configuring Github Drone

 

6. 配置 SMTP 服务器

如果我们想让 drone 使用 email 发送通知,那么我们需要在 SMTP 配置里面设置我们的 SMTP 服务器。如果我们已经有了一个 SMTP 服务,那就只需要简单的使用它的配置文件就行了,但是因为我们没有一个 SMTP 服务器,我们需要安装一个 MTA 比如 Postfix,然后在 drone 配置文件里配置好 SMTP。

 

Ubuntu

在 ubuntu 里使用下面的 apt 命令安装 postfix。

  1. <span class="com">#</span><span class="kwd">apt-get</span><span class="pln"> install postfix</span>

 

CentOS

在 CentOS 里使用下面的 yum 命令安装 postfix。

  1. <span class="com">#</span><span class="kwd">yum</span><span class="pln"> install postfix</span>

安装好之后,我们需要编辑我们的 postfix 配置文件。

  1. <span class="com">#</span><span class="kwd">nano</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">postfix</span><span class="pun">/</span><span class="pln">main</span><span class="pun">.</span><span class="pln">cf</span>

然后我们要把 myhostname 的值替换为我们自己的 FQDN,比如 drone.linoxide.com。

  1. <span class="pln">myhostname </span><span class="pun">=</span><span class="pln"> drone</span><span class="pun">.</span><span class="pln">linoxide</span><span class="pun">.</span><span class="pln">com</span>

现在开始配置 drone 配置文件里的 SMTP 部分。

  1. <span class="com">#</span><span class="kwd">nano</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">drone</span><span class="pun">/</span><span class="pln">drone</span><span class="pun">.</span><span class="pln">toml</span>

找到[smtp] 部分补充上下面的内容。

  1. <span class="pun">[</span><span class="pln">smtp</span><span class="pun">]</span>
  2. <span class="pln">host </span><span class="pun">=</span><span class="str">"drone.linoxide.com"</span>
  3. <span class="pln">port </span><span class="pun">=</span><span class="str">"587"</span>
  4. <span class="kwd">from</span><span class="pun">=</span><span class="str">"[email protected]"</span>
  5. <span class="pln">user </span><span class="pun">=</span><span class="str">"root"</span>
  6. <span class="kwd">pass</span><span class="pun">=</span><span class="str">"password"</span>

如何在 linux 上配置持续集成服务 - Drone

Configuring SMTP Drone

注意:这里的 userpass 参数强烈推荐一定要改成某个具体用户的配置。

相关推荐