1. 环境搭建
1.1 环境
10.0.0.237 gitlab
10.0.0.227 jenkins
10.0.0.217 nginx-lb
10.0.0.82 developer-2/websrv-2
10.0.0.81 developer-1/websrv-1
1.2 developer-2上完成wordpress项目创建
[18:25:47 root@developer-2 ~]#tar xf wordpress-5.7.2.tar.gz
[18:28:17 root@developer-2 ~]#cd wordpress/
[18:28:25 root@developer-2 ~/wordpress]#git remote -v
fatal: not a git repository (or any of the parent directories): .git
[18:28:27 root@developer-2 ~/wordpress]#git init
Initialized empty Git repository in /root/wordpress/.git/
1.3 关联仓库, 首次提交代码
[18:28:29 root@developer-2 ~/wordpress]#git remote add origin http://gitlab.abc.com/devops/wordpress.git
[18:28:57 root@developer-2 ~/wordpress]#git add .
[18:29:02 root@developer-2 ~/wordpress]#git commit -m '首次提交代码'
[18:29:25 root@developer-2 ~/wordpress]#git push origin master
1.4 Jenkins创建freestyle项目, 然后配置gitlab仓库项目对应地址
1.5 测试构建
2. 手动实现集群架构代码上线
2.1 nginx负载均衡配置
[13:07:11 root@lb /etc/nginx/conf.d]#vim wordpress.conf
upstream wordpress {
server 10.0.0.81:9000;
server 10.0.0.82:9000;
}
server {
server_name wordpress.abc.com;
listen 80;
location / {
root /data/project/wordpress;
index index.php index.html index.html;
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass wordpress;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
~
~
[13:08:54 root@lb ~]#mkdir /data/project/wordpress -p
[13:08:57 root@lb ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[13:18:54 root@lb ~]#nginx -s reload
2.2 配置10.0.0.81-php
[13:11:44 root@developer-1 ~]#yum -y install php-fpm php-mysqlnd php-json
mkdir /code/wordpress
[13:14:13 root@developer-1 ~]#vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 9000
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
;listen.allowed_clients = 127.0.0.1
[13:15:17 root@developer-1 ~]#systemctl enable --now php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
2.3 配置10.0.0.82-php
[13:11:45 root@developer-2 ~]#yum -y install php-fpm php-mysqlnd php-json
mkdir /code/wordpress
[13:16:21 root@developer-2 ~]#vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = 9000
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
;listen.allowed_clients = 127.0.0.1
[13:16:59 root@developer-2 ~]#systemctl enable --now php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
2.4 部署MySQL
10.0.0.83
[15:30:51 root@mysql ~]#yum -y install mysql-server
[15:30:51 root@mysql ~]#systemctl enable --now mysqld
[15:30:51 root@mysql ~]#mysql -e "create database wordpress"
[15:30:51 root@mysql ~]#mysql -e 'create user wordpress@"10.0.0.%" identified by "wordpress"'
[15:30:51 root@mysql ~]#mysql -e 'grant all on wordpress.* to wordpress@"10.0.0.%"'
2.5 部署wordpress
# 此前wordpress代码已经从developer-2推送到gitlab
这里需要先修改wordpress中的mysql配置, 然后再次推送到master分支, 并且打标签tag v1.1
[15:34:34 root@developer-2 ~]#cd wordpress/
[15:34:38 root@developer-2 ~/wordpress]#cp wp-config-sample.php wp-config.php
[15:38:07 root@developer-2 ~/wordpress]#vim wp-config.php
...
...
define( 'DB_NAME', 'wordpress' );
/** MySQL数据库用户名 */
define( 'DB_USER', 'wordpress' );
/** MySQL数据库密码 */
define( 'DB_PASSWORD', 'wordpress' );
/** MySQL主机 */
define( 'DB_HOST', '10.0.0.83' );
...
...
[15:40:25 root@developer-2 ~/wordpress]#chown -R nginx.nginx ~/wordpress
# 推送到gitlab
[15:40:47 root@developer-2 ~/wordpress]#git add .
[15:42:23 root@developer-2 ~/wordpress]#git commit -m "添加数据库信息"
[15:42:44 root@developer-2 ~/wordpress]#git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.65 KiB | 1.65 MiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To gitlab.abc.com:devops/wordpress.git
e982fa1..9a39511 master -> master
[15:44:04 root@developer-2 ~/wordpress]#git tag -a 'v1.1' -m '添加数据库地址'
[15:44:13 root@developer-2 ~/wordpress]#git tag
v1.1
[15:44:15 root@developer-2 ~/wordpress]#git push origin v1.1
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 189 bytes | 189.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To gitlab.abc.com:devops/wordpress.git
* [new tag] v1.1 -> v1.1
2.6 再次构建, 之后把jenkins目录下的代码复制到php服务器和nginx上
[15:48:37 root@jenkins /var/lib/jenkins/workspace/wordpress]#scp -r * 10.0.0.81:/code/wordpress
[15:48:37 root@jenkins /var/lib/jenkins/workspace/wordpress]#scp -r * 10.0.0.82:/code/wordpress
[15:51:04 root@jenkins /var/lib/jenkins/workspace/wordpress]#scp -r * 10.0.0.217:/data/project/wordpress
2.7 浏览器访问wordpress.abc.com
# 此时, 后端php服务器即使宕机一个, 也不影响访问
# 此外, nginx本地也要有wordpress代码, 因为静态页面需要nginx来响应
3. 实现代码自动上线
# 配置jenkins免秘钥登录10.0.0.217
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.217
3.1 上线脚本
[16:17:50 root@jenkins /data/scripts]#vim wordpress.sh
#!/bin/bash
DATE=`date +%Y-%m-%d-%H-%M-%S`
php_server="10.0.0.81 10.0.0.82"
#代码在jenkins上打包路径, 并且有jenkins复制到php服务器该路径
sdir="/opt"
#代码在服务器上的部署路径
ddir="/code"
#1. 进入jenkins项目目录, 将代码打包
get_code(){
cd ${WORKSPACE} && \
tar czf ${sdir}/wordpress-${DATE}.tar.gz ./*
}
#2. 将内容通告scp拷贝到php集群
scp_php_server(){
for host in $php_server; do
scp ${sdir}/wordpress-${DATE}.tar.gz root@$host:${sdir}
ssh root@$host "mkdir -p ${ddir}/wordpress-${DATE} && \
tar xf ${sdir}/wordpress-${DATE}.tar.gz -C ${ddir}/wordpress-${DATE} && \
rm -rf ${ddir}/wordpress && \
ln -s ${ddir}/wordpress-${DATE} ${ddir}/wordpress"
done
}
deploy(){
get_code
scp_php_server
}
deploy
3.2 执行构建
3.3 访问网站
3.4 实现项目代码升级发布
- developer-2上修改代码, 在wordpress目录下创建jenkins/index.php, 模拟php代码更改
[19:15:37 root@developer-2 ~]#cd wordpress/
[19:15:39 root@developer-2 ~/wordpress]#ls
index.php readme.html wp-admin wp-comments-post.php wp-config-sample.php wp-cron.php wp-links-opml.php wp-login.php wp-settings.php wp-trackback.php
license.txt wp-activate.php wp-blog-header.php wp-config.php wp-content wp-includes wp-load.php wp-mail.php wp-signup.php xmlrpc.php
[19:15:42 root@developer-2 ~/wordpress]#mkdir jenkins
[19:15:45 root@developer-2 ~/wordpress]#vim jenkins/index.php
<?php
echo "v1.2"
?>
- 推送到gitlab
[19:22:36 root@developer-2 ~/wordpress]#git remote -v
origin http://gitlab.abc.com/devops/wordpress.git (fetch)
origin http://gitlab.abc.com/devops/wordpress.git (push)
[19:22:39 root@developer-2 ~/wordpress]#git add .
[19:22:46 root@developer-2 ~/wordpress]#git commit -m '添加jenkins/index.php-v1.2'
[master aecf334] 添加jenkins/index.php-v1.2
1 file changed, 3 insertions(+)
create mode 100644 jenkins/index.php
[19:22:57 root@developer-2 ~/wordpress]#git push origin master
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 367 bytes | 367.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
45ee43a..aecf334 master -> master
[19:23:40 root@developer-2 ~/wordpress]#git tag -a 'v1.2' -m 'jenkins/index.php-v1.2'
[19:23:58 root@developer-2 ~/wordpress]#git push origin v1.2
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 172 bytes | 172.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
* [new tag] v1.2 -> v1.2
- Jenkins拉取master代码, 执行构建
[19:26:27 root@developer-1 /code/wordpress]#ll jenkins/
total 4
-rw-r--r-- 1 root root 25 Jun 1 19:25 index.php
[19:26:37 root@developer-2 /code/wordpress]#ll jenkins/
total 4
-rw-r--r-- 1 root root 25 Jun 1 19:25 index.php
-
访问测试
3.5 配置基于tag发布的参数化构建
- 修改构建脚本
[19:39:46 root@jenkins /var/lib/jenkins/workspace]#vim /data/scripts/wordpress.sh
#!/bin/bash
DATE=`date +%Y-%m-%d-%H-%M-%S`
php_server="10.0.0.81 10.0.0.82"
#代码在jenkins上打包路径, 并且有jenkins复制到php服务器该路径
sdir="/opt"
#代码在服务器上的部署路径
ddir="/code"
#1. 进入jenkins项目目录, 将代码打包
get_code(){
cd ${WORKSPACE} && \
tar czf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz ./*
}
#2. 将内容通告scp拷贝到php集群
scp_php_server(){
for host in $php_server; do
scp ${sdir}/wordpress-${DATE}-${git_version}.tar.gz root@$host:${sdir}
ssh root@$host "mkdir -p ${ddir}/wordpress-${DATE}-${git_version} && \
tar xf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz -C ${ddir}/wordpress-${DATE}-${git_version} && \
rm -rf ${ddir}/wordpress && \
ln -s ${ddir}/wordpress-${DATE}-${git_version} ${ddir}/wordpress"
done
}
deploy(){
get_code
scp_php_server
}
deploy
- 执行构建, 可以看到一共有两个Gitlab上的tag版本
- 多提交几个版本, 测试升级和回滚
# v1.3
[19:45:14 root@developer-2 ~/wordpress/jenkins]#vim index.php
<?php
echo "v1.3"
?>
[19:45:24 root@developer-2 ~/wordpress/jenkins]#git add index.php
[19:45:27 root@developer-2 ~/wordpress/jenkins]#git commit -m '提交v1.3'
[master 1c396e6] 提交v1.3
1 file changed, 1 insertion(+), 1 deletion(-)
[19:45:36 root@developer-2 ~/wordpress/jenkins]#git push origin master
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 336 bytes | 112.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
aecf334..1c396e6 master -> master
[19:45:49 root@developer-2 ~/wordpress/jenkins]#git tag -a 'v1.3' -m '提交v1.3'
[19:46:02 root@developer-2 ~/wordpress/jenkins]#git push origin v1.3
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 173 bytes | 173.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
* [new tag] v1.3 -> v1.3
# v1.4
[19:45:40 root@developer-2 ~/wordpress/jenkins]#vim index.php
<?php
echo "v1.4"
?>
[19:46:51 root@developer-2 ~/wordpress/jenkins]#git add index.php
[19:47:15 root@developer-2 ~/wordpress/jenkins]#git commit -m '提交v1.4'
[master 0e28f98] 提交v1.4
1 file changed, 1 insertion(+), 1 deletion(-)
[19:47:20 root@developer-2 ~/wordpress/jenkins]#git push origin master
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 337 bytes | 337.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
1c396e6..0e28f98 master -> master
[19:47:30 root@developer-2 ~/wordpress/jenkins]#git tag -a 'v1.4' -m '提交v1.4'
[19:47:39 root@developer-2 ~/wordpress/jenkins]#git push origin v1.4
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 172 bytes | 172.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
* [new tag] v1.4 -> v1.4
# v1.5
[19:48:16 root@developer-2 ~/wordpress/jenkins]#vim index.php
<?php echo "v1.5"
?>
[19:48:38 root@developer-2 ~/wordpress/jenkins]#git add index.php
[19:48:45 root@developer-2 ~/wordpress/jenkins]#git commit -m '提交v1.5'
[master 5e57f67] 提交v1.5
1 file changed, 1 insertion(+), 1 deletion(-)
[19:48:51 root@developer-2 ~/wordpress/jenkins]#git push origin master
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 336 bytes | 336.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
0e28f98..5e57f67 master -> master
[19:49:01 root@developer-2 ~/wordpress/jenkins]#git tag -a 'v1.5' -m '提交v1.5'
[19:49:14 root@developer-2 ~/wordpress/jenkins]#git push origin v1.5
Username for 'http://gitlab.abc.com': developer-2
Password for 'http://developer-2@gitlab.abc.com':
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 172 bytes | 172.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To http://gitlab.abc.com/devops/wordpress.git
* [new tag] v1.5 -> v1.5
- 测试升级和回滚
- 发布v1.5
- 回退到v1.2
3.6 实现选择部署还是回滚
- 修改jenkins, 添加选项参数
- 修改构建脚本
[19:58:59 root@jenkins /var/lib/jenkins/workspace]#vim /data/scripts/wordpress.sh
#!/bin/bash
DATE=`date +%Y-%m-%d-%H-%M-%S`
php_server="10.0.0.81 10.0.0.82"
#代码在jenkins上打包路径, 并且有jenkins复制到php服务器该路径
sdir="/opt"
#代码在服务器上的部署路径
ddir="/code"
#1. 进入jenkins项目目录, 将代码打包
get_code(){
cd ${WORKSPACE} && \
tar czf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz ./*
}
#2. 将内容通告scp拷贝到php集群
scp_php_server(){
for host in $php_server; do
scp ${sdir}/wordpress-${DATE}-${git_version}.tar.gz root@$host:${sdir}
ssh root@$host "mkdir -p ${ddir}/wordpress-${DATE}-${git_version} && \
tar xf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz -C ${ddir}/wordpress-${DATE}-${git_version} && \
rm -rf ${ddir}/wordpress && \
ln -s ${ddir}/wordpress-${DATE}-${git_version} ${ddir}/wordpress"
done
}
deploy(){
get_code
scp_php_server
}
rollback(){
echo "rollback"
}
if [ ${deploy_env} == "deploy" ]; then
deploy
elif [ ${deploy_env} == "rollback" ]; then
rollback
fi
- 部署v1.5
- 回退到v1.3
因为构建脚本还没有添加回退功能, 所以只能看到拉取了v1.3版本
- 增加回退功能, 先把/code目录下多余的版本删除
l[20:06:01 root@developer-2 /code]#ll
total 8
lrwxrwxrwx 1 root root 40 Jun 1 20:02 wordpress -> /code/wordpress-2021-06-01-20-02-01-v1.5
drwxr-xr-x 6 root root 4096 Jun 1 19:53 wordpress-2021-06-01-19-53-36-v1.2
drwxr-xr-x 6 root root 4096 Jun 1 20:02 wordpress-2021-06-01-20-02-01-v1.5
[20:06:27 root@developer-1 /code]#ll
total 8
lrwxrwxrwx 1 root root 40 Jun 1 20:02 wordpress -> /code/wordpress-2021-06-01-20-02-01-v1.5
drwxr-xr-x 6 root root 4096 Jun 1 19:53 wordpress-2021-06-01-19-53-36-v1.2
drwxr-xr-x 6 root root 4096 Jun 1 20:02 wordpress-2021-06-01-20-02-01-v1.5
- 修改构建脚本, 增加回退功能
[20:09:11 root@jenkins /var/lib/jenkins/workspace]#vim /data/scripts/wordpress.sh
#!/bin/bash
DATE=`date +%Y-%m-%d-%H-%M-%S`
php_server="10.0.0.81 10.0.0.82"
#代码在jenkins上打包路径, 并且有jenkins复制到php服务器该路径
sdir="/opt"
#代码在服务器上的部署路径
ddir="/code"
#1. 进入jenkins项目目录, 将代码打包
get_code(){
cd ${WORKSPACE} && \
tar czf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz ./*
}
#2. 将内容通告scp拷贝到php集群
scp_php_server(){
for host in $php_server; do
scp ${sdir}/wordpress-${DATE}-${git_version}.tar.gz root@$host:${sdir}
ssh root@$host "mkdir -p ${ddir}/wordpress-${DATE}-${git_version} && \
tar xf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz -C ${ddir}/wordpress-${DATE}-${git_version} && \
rm -rf ${ddir}/wordpress && \
ln -s ${ddir}/wordpress-${DATE}-${git_version} ${ddir}/wordpress"
done
}
deploy(){
get_code
scp_php_server
}
rollback(){
rollback_file=`ssh root@10.0.0.81 "find /code/ -maxdepth 1 -type d -name "wordpress-*-${git_version}""`
for host in $php_server; do
ssh root@$host "rm -rf ${ddir}/wordpress && \
ln -s ${rollback_file} ${ddir}/wordpress"
done
}
if [ ${deploy_env} == "deploy" ]; then
deploy
elif [ ${deploy_env} == "rollback" ]; then
rollback
fi
- 测试部署和回退
部署1.3
回退到1.2
- 解决重复构建
[20:11:39 root@jenkins /var/lib/jenkins/workspace]#vim /data/scripts/wordpress.sh
#!/bin/bash
DATE=`date +%Y-%m-%d-%H-%M-%S`
php_server="10.0.0.81 10.0.0.82"
#代码在jenkins上打包路径, 并且有jenkins复制到php服务器该路径
sdir="/opt"
#代码在服务器上的部署路径
ddir="/code"
#1. 进入jenkins项目目录, 将代码打包
get_code(){
cd ${WORKSPACE} && \
tar czf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz ./*
}
#2. 将内容通告scp拷贝到php集群
scp_php_server(){
for host in $php_server; do
scp ${sdir}/wordpress-${DATE}-${git_version}.tar.gz root@$host:${sdir}
ssh root@$host "mkdir -p ${ddir}/wordpress-${DATE}-${git_version} && \
tar xf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz -C ${ddir}/wordpress-${DATE}-${git_version} && \
rm -rf ${ddir}/wordpress && \
ln -s ${ddir}/wordpress-${DATE}-${git_version} ${ddir}/wordpress"
done
}
deploy(){
get_code
scp_php_server
}
rollback(){
rollback_file=`ssh root@10.0.0.81 "find /code/ -maxdepth 1 -type d -name "wordpress-*-${git_version}""`
for host in $php_server; do
ssh root@$host "rm -rf ${ddir}/wordpress && \
ln -s ${rollback_file} ${ddir}/wordpress"
done
}
if [ ${deploy_env} == "deploy" ]; then
if [ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ]; then
echo "已经部署过该版本, ${git_version}"
exit 1
else
deploy
fi
elif [ ${deploy_env} == "rollback" ]; then
rollback
fi
- 测试部署v1.2
由于1.2版本此前部署过, 因此会提示已经部署, 直接退出构建
- 接下来要添加部署或者回退后重启php-fpm, 以及将新的代码也复制给nginx
#!/bin/bash
DATE=`date +%Y-%m-%d-%H-%M-%S`
php_server="10.0.0.81 10.0.0.82"
#代码在jenkins上打包路径, 并且有jenkins复制到php服务器该路径
sdir="/opt"
#代码在服务器上的部署路径
ddir="/code"
#1. 进入jenkins项目目录, 将代码打包
get_code(){
cd ${WORKSPACE} && \
tar czf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz ./*
}
#2. 将内容通告scp拷贝到php集群
scp_php_server(){
for host in $php_server; do
scp ${sdir}/wordpress-${DATE}-${git_version}.tar.gz root@$host:${sdir}
ssh root@$host "mkdir -p ${ddir}/wordpress-${DATE}-${git_version} && \
tar xf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz -C ${ddir}/wordpress-${DATE}-${git_version} && \
rm -rf ${ddir}/wordpress && \
ln -s ${ddir}/wordpress-${DATE}-${git_version} ${ddir}/wordpress && \
systemctl restart php-fpm && echo 'php重启成功'"
done
scp ${sdir}/wordpress-${DATE}-${git_version}.tar.gz root@10.0.0.217:${sdir}
ssh root@10.0.0.217 "mkdir -p /data/project/wordpress-${DATE}-${git_version} && \
tar xf ${sdir}/wordpress-${DATE}-${git_version}.tar.gz -C /data/project/wordpress-${DATE}-${git_version} && \
rm -rf /data/project/wordpress && \
ln -s /data/project/wordpress-${DATE}-${git_version} /data/project/wordpress"
}
deploy(){
get_code
scp_php_server
}
rollback(){
rollback_file=`ssh root@10.0.0.81 "find /code/ -maxdepth 1 -type d -name "wordpress-*-${git_version}""`
rollbackfile_for_nginx=`basename ${rollback_file}`
for host in $php_server; do
ssh root@$host "rm -rf ${ddir}/wordpress && \
ln -s ${rollback_file} ${ddir}/wordpress"
done
ssh root@10.0.0.217 "rm -rf /data/project/wordpress && \
ln -s /data/project/${rollback_file_for_nginx} /data/project/wordpress"
}
if [ ${deploy_env} == "deploy" ]; then
if [ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ]; then
echo "已经部署过该版本, ${git_version}"
exit 1
else
deploy
fi
elif [ ${deploy_env} == "rollback" ]; then
rollback
fi
- 实际工作中, 开发会把代码推送到gitlab, 无需运维人员修改代码, 只需要根据不同项目以及业务需求, 完成构建和发布即可
链接:https://www.jianshu.com/p/d7d48882def1