gitlab+jenkins+nginx持續集成


環境

centos7.2  2CPU 內存4G

gitlab:192.168.186.132  

Jenkins:centos7.2 192.168.186.133 

nginx:centos7.2 192.168.186.134

 

 

配置yum源

mkdir -p /etc/yum.repos.d/old

mv /etc/yum.repos.d/* old

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

 

關閉selinux並確定是出於關閉狀態

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled’ /etc/selinux/config`

 

或者臨時關閉

setenforce 0

 

gitlab安裝


安裝依賴包
yum install curl policycoreutils openssh-server openssh-clients postfix -y

 

啟動postfix

systemctl start postfix

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | bash

[root@node1 ~]# vim /etc/yum.repos.d/gitlab-ce.repo [gitlab-ce] name=gitlab-ce baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7 repo_gpgcheck=0 gpgcheck=0 enabled=1 gpgkey=https://packages.gitlab.com/gpg.key [root@node1 ~]# yum makecache [root@node1 ~]# yum install -y gitlab-ce

在安裝一個git客戶端
yum -y install git
配置並啟動gitlab-ce
[root@node1 ~]# gitlab-ctl reconfigure
##時間可能比較長,請你耐心等待即可!~~~
##關閉:gitlab-ctl stop
##啟動:gitlab-ctl start
##重啟:gitlab-ctl restart
可以使用gitlab-ctl管理查看gitlab 例如查看gitlab狀態:

 

默認安裝的gitlab,主要有四個目錄: 
/opt/gitlab/ ## 主目錄 
/etc/gitlab/ ## 放置配置文件 
/var/opt/gitlab/ ## 各個組件 
/var/log/gitlab/ ## 放置日志文件

檢查gitlab各組件狀態: 
gitlab-ctl status

重啟gitlab: 
gitlab-ctl restart

只重啟某個組件: 
gitlab-ctl restart nginx

 

git常用命令

查看遠程分支 

git branch -a    ##remotes 表示遠程分支

 

 

查看本地分支

git branch 

 

 

強制更新分支信息

git fetch origin --prune

 

Clone指定分支,並且下載到指定目錄下

git clone -b 分支名 git路徑 "指定目錄"

 

 

切換分支

當前在test分支下切換到master

 

 

git checkout master

 

 

創建遠程分支
#本地開發我們可能會創建一個新的分支(遠程並沒有此分支)
#本地
git checkout -b f-category
#然后mkdir a.txt
git add a.txt
#此時 git push origin,會出錯是因為遠程沒有此分支。
git push origin f-category #將此分支推到遠程,並在遠程創建分支(或者 f-category:dev方式推送到遠程分支)

 

 

 確保80端口沒有被占用,被占用的話,建議修改gitlab端口或者使用docker容器搭建gitlab
訪問192.168.186.132 會提示讓設置一個密碼,設置的密碼自己記住就可以.至少8位數

 

 

進入登陸界面 輸入用戶名及密碼 用戶root 密碼為剛才設置的密碼(我這里因為試裝了中文漢化版,可參考https://www.cnblogs.com/straycats/p/7637373.html

 

 

 

 

創建新項目

 

 

在創建好的項目里面,點擊README順便輸入點文字

 

 

 

 

 

 

為了保證安全取消用戶能夠自己注冊 (在下面找到seve保存)

 

 

 

 

現在登陸就能發現沒有注冊了
0創建ssh免秘鑰登陸 模擬使用gitlab這台服務器當做客戶端
ssh-keygen -t rsa -C “你的郵箱”回三次車,密碼為空

 

 

 

 

[root@test-1 ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMV06cQt2fVDwhJ8IocyRDIkgXsvNiaLJW7+4HHF7yMSYf6i2LORK+IEp+gzPO010slfeupw1Kd0QKE993eXMjz5h1x6rm3f/SH7+s/86gi/1vCcx4l1by2nOutixomf4dd3JvnZwVEIXI9HOVCqLRJxn6XnUWzvZsz5tO+8tlPNPxKQ6NZ+gMXcCqUJPQfR2D2JbImfrWSEA5UFC17S/HB7v2GJ7+gitMmFXdHNE0ozu3/SFwmEOQ8LuMKcK0tpG3vDqfKEas17bQs6TDu9nwwQ4kF7540RLLHZ7XTqElnsLz2Ek/mjS+mXna5pkiU8uorXSx66haWL6ij9d4ySab *************@139.com

 

把公鑰粘貼到gitlab用戶當中

 

 

 

 

 

創建訪問令牌用於后面Jenkins拉取代碼

 

 

 

 

 修改gitlab的拉取地址為主機IP
vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

 

 

 

 重啟gitlab-ce 
[root@node1 ~]# gitlab-ctl restart

 

 

 

拉取gitlab devops項目

 

 

 

 

創建一個index.html用來模擬開發寫代碼並提交
[root@node1 test]# pwd
/root/devops
[root@node1 test]# cat index.html 
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>devops 測試</h1> </body> </html>
#cd devops/
#git add .
#git commit --m "add index.html"
#git push

 

 

###參數說明
[root@node1 test]# git add *    ##提交所有更改信息(刪除 增加文件)
[root@node1 test]# git config --global user.email "******@139.com"   ##身份驗證
[root@node1 test]# git config --global user.name "root"             ##身份驗證
[root@node1 test]# git commit -m "HEAD"         ## 提交所有變更的文件

[root@node1 test]#git push origin master

 

 

 驗證查看gitlab上是否有提交剛剛創建的文件

 

 

以上就是gitlab搭建過程以及拉取、上傳驗證

 

 jenkins安裝

Jenkins是Java編寫的,所以需要先安裝JDK,這里采用yum安裝,如果對版本有需求,可以直接在Oracle官網下載JDK。

 

配置yum源

mkdir -p /etc/yum.repos.d/old

mv /etc/yum.repos.d/* old

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@node2 ~]# yum -y install java-1.8.0 [root@node2 ~]# yum -y install wget git [root@node2 ~]# cd /etc/yum.repos.d/ [root@node2 yum.repos.d]# wget http://pkg.jenkins.io/redhat/jenkins.repo [root@node2 yum.repos.d]#rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key [root@node2 yum.repos.d]# yum install -y jenkins [root@node2 ~]# systemctl start jenkins

[root@node2 ~]# systemctl enable jenkins
訪問jenkins 192.168.186.133:8080 (如端口有沖突也可以在配置文件進行修改)
[root@node2 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword ##查看密碼

 

 

第二種方法

直接下載 rpm 安裝

各個版本地址 https://pkg.jenkins.io/

wget https://pkg.jenkins.io/redhat/jenkins-2.156-1.1.noarch.rpm
rpm -ivh jenkins-2.156-1.1.noarch.rpm

配置
vim /etc/sysconfig/jenkins
 #監聽端口 JENKINS_PORT="8080"

配置權限

為了不因為權限出現各種問題,這里直接使用root

修改用戶為root

vim /etc/sysconfig/jenkins
 #修改配置 $JENKINS_USER="root"

修改目錄權限

chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins

重啟

service jenkins restart
ps -ef | grep jenkins
systemctl start jenkins

 

 

 

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

安裝兩個插件 gitlab-plugin gitlab hook-plugin Pipeline Maven Integration、Maven Integration、Publish Over SSH、Gitee 
安裝gitlab gitlab hook即可
要是找不到可以在插件地址下載然后在上傳 (這里我裝了很多插件,也可以按自己的需要安裝即可)

 

 

 系統設置,配置遠程應用服務器 

 

 

 

 

 

添加完主機之后,新建一個項目,開始配置構建操作

 

 

 

 

 

 因不想畫圖借用別人得圖(IP地址與用戶名不對應) 192.168.186.132 root

 

 

 

 

 

 

 

 

 

 

 

 另一種方法用shell也可以

 

 

配置gitlab,當有代碼提交時,觸發jenkins的部署操作

 

 

 

 

 

 

 

 

 

 

 

測試提交代碼是否會部署到遠程主機

git clone git@192.168.186.132:data/yht.git cd yht/ echo '測試Jenkins' > index.html git add . git commit -m 'add index.html' git push

 

 因不想打命令,這里我把上傳步驟寫在腳本里,直接執行

 

 

 

 

 

 

 

 

 

小坑:

  • 錯誤提示:

    #很多朋友使用最新版本的gitlab做自動部署時,在增加web鈎子那一步, #點擊test push events時會報錯:Url is blocked: Requests to the local network are not allowed

 

 

解決方法:

#這是因為新版的gitlab為了安全默認禁止了本地局域網地址調用web hook #我們在設置里允許就行,具體步驟如下:

 

 

 

 

 參考文檔:https://blog.51cto.com/bigboss/2129477






 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM