PHP 基于 Jenkins 和 Ansible 实现自动化发布和回滚

简要说明

通过 Jenkins 从 Gitlab 拉取 PHP 代码,然后再结合 Ansible 使用编写好的 deploy roles 和 rollback roles 剧本来实现对 Web 服务器上的 PHP代码快速发布与回滚。这里以 WordPress 项目为例来进行演示。

此篇文章不涉及到关于 Git、Ansible、Jenkins 的安装配置。

此篇文章的 Ansible 代码地址:https://github.com/wdroot/ansible/tree/master/deploy_php

环境信息

CentOS 7.6
Jenkins 2.176:10.14.6.250
PHP 7.2/GitLab 12.2.4:10.14.6.85

配置步骤

打开 Jenkins 主页面左侧,选择新建任务,任务名称为 wordpress,选择构建一个自由风格的软件项目。

接下来,勾选参数化构建过程,选择 Choice Parameter(选项参数)
《PHP 基于 Jenkins 和 Ansible 实现自动化发布和回滚》

配置 PHP 的 git 地址
《PHP 基于 Jenkins 和 Ansible 实现自动化发布和回滚》

配置 Jenkins 上的 Ansible
《PHP 基于 Jenkins 和 Ansible 实现自动化发布和回滚》

PHP 代码发布

《PHP 基于 Jenkins 和 Ansible 实现自动化发布和回滚》

控制台输出的信息如下:

Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/wordpress
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@10.14.6.85:root/wordpress.git # timeout=10
Fetching upstream changes from git@10.14.6.85:root/wordpress.git
 > git --version # timeout=10
 > git fetch --tags --progress git@10.14.6.85:root/wordpress.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8e74823bcdfd7091dfff3429a28250765e3ba86d (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 8e74823bcdfd7091dfff3429a28250765e3ba86d
Commit message: "edit license for test"
 > git rev-list --no-walk 8e74823bcdfd7091dfff3429a28250765e3ba86d # timeout=10
[wordpress] $ /bin/sh -xe /tmp/jenkins5845111792105841357.sh
+ [[ deploy == \d\e\p\l\o\y ]]
++ date +%Y%m%d%H%M
+ ansible-playbook -e 'job_name=wordpress release_version=201909261122' /etc/ansible/deploy.yml

PLAY [web] *********************************************************************

TASK [deploy : include_tasks] **************************************************
included: /etc/ansible/roles/deploy/tasks/setup.yml for 10.14.6.85

TASK [deploy : Get src code path info] *****************************************

ok: [10.14.6.85]

TASK [deploy : Ensure deployment basedir exists] *******************************

ok: [10.14.6.85]

TASK [deploy : Ensure release path exists] *************************************

changed: [10.14.6.85]

TASK [deploy : Yum install rsync] **********************************************

ok: [10.14.6.85]

TASK [deploy : include_tasks] **************************************************

included: /etc/ansible/roles/deploy/tasks/update.yml for 10.14.6.85

TASK [deploy : Sync release code to release_path] ******************************

changed: [10.14.6.85]

TASK [deploy : Change code file attribute] *************************************

changed: [10.14.6.85]

TASK [deploy : Change application link to new release] *************************

changed: [10.14.6.85]

TASK [deploy : include_tasks] **************************************************
included: /etc/ansible/roles/deploy/tasks/cleanup.yml for 10.14.6.85

TASK [deploy : Clean up releases] **********************************************
skipping: [10.14.6.85]

PLAY RECAP *********************************************************************
10.14.6.85                 : ok=10   changed=4    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

Finished: SUCCESS

从上面的输出信息,能看到这次部署的版本号信息: release_version=201909261122。
登录到 PHP 服务器(10.14.6.85)查看刚才的部署信息:

# ls -lt /opt/app/wordpress/
total 8
drwxr-xr-x 5 nginx nginx 4096 Sep 26 11:22 201909261122
drwxr-xr-x 5 nginx nginx 4096 Sep 26 10:29 201909261120
# ls -l /var/www/wordpress
lrwxrwxrwx 1 root root 31 Sep 26 11:23 /var/www/wordpress -> /opt/app/wordpress/201909261122

PHP 代码回滚

《PHP 基于 Jenkins 和 Ansible 实现自动化发布和回滚》

执行回滚后,控制台输出信息如下:

Started by user admin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/wordpress
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@10.14.6.85:root/wordpress.git # timeout=10
Fetching upstream changes from git@10.14.6.85:root/wordpress.git
 > git --version # timeout=10
 > git fetch --tags --progress git@10.14.6.85:root/wordpress.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8e74823bcdfd7091dfff3429a28250765e3ba86d (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 8e74823bcdfd7091dfff3429a28250765e3ba86d
Commit message: "edit license for test"
 > git rev-list --no-walk 8e74823bcdfd7091dfff3429a28250765e3ba86d # timeout=10
[wordpress] $ /bin/sh -xe /tmp/jenkins6915046394505464093.sh
+ [[ rollback == \d\e\p\l\o\y ]]
+ ansible-playbook -e job_name=wordpress /etc/ansible/rollback.yml

PLAY [web] *********************************************************************

TASK [rollback : include_tasks] ************************************************
included: /etc/ansible/roles/rollback/tasks/setup.yml for 10.14.6.85

TASK [rollback : Get rollback basedir] *****************************************
ok: [10.14.6.85]

TASK [rollback : Get release count] ********************************************

changed: [10.14.6.85]

TASK [rollback : Check there is more than one release] *************************
skipping: [10.14.6.85]

TASK [rollback : Get specified rollback release path] **************************

skipping: [10.14.6.85]

TASK [rollback : Check the provided rollback release exists] *******************
skipping: [10.14.6.85]

TASK [rollback : Get previous releases version] ********************************

changed: [10.14.6.85]

TASK [rollback : Get rollback release version] *********************************

changed: [10.14.6.85]

TASK [rollback : Get rollback release path info] *******************************

ok: [10.14.6.85]

TASK [rollback : include_tasks] ************************************************
included: /etc/ansible/roles/rollback/tasks/rollback.yml for 10.14.6.85

TASK [rollback : Rollback application link to old release] *********************

changed: [10.14.6.85]

PLAY RECAP *********************************************************************
10.14.6.85                 : ok=8    changed=4    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   

Finished: SUCCESS

从控制台最后的输出信息中能够看到,已经成功回滚到上一个正式版本。
登录到 PHP 服务器(10.14.6.85)确认一下:

# ls -lt /opt/app/wordpress
total 8
drwxr-xr-x 5 nginx nginx 4096 Sep 26 11:22 201909261122
drwxr-xr-x 5 nginx nginx 4096 Sep 26 10:29 201909261120
# ls -l /var/www/wordpress
lrwxrwxrwx 1 root root 31 Sep 26 11:45 /var/www/wordpress -> /opt/app/wordpress/201909261120

从 PHP 服务器上能够看到已经成功回滚到上一个版本。

关于如何保留旧版本的数量、如何回滚到指定版本以及 Ansible roles 代码等信息:
https://github.com/wdroot/ansible/tree/master/deploy_php

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注