前言
gitlab和jenkins的部署和應用在前面的博文都解釋過了,
自行參考:
持續集成之gitlab部署與應用
持續集成之jenkins部署與關聯gitlab
這一篇以代碼上線爲主,沒有過多理論,但前提是gitlab,jenkins都要部署好,做好關聯,前面博文裏也有。
實驗環境:
設備 | IP | 目的 |
---|---|---|
Gitlab,Jenkins服務器 | 192.168.10.4 | gitlab代碼上傳,Jenkins觸發自動上線到web網站 |
nginx | 192.168.10.1 | 做web網站 |
部署nginx即要上線代碼的網站
1.關閉防火牆和selinux
2.準備官方yum源,部署nginx
3.yum安裝nginx
yum -y install nginx
已經更新到了1.18.0版本
4.開啓服務就好
systemctl start nginx
systemctl enable nginx
放置一遍,現在不用它了
在Gitlab服務器上傳代碼
前提:gitlab部署,創建項目,組,用戶略,參考gitlab篇
本地git部署與操作參考git篇
1.在本地git倉庫下載gitee(碼雲)上的靜態網頁代碼模板
git clone https://gitee.com/kangjie1209/monitor.git
複製monitor
裏面所有的內容到/git
目錄下
mv /monitor/* /git
[root@localhost git]# ls #下載的所有的代碼
404.html form-components.html messages.html
alerts.html form-elements.html
assets form-examples.html mstp_105_SuperAdmin.iml
buttons.html form-validation.html mstp_map.html
calendar.html images-icons.html other-components.html
charts.html img profile-page.html
components.html index.html QHME.iml
content-widgets.html js readme.md
css keyInfo.html
deviceManager.html labels.html real-time.html
dianfei.html LICENSE sa.html
efficiencyAnalysis.html list-view.html tables.html
energy_consumption.html login.html typography.html
file-manager.html media userMng.html
fonts media.html
2.代碼上傳到gitlab服務器
git remote add tester [email protected]:Vitamin/tester.git
git add --all
git commit -m 'add 111'
git push -u tester master
在gitlab查看上傳情況
Jenkins實現腳本上傳網站代碼
前提:登錄jenkins,新建自由項目web
new item – create – freestyle – configure – 源代碼庫 – git – gitlab路徑及私鑰認證 – save ——build now:手動同步測試
達到上一篇Jenkins部署與關聯的最後的效果
具體參考Jenkins篇
1.在jenkins服務器編寫上傳nginx的腳本
[root@localhost git]# cat /root/web.sh
#!/bin/sh
CODE_DIR=/var/lib/jenkins/workspace/web/
WEB_DIR=/usr/share/nginx
IP=192.168.10.1
TIME=`date +%F-%H-%M-%S`
cd $CODE_DIR && tar zcf /tmp/web-${TIME}.tar.gz ./*
scp /tmp/web-${TIME}.tar.gz $IP:$WEB_DIR
ssh root@$IP "cd $WEB_DIR && mkdir web-$TIME"
ssh root@$IP "cd $WEB_DIR && tar xf web-${TIME}.tar.gz -C web-$TIME && rm -rf web-${TIME}.tar.gz"
ssh root@$IP "cd $WEB_DIR && rm -rf html && ln -s web-$TIME html"
2.因爲腳本里需要遠程連接nginx服務器,所以提前與nginx服務器建立密鑰對連接
[root@localhost git]# ssh-copy-id [email protected]
The authenticity of host '192.168.10.1 (192.168.10.1)' can't be established.
ECDSA key fingerprint is 20:d3:c8:52:13:41:61:d8:35:4d:dc:e8:96:92:46:b4.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
3.配置構建關聯腳本,實現自動上傳
項目web – configure – build – Execute shell – 添加sh /root/web.sh ——應用保存
4.由於gitlab已經上傳好了腳本也和Jenkins做好了關聯,所以在Jenkins端直接手動構建項目;然後訪問http://192.168.10.1
此時,在nginx服務器網站根目錄已經發生了改變
訪問
解決構建後執行腳本報錯沒有權限問題
原因:Jenkins在安裝時配置文件默認Jenkins用戶是Jenkins,只要修改成root,他就會使用本機root身份執行,否則執行腳本就會沒有權限
解決辦法一:
(1)修改jenkins登錄shell,把/bin/false
改爲/bin/bash
vim /etc/passwd
修改:
jenkins:x:990:985:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
保存退出
(2)修改jenkins服務器的本地profile
vim /root/.bash_profile
添加:
export PS1='[\u@\h \W]\$'
保存退出
source /root/.bash_profile
(3)賦予jenkins用戶sudo權限
isudo
添加:
jenkins ALL=(ALL) NOPASSWD: ALL
保存退出
方法二:
直接修改Jenkins配置文件,把
JENKINS_USER="Jenkins"
改爲 JENKINS_USER="root"
重啓服務即可
jenkins關聯gitlab,實現自動構建上傳代碼
實現自動構建的方法有很多,上一篇說過了定時觸發,現在是隻要gilab上傳代碼,Jenkins就會觸發構建並上傳nginx網頁根目錄
1.Jenkins服務器操作
web項目-- configure – build triggers – 勾選最長選項 – 點擊高級advanced
– 選擇Filter branches by name – 點擊generate生成令牌 – 複製令牌和觸發器頂部url路徑-- save保存
2.gitlab操作
把生成的口令和url路徑給gitlab,用與兩者之間的連接
web項目 – settings – integrations – 把複製的令牌和url粘貼過來 – add webhook
(3)模擬企業更新push代碼,測試自動構建上傳。
vim index.html
git add --all
git commit -m 'add 333'
git push -u tester master
此時,你只需要盯着Jenkins看有沒有新項目構建就🆗了
再次訪問nginx,標題後也多了333
至此,自動上線代碼就🆗了