Jenkins+Git+Gitlab+Ansible实现持续集成⾃动化部署静态⽹站
(6)
前⾔
在之前已经写了关于Git,Gitlab以及Ansible的两篇博客《》,《》,以及关于jenkins的简单使⽤《》。相信⼤家也已经完全掌握了这三项⼯具的使⽤,也可以使⽤这⼏项⼯具可以部署静态以及动态⽹站了。
以前的博客可以实现⼀键部署⽹站了,但是并没有实现持续化集成部署⽹站。沉重的⼯作还是落在了可怜的运维⼯程师上⾯。
但是你有没有想过这样⼀个问题,假如⽹站全部部署成功了,现在我们的开发程序员隔三差五的修改⽹站上的某些功能或者修改页⾯内容,难道都需要运维⼈员再⼿动执⾏命令吗?有没有⼀种⽅法使得程序员修改完成代码之后可以⾃⼰测试、部署上线哪?
回答是有的!jenkins就可以完成上述的⼯作了。
本篇博客将使⽤git+gitlab+ansible+jenkins实现真正的持续化⼀键部署静态⽹站
下⼀篇博客将介绍如何使⽤git+gitlab+ansible+jenkins部署⼀套动态的⽹站。敬请期待。
Gitlab创建项⽬
第⼀步:gitlab的安装即配置
请参考我之前的博客《》
第⼆步:创建项⽬
如下图,我创建了⼀个static_web的项⽬
Git下载仓库
第⼀步:创建⽬录并下载仓库
[root@ken ~]# mkdir /ken
[root@ken ~]# cd /ken
[root@ken ken]# git clone 10.220.5.137/webg1/static_web.git
Cloning into 'static_web'...
Username for '10.220.5.137': root
Password for 'root@10.220.5.137':
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
Ansible剧本编写
第⼀步:进⼊到上⾯下载下来的⼯作⽬录中
[root@ken ken]# ls
static_web
[root@ken ken]# cd static_web/
[root@ken static_web]# ls -a
.
  ..  .git  README
第⼆步:使⽤roles来编写剧本
⾸先需要创建相关的⽬录
[root@ken static_web]# mkdir roles/httpd/{tasks,vars,files} -p
第三步:编写tasks⽂件
[root@ken static_web]# vim roles/httpd/l
[root@ken static_web]# cat roles/httpd/l
- name: install httpd
yum: name=httpd state=present
- name: start httpd
service: name=httpd state=restarted
-
name: copy test file to httpd
copy: src=roles/httpd/files/index.html dest=/var/www/html
第四步:编写file下的测试⽂件
[root@ken static_web]# vim roles/httpd/files/index.html
[root@ken static_web]# cat roles/httpd/files/index.html
test for static web
第五步:编写主机清单
[root@ken static_web]# cat inventory/test
[ken]
10.220.5.138
第六步:编写剧本⽂件
[root@ken static_web]# l
[root@ken static_web]# l
- hosts: ken
remote_user: root
roles:
- httpd
第七步:执⾏剧本
注意:在执剧本的时候需要使⽤-i指定你创建的主机列表清单,否则会不到需要执⾏的节点可以看到剧本执⾏完成也没有报错
[root@ken static_web]# ansible-playbook -i inventory/l
PLAY [ken] ***********************************************************************
TASK [Gathering Facts] ***********************************************************
ok: [10.220.5.138]
TASK [httpd : install httpd] *****************************************************
changed: [10.220.5.138]
TASK [httpd : start httpd] *******************************************************
changed: [10.220.5.138]
TASK [httpd : copy test file to httpd] *******************************************
changed: [10.220.5.138]
PLAY RECAP ***********************************************************************
10.220.5.138              : ok=4    changed=3    unreachable=0    failed=0
第⼋步:⽹页浏览
现在就可以登录该节点进⾏访问了
⽂件提交⾄gitlab
经过上⾯的步骤之后,已经部署完成了⼀个静态页⾯
现在把这些⽂件提交⾄gitlab
第⼀步:⽂件提交⾄仓库
[root@ken static_web]# git add .
[root@ken static_web]# git commit -m "v1"
第⼆步:报错
提交报了这个错
*** Please tell me who you are.
Run
git config --ail "you@example"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@ken.(none)')
直接执⾏命令
[root@ken static_web]# git config --ail "you@example"
[root@ken static_web]# git config --global user.name "Your Name"
第三步:推送⾄gitlab
[root@ken static_web]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Username for '10.220.5.137': root    ####输⼊连接你的gitlab的⽤户名
Password for 'root@10.220.5.137':    ###输⼊该⽤户密码
Counting objects: 12, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (11/11), 815 bytes | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To 10.220.5.137/webg1/static_web.git
<2ac703  master -> master
第四步:web端查看结果
发现v1版已经上传成功
jenkins 实现持续集成
经过上⾯的⼀些操作之后,我们已经完成了静态⽹站的部署,已经代码的上传
但是发现还是每次执⾏需要输⼊命令等
现在我们就使⽤jenkins来实现持续化部署
第⼀步:jenkins中创建任务
静态网站和动态网站区别
我创建了⼀个⾃由风格的软件项⽬
项⽬名称为test_for_static_web
输⼊完毕之后选择确定即可
第⼆步:添加源码管理信息
这⾥的url就是你的项⽬的地址
下⾯的凭证,点击Add输⼊你的gitlab的密码和账号即可
第三步:选择构建
增加侯建步骤选择执⾏shell
第四步:编写shell
第⼀⾏:指定bash为解释器,和编写shell⼀样
第⼆⾏:进⼊到⼯作⽬录,这⾥引⽤了了⼀个变量,意思是⼯作⽬录,因为我们的剧本⾥⾯都是相对路径所以我们需要进⼊到git拉取下来的⽂件中进⾏执⾏可以点击下⾯的可⽤环境列表了解更多
第三⾏:即执⾏剧本
以上步骤完成之后点击下⽅的保存即可
第五步:查看执⾏结果
点击⽴即构建之后,在最下⽅会出现⼀个圆圈,这个圆圈红⾊代表失败,蓝⾊代表成功,可以⿏标放上去显⽰的
第六步:查看失败原因
点击下⽅的红⾊圆圈即可查看失败原因
这⾥的失败原因是因为运⾏jenkins程序的是jenkins⽤户,我们连接节点的秘钥是root的,所以现在连接不上
第七步:更改jenkins的配置⽂件
把运⾏jenkins的⽤户更改为root即可
更改完成之后重启jenkins
[root@ken static_web]# sed -i 's/JENKINS_USER="jenkins"/JENKINS_USER="root"/' /etc/sysconfig/jenkins
[root@ken static_web]# systemctl restart jenkins
第⼋步:再次执⾏构建
再次点击构建可以发现现在红⾊圆圈变成了蓝⾊的成功圆圈
点击⼀下这个成功的圆圈查看运⾏过程
第九步:查看⼯作⽬录
许多⼈会疑惑,在这⾥使⽤的git clone,它把⽂件下载到哪⾥去了那?
其实是放在jenkins的⼯作⽬录之下的你的项⽬名称⾥⾯了,还记得我们的项⽬名称是test_for_static_web吗?[root@ken static_web]# ls /var/lib/jenkins/workspace/
test_for_static_web/
查看⼀下这个⽬录下⾯都有什么
看到了吧,从gitlab克隆的回来的⽂件都放在了这⾥即jenkins⼯作⽬录下>你的任务名称⾯
[root@ken static_web]# ls /var/lib/jenkins/workspace/test_for_static_web
inventory    l  README  roles
更改⽹站数据
现在我们的⼯程师就可以在他的电脑上⾯更改⽹站数据,并实现持久集成⾃动化部署了
第⼀步:克隆数据
现在我使⽤的电脑IP 为10.220.5.139,现在假如另外⼀位⼯程师的电脑是10.220.5.137上⾯
[root@ken tmp]# mkdir p
[root@ken tmp]# cd p
[root@ken p]# git clone 10.220.5.137/webg1/static_web.git
Cloning into 'static_web'...
Username for '10.220.5.137': root
Password for 'root@10.220.5.137':
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 14 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (14/14), done.
[root@ken p]#
第⼆步:更改⽹站数据
[root@ken static_web]# echo "this is new data for static web">> roles/httpd/files/index.html
第三步:提交
[root@ken static_web]# git add .
[root@ken static_web]# git commit -m "v2"
[master e5f5d42] v2
1 file changed, 1 insertion(+)
[root@ken static_web]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Username for '10.220.5.137': root
Password for 'root@10.220.5.137':
Counting objects: 11, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 443 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To 10.220.5.137/webg1/static_web.git
<5f5d42  master -> master