在以前已經寫了關於Git,Gitlab以及Ansible的兩篇博客《Git+Gitlab+Ansible劇本實現一鍵部署Nginx--技術流ken》,《Git+Gitlab+Ansible劇本實現一鍵部署動態網站(二)--技術流ken》,以及關於jenkins的簡單使用《Jenkins持續集成介紹及插件安裝版本更新演示(一)--技術流ken》。相信你們也已經徹底掌握了這三項工具的使用,也能夠使用這幾項工具能夠部署靜態以及動態網站了。html
之前的博客能夠實現一鍵部署網站了,可是並無實現持續化集成部署網站。沉重的工做仍是落在了可憐的運維工程師上面。linux
可是你有沒有想過這樣一個問題,假如網站所有部署成功了,如今咱們的開發程序員隔三差五的修改網站上的某些功能或者修改頁面內容,難道都須要運維人員再手動執行命令嗎?有沒有一種方法使得程序員修改完成代碼以後能夠本身測試、部署上線哪?git
回答是有的!jenkins就能夠完成上述的工做了。程序員
本篇博客將使用git+gitlab+ansible+jenkins實現真正的持續化一鍵部署靜態網站web
下一篇博客將介紹如何使用git+gitlab+ansible+jenkins部署一套動態的網站。敬請期待。shell
第一步:gitlab的安裝即配置vim
請參考我以前的博客《Gitlab在linux/windows中免密使用(二)--技術流ken》windows
第二步:建立項目bash
以下圖,我建立了一個static_web的項目運維
第一步:建立目錄並下載倉庫
[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://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.
第一步:進入到上面下載下來的工做目錄中
[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
第一步:文件提交至倉庫
[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 "you@example.com" 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 "you@example.com" [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://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 http://10.220.5.137/webg1/static_web.git c47e814..e2ac703 master -> master
第四步:web端查看結果
發現v1版已經上傳成功
通過上面的一些操做以後,咱們已經完成了靜態網站的部署,已經代碼的上傳
可是發現仍是每次執行須要輸入命令等
如今咱們就使用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://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 'http://10.220.5.137': root Password for 'http://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 http://10.220.5.137/webg1/static_web.git e2ac703..e5f5d42 master -> master
第三步:jenkins持續化部署
點擊構建
發現沒有報錯
第四步:網站查看
發現網站數據已經更新
轉載於:https://www.cnblogs.com/kenken2018/p/10007959.html