Jenkins+Git+Gitlab+Ansible實現持續集成自動化部署靜態網站(一)--技術流ken
前言
在之前已經寫了關於Git,Gitlab以及Ansible的兩篇博客《Git+Gitlab+Ansible劇本實現一鍵部署Nginx--技術流ken》,《Git+Gitlab+Ansible劇本實現一鍵部署動態網站(二)--技術流ken》,以及關於jenkins的簡單使用《Jenkins持續集成介紹及插件安裝版本更新演示(一)--技術流ken》。相信大家也已經完全掌握了這三項工具的使用,也可以使用這幾項工具可以部署靜態以及動態網站了。
以前的博客可以實現一鍵部署網站了,但是並沒有實現持續化集成部署網站。沈重的工作還是落在了可憐的運維工程師上面。
但是你有沒有想過這樣一個問題,假如網站全部部署成功了,現在我們的開發程序員隔三差五的修改網站上的某些功能或者修改頁面內容,難道都需要運維人員再手動執行命令嗎?有沒有一種方法使得程序員修改完成代碼之後可以自己測試、部署上線哪?
回答是有的!jenkins就可以完成上述的工作了。
本篇博客將使用git+gitlab+ansible+jenkins實現真正的持續化一鍵部署靜態網站
下一篇博客將介紹如何使用git+gitlab+ansible+jenkins部署一套動態的網站。敬請期待。
Gitlab創建項目
第一步:gitlab的安裝即配置
請參考我之前的博客《Gitlab在linux/windows中免密使用(二)--技術流ken》
第二步:創建項目
如下圖,我創建了一個static_web的項目
Git下載倉庫
第一步:創建目錄並下載倉庫
[root@ken ~]# mkdir /ken [root@ken~]# cd /ken [root@ken ken]# git clone http://10.220.5.137/webg1/static_web.git Cloning into ‘static_web‘... Username for ‘http://10.220.5.137‘: root Password for ‘http://[email protected]‘: 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/tasks/main.yml [root@ken static_web]# cat roles/httpd/tasks/main.yml - 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]# vim ken.yml [root@ken static_web]# cat ken.yml - hosts: ken remote_user: root roles: - httpd
第七步:執行劇本
註意:在執劇本的時候需要使用-i指定你創建的主機列表清單,否則會找不到需要執行的節點
可以看到劇本執行完成也沒有報錯
[root@ken static_web]# ansible-playbook -i inventory/test ken.yml 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 --global user.email "[email protected]" 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 --global user.email "[email protected]" [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 ‘http://10.220.5.137‘: root ####輸入連接你的gitlab的用戶名 Password for ‘http://[email protected]‘: ###輸入該用戶密碼 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 http://10.220.5.137/webg1/static_web.git c47e814..e2ac703 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 ken.retry ken.yml 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 http://10.220.5.137/webg1/static_web.git Cloning into ‘static_web‘... Username for ‘http://10.220.5.137‘: root Password for ‘http://[email protected]‘: 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 ‘http://10.220.5.137‘: root Password for ‘http://[email protected]‘: 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 http://10.220.5.137/webg1/static_web.git e2ac703..e5f5d42 master -> master
第三步:jenkins持續化部署
點擊構建
發現沒有報錯
第四步:網站查看
發現網站數據已經更新
Jenkins+Git+Gitlab+Ansible實現持續集成自動化部署靜態網站(一)--技術流ken