基於gitlab和jenkins的自動化部署例項
阿新 • • 發佈:2019-02-19
gitlab+jenkins服務簡述:
GitLab是一個程式碼倉庫,用來管理程式碼。Jenkins是一個自動化伺服器,可以執行各種自動化構建、測試或部署任務。
所以這兩者結合起來,就可以實現開發者提交程式碼到GitLab,Jenkins以一定頻率自動執行測試、構建和部署的任務,
幫組開發團隊更高效的整合和釋出程式碼。
實驗環境:
紅帽7.3版本虛擬機器
gitlab的安裝配置,官網給的配置最低要求為4G,使用虛擬機器配置的話結合物理機的配置要求分配記憶體:
[root@test1 ~]# yum install curl policycoreutils openssh-server openssh-clients postfix 安裝依賴性
[root@test1 ~]# systemctl status postfix.service 檢視服務狀態確保開啟
[root@test1 ~]# yum install -y net-tools 安裝工具包
[root@test1 ~]# ls
gitlab-ce-11.2.0-ce.0.el7.x86_64.rpm
[root@test1 ~]# rpm -ivh gitlab-ce-11.2.0-ce.0.el7.x86_64.rpm 安裝gitlab服務
安裝過程比較慢因為是跟我們分配記憶體大小相關,安裝成功會出來gitlab的圖案:
[root@test1 ~]# vim /etc/gitlab/gitlab.rb 編輯配置檔案寫入當前主機IP,修改gitlab倉庫路徑
[root@test1 ~]# gitlab-ctl reconfigure 初始化gitlab
[root@test1 ~]# gitlab-ctl restart 過載服務
在網頁訪問輸入密碼大於八位即可:
設定使用者密碼:
成功登陸:
我們可以建立一個新工程:
建立工程成功:
配置免密連線:
[root@foundation38 Desktop]# systemctl restart httpd 重啟httpd服務
[root@foundation38 Desktop]# cd
[root@foundation38 ~]# cd .ssh/
[root@foundation38 .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@foundation38 .ssh]# cat id_rsa.pub 檢視公鑰
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNeD0hcnCreyvvLHo5fMcQ4MszPyzAihqHQa9plB7Hxn+Qau4SSo8h2jJbhIDtvGfQdrCcB7823+skP6QFrfhUWBUbwZ7UjleSbumA3YhHK/ooCEzDx1kQY9pXxOEIp7jZ4PWfOhvINyFC1RH/kPC7Tx7697lq3mcWQCRS1wArF3vg3AHsNDtswwiYLitHkiZDlUVBBBnwH4GM1xe4YKtQXDOoqjnfxoq2LHFh8JKb/92NvqzYr1w+E8ps/jx+AoeUnv9pU4qtHierA3B/DQawI+wH3ChABZAjmr7zONbdINj1QJIjFmjDOgXQIc2eyYTFJb7/qaCwvm+Cwm6SVitx root@foundation38.ilt.example.com
配置gitlab的免密連線:
[root@foundation38 .ssh]# git clone [email protected]:root/demo.git 進行克隆
Cloning into 'demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@foundation38 .ssh]# cd demo/
[root@foundation38 demo]# ls
README.md
[root@foundation38 demo]# cd .git/
[root@foundation38 .git]# ls
branches description hooks info objects refs
config HEAD index logs packed-refs
[root@foundation38 .git]# git remote -v
origin git@172.25.38.11:root/demo.git (fetch)
origin git@172.25.38.11:root/demo.git (push)
[root@foundation38 .git]# cd ..
[root@foundation38 demo]# ls
README.md
[root@foundation38 demo]# vim README.md
[root@foundation38 demo]# cat README.md
# demo
# helloworld
[root@foundation38 demo]# git add README.md 提交到暫存區
[root@foundation38 demo]# git commit -m "add README.md" 直接提交
[master e4b9560] add README.md
1 file changed, 1 insertion(+)
[root@foundation38 demo]# git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.25.38.11:root/demo.git
c11a0b1..e4b9560 master -> master
在gitlab介面可以看到實時資料:
重新開啟一臺虛擬機器配置jenkins:
[root@localhost ~]# ls
[root@localhost ~]# route -n 檢視閘道器
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0
[root@localhost ~]# ip route add default via 172.25.38.250 新增閘道器
[root@localhost ~]# route -n 檢視閘道器
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.38.250 0.0.0.0 UG 0 0 0 eth0
172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0
[root@localhost ~]# vim /etc/resolv.conf 配置dns
[root@localhost ~]# cat /etc/resolv.conf
nameserver 114.114.114.114
[root@foundation38 Desktop]# iptables -t nat -I POSTROUTING -s 172.25.38.0/24 -j MASQUERADE 在真機新增策略
[root@localhost ~]# ping www.baidu.com 保證虛擬機器可以ping通百度(上網即可)
jenkins的安裝配置:
[root@localhost ~]# ls
jdk-8u171-linux-x64.rpm jenkins-2.121.1-1.1.noarch.rpm
[root@localhost ~]# yum install * -y
[root@localhost ~]# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@localhost ~]# /etc/init.d/jenkins start 開啟服務
Starting jenkins (via systemctl): [ OK ]
[root@localhost ~]# yum install net-tools 安裝工具包
[root@localhost ~]# netstat -antlp 檢視埠8080開啟
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 905/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1193/master
tcp 0 0 172.25.38.12:22 172.25.38.250:55754 ESTABLISHED 11819/sshd: root@pt
tcp6 0 0 :::8080 :::* LISTEN 12175/java
tcp6 0 0 :::22 :::* LISTEN 905/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1193/master
在網頁訪問jenkins:
根據提示在虛擬機器獲取初始化密碼:
[root@localhost ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
bdb371f17f07452eb5d935d7ab6d4f16
複製貼上上去進行下一步:
安裝建議安裝外掛:
這裡要保證所有外掛安裝成功,保證網路有失敗的再次安裝即可:
建立Admin使用者:
儲存完成:
開始使用jenkins:
進入jenkins介面:
在本臺虛擬機器安裝git:
[root@localhost ~]# hostnamectl set-hostname test2 更改主機名字
[root@localhost ~]# exit
logout
Connection to 172.25.38.12 closed.
[kiosk@foundation38 images]$ ssh root@172.25.38.12
root@172.25.38.12's password:
Last login: Fri Aug 24 13:30:58 2018 from 172.25.38.250
[root@test2 ~]# yum install -y git 安裝git
設定瀏覽器網頁為中文的設定:
檢視私鑰:
[root@foundation38 ~]# cd /root/.ssh/
[root@foundation38 .ssh]# ls
authorized_keys demo id_rsa id_rsa.pub known_hosts
[root@foundation38 .ssh]# cat id_rsa 全部複製到網頁即可
設定免密儲存即可:
[root@foundation38 demo]# pwd
/home/kiosk/demo
[root@foundation38 demo]# ls
aa.txt readme.md test.txt
[root@foundation38 demo]# vim test.txt
[root@foundation38 demo]# cat test.txt
test
[root@foundation38 demo]# git add test.txt 提交資訊
[root@foundation38 demo]# git commit -m "add test.txt"
[master 02cadf8] add test.txt
1 file changed, 1 deletion(-)
[root@foundation38 demo]# git push origin master 推送資訊
Username for 'https://github.com': xuefeilong
Password for 'https://[email protected]':
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 248 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/xuefeilong/test.git
acbf46a..02cadf8 master -> master
可以看到有推送資訊標誌:
檢視控制檯輸出可以看到提交的資訊:
安裝gitlab外掛:
構建觸發器:
[root@test2 ~]# curl -X PUT --header "PRIVATE-TOKEN: JsL4B7yUKz4Qz8W2_GMy" 'http://172.25.38.11/api/v4/application/settings?allow_local_requests_from_hooks_and_services=true'
[root@foundation38 demo]# vim hello.txt
[root@foundation38 demo]# cat hello.txt
hello
hello
hello
hello
hello
[root@foundation38 demo]# git add hello.txt
[root@foundation38 demo]# git commit -m "add hello.txt"
[master 80d86bd] add hello.txt
1 file changed, 5 insertions(+)
create mode 100644 hello.txt
[root@foundation38 demo]# git push origin master
Username for 'https://github.com': xuefeilong
Password for 'https://[email protected]':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 334 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xuefeilong/test.git
f11ce30..80d86bd master -> master
在jenkins網頁可以看到提交資訊: