Docker + Jenkins 快速打造 PHP 持续集成服务器

纸上得来终觉浅 绝知此事要躬行 -- 陆游

假如你想快速搭建一个 PHP CI/CD 服务器,那么 Docker 可以作为一个很好的选择。本文从实践的角度给出一个快速搭建 Docker + Jenkins 的 PHP 持续集成服务器的方案。如果你想了解更多的理论知识,本文的参考资料章节已献出大量干货。

第一步:安装 Docker

不是本文的重点,略 (如有疑问,请自行百度或者在评论里留言)

第二步:安装 Docker 的 Jenkins 镜像 (已配置好所需要的各种 Jenkins 插件)

docker pull voduytuan/jenkins-php-docker

经过漫长的等待后,镜像就下载好了,用 docker images 命令可以查看本地已经有了 voduytuan/jenkins-php-docker 镜像:

Docker Image

第三步:克隆 php template 文件夹

克隆 php template 文件夹到 /var/docker/jenkins-php-docker 目录,并给该目录赋予 rwx 权限。

cd  /var/docker/jenkins-php-docker
git clone https://github.com/naxhh/jenkins-php-docker
chmod -R 777 /var/docker/jenkins-php-docker

第四步:运行 Docker 容器

docker run -d -P -v /var/docker/jenkins-php-docker:/var/jenkins_home:rw voduytuan/jenkins-php-docker

执行 docker ps 命令,即可看到 Jenkins 容器已经在运行:

file

按照上面给出的端口在浏览器中就可以访问配置好的 Jenkins 服务器了,而且已经有了一个模版项目 php-template

file

第五步:测试一下

可以快速添加 Money 这个项目测试一下你的服务器是否能正常工作。

Copy existing Item 那里选择已有的模版项目 php-template 可以省去很多配置。

file

这里填写要测试的项目的 git 仓库地址,然后保存

file

构建几次项目后,就可以看到各式各样的测试图表:

file

file

file

参考资料

在线服务器示例

  1. Jenkins 服务器

PHP 项目的 Jenkins 任务模版参考

  1. Template for Jenkins Jobs for PHP Projects

持续集成系列文章

  1. Continuous Delivery: Introduction to concepts and tools
  2. Continuous Delivery: CI Tools Setup
  3. Continuous Delivery: Static Analysis
  4. Continuous Delivery: Unit Tests
  5. Continuous Delivery: Code Coverage
  6. Continuous Deployment: Introduction
  7. Continuous Deployment: Strategies
  8. Continuous Deployment: Implementation
  9. Continuous Deployment: Implementation with Ansible and Docker
  10. Continuous Integration, Delivery or Deployment with Jenkins, Docker and Ansible

Docker 镜像

  1. Jenkins-PHP Docker
  2. jenkins-ci-php

PDF

  1. Integrating PHP Projects with Jenkins
  2. Jenkins: The Definitive Guide
You can’t use an old map to explore a new world.
本帖已被设为精华帖!
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 11
[JDepend] Couldn't generate JDepend file at 'build/logs/jdepend.xml'java.io.FileNotFoundException: /var/jenkins_home/jobs/php-template/workspace/build/logs/jdepend.xml (No such file or directory)
Finished: FAILURE

我在执行php-template的时候会报这个错,这个是什么原因,求大神解答

7年前 评论

@yhl54199 php-template 只是php项目的一个jenkins配置模版,并没有实际的代码,所以不能用来执行jenkins的构建过程,测试的话可以用第五步里说到的那个 Money 项目来执行构建过程。

7年前 评论

@Lee2011 我用money构建时最后会失败提示
publish xunit test result report changed build result to failure
你有遇到吗

7年前 评论

@yhl54199 没有遇到过你说的这种情况,可以看下构建的日志,找一找原因,看上去像是单元测试那里有点问题

7年前 评论

楼主用travis吗?

6年前 评论

@Lee2011 按照楼主的教程部署 报错了

![Uploading file...]()

6年前 评论

file
按照楼主的教程部署 报错了
@Lee2011

6年前 评论

@wujunze travis 没怎么用过。。。写这个文章的时候还是 Jenkins 1 的时候,现在已经 Jenkins 2 了,不知道是不是因为这个原因

6年前 评论
wonbin

你好 看了一下docker file 发现环境用的都是 php5.4 ,现在是PHP7 的项目,请问可以使用吗

4年前 评论

@wonbin 应该是不可以的。不过现在可以参考 laradock 来自己配置和构建想要的镜像

4年前 评论
wonbin 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!