基于 Git Tag 发布及回滚代码

摘要:
发布基于Jenkins+Git+Ansible 1的PHP项目。部署PHP运行环境PHP是一个动态程序,负责解析PHP-FPM服务,它不支持静态网页处理。它通常与Nginx结合来解决这个问题。当客户端请求PHP页面时,Nginx通过fastcgi接口将其转发给本地9000端口的PHP-FPM子进程进行处理,并在处理后返回Nginx。

基于 Jenkins + Git + Ansible 发布 PHP 项目

1、部署 PHP 运行环境

PHP 是一个动态程序,负责解析 PHP-FPM 服务,而这个服务不支持静态网页处理,一般结合 Nginx 解决这个问题。Nginx 本身是一个静态 Web 服务器,并不支持解析 PHP 程序,但它支持了 FastCGI 接口来调用动态服务来解析 PHP 程序。

当客户端请求 PHP 页面时,Nginx 通过 fastcgi 接口转发给本地 9000 端口的 PHP-FPM 子进程处理,处理完成后返回 Nginx。

1)安装 Nginx

配置 Nginx 网络源

[root@tomcat ~]# vim /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

安装并启动

[root@tomcat ~]# yum -y install nginx

[root@tomcat ~]# systemctl start nginx

2)安装 PHP

安装 PHP 依赖的第三方库,命令如下:

[root@tomcat ~]# yum -y install gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel gcc gcc-c++ make openssl-*

编译安装 PHP

[root@tomcat ~]# tar xf php-5.6.39.tar.gz -C /usr/src/

[root@tomcat ~]# cd /usr/src/php-5.6.39/

[root@tomcat php-5.6.39]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli --with-openssl --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-iconv --enable-fpm --enable-zip --enable-mbstring && make && make install

配置 php-fpm,命令如下:

[root@tomcat php-5.6.39]# cp php.ini-production /usr/local/php/etc/php.ini

[root@tomcat php-5.6.39]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@tomcat php-5.6.39]# vim /usr/local/php/etc/php-fpm.conf

149  user = nginx
150  group = nginx

[root@tomcat php-5.6.39]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@tomcat php-5.6.39]# chmod +x /etc/init.d/php-fpm

[root@tomcat php-5.6.39]# service php-fpm start
Starting php-fpm done

3)Nginx 代理 PHP

添加虚拟主机配置如下:

[root@tomcat ~]# vim /etc/nginx/conf.d/default.conf

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm index.php;
    }

    location ~.php${
        root /usr/share/nginx/html;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam
e;
        include fastcgi_params;
    }

[root@tomcat ~]# systemctl restart nginx

2、安装 Ansible 插件

基于 Git Tag 发布及回滚代码第1张

 3、上传 PHP 项目代码到 Git 仓库

1)在 Git 服务器创建 wordpress 版本仓库

[root@git ~]# su - git

[git@git ~]$ mkdir wordpress.git
[git@git ~]$ cd wordpress.git/
[git@git wordpress.git]$ git --bare init
初始化空的 Git 版本库于 /home/git/wordpress.git/

2)下载开源 PHP 博客系统 wordpress

[root@jenkins ~]# wget https://wordpress.org/latest.tar.gz

[root@jenkins ~]# tar xf latest.tar.gz
[root@jenkins ~]# cd wordpress/

3) 提交到 Git 仓库

[root@jenkins wordpress]# git init

[root@jenkins wordpress]# git remote add origin git@192.168.200.127:/home/git/wordpress.git

[root@jenkins wordpress]# git add .

[root@jenkins wordpress]# git commit -m "wp"

[root@jenkins wordpress]# git tag 1.0.0

[root@jenkins wordpress]# git push origin 1.0.0

4、Jenkins 创建项目并发布测试

基于 Git Tag 发布及回滚代码第2张

基于 Git Tag 发布及回滚代码第3张

 基于 Git Tag 发布及回滚代码第4张

 基于 Git Tag 发布及回滚代码第5张

基于 Git Tag 发布及回滚代码第6张

 基于 Git Tag 发布及回滚代码第7张

 基于 Git Tag 发布及回滚代码第8张

 基于 Git Tag 发布及回滚代码第9张

1)模拟实际生产环境提交代码,作用是可以清楚看到两次发版代码的不同

[root@jenkins ~]# cd wordpress/
[root@jenkins wordpress]# echo "Hello Wordpress" > test.html

2)将修改的 test.html 提交到 Git 仓库

[root@jenkins wordpress]# git add .
[root@jenkins wordpress]# git commit -m "hw"
[master f1f2d1c] hw
1 file changed, 1 insertion(+)
create mode 100644 test.html
[root@jenkins wordpress]# git tag 1.0.1
[root@jenkins wordpress]# git push origin 1.0.1
枚举对象: 4, 完成.
对象计数中: 100% (4/4), 完成.
使用 2 个线程进行压缩
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 270 bytes | 270.00 KiB/s, 完成.
总共 3 (差异 1),复用 0 (差异 0)
To 192.168.200.127:/home/git/wordpress.git
* [new tag] 1.0.1 -> 1.0.1

 3)在 Jenkins 执行构建任务

基于 Git Tag 发布及回滚代码第10张

 基于 Git Tag 发布及回滚代码第11张

 然后呢,代码回滚操作,你就按版本来构建就行了。

免责声明:文章转载自《基于 Git Tag 发布及回滚代码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇为啥谷歌浏览器打不开微信公众平台?SVN 向 GIT 进行转换如何拉取指定版本后的提交下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

git的一套全流程上手(不包括报错(我忘了有哪些报错了)以及其解决方案(篇幅问题))

git|Github|Gitee|远程仓库 的基本使用 本篇主要说几个git的场景,单纯是为了让新手能够快速上手(因为我就是新手,我给我自己写了一篇教学用的文章,没毛病) 注: 这里不会去写git能够协同工作的原理(因为我自己还没搞明白,未来我是要去重写git,并改写很多功能的(实际上已经在写了)) 我不会贴效果图,很麻烦,而且我也贴不好。贴好了事半功倍...

PHP实现一个简单url路由功能

  如果一个页面的内容呈现,需要根据url上传递的参数来进行渲染。很多时候可能是这样子写:xxx.com/xx?c=x&m=x& t=..,而我们看到的url往往是这样子的(以新浪微游戏的咖啡恋人为例) game.weibo.com/ilovecoffee….这种URL设计看上去比前一种更好一点:) 如果我们访问一下不存在的游戏应用,例如...

php判断表单是否提交

我们一般通过 submit 提交表单时,会在乎在表单中填写的一大堆数据是否提交到后台。这里就需要做个判断,使用php代码来判断表单数据是否被提交一般采用如下的形式:大理石平台保养 1 2 3 4 5 <?php if(isset($_POST['submit'])) {   echo" 数据被提交过来了"; } ?> 说明:is...

[Linux] nginx记录多种响应时间

官网介绍$request_time – Full request time, starting when NGINX reads the first byte from the client and ending when NGINX sends the last byte of the response body$upstream_connect_tim...

nginx运行vue项目,并对后端做负载均衡

一、打包vue项目 WebStorm打包vue项目过程参考:Vue_打包并发布项目。 二、部署至nginx 1.上传项目 将打包结果(dist文件夹)通过SFTP上传至Linux服务器(我用的是虚拟机),位置随意,我放到了nginx解压文件夹同级目录下。 2.创建配置文件 进入nginx解压文件夹下创建配置文件,custom.conf是我创建的配置文件。...

PHP 出现 502 解决方案

nginx+php 出现502 bad gateway,一般这都不是nginx的问题,而是由于 fastcgi或者php的问题导致的,常见的有以下几种。 1.   php.ini 的memory_limit 过小(如果有个别php程序进程需要占用极大内存时这个必须注意) 2.   php-fpm.conf 中max_children或者max_reques...