一、安裝配置gitlab服務,在gitlab新建一個倉庫,配置本地密鑰,並經過ssh方式拉取gitlab倉庫代碼php
https://packages.gitlab.com/gitlab/gitlab-ce?page=1 gitlab包下載地址html
wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-11.3.4-ce.0.el7.x86_64.rpm/download.rpmjava
修改配置文件,並啓動服務node
root@gitlab:~# grep "^[a-Z]" /etc/gitlab/gitlab.rbmysql
external_url 'http://192.168.1.196.3'linux
#可選郵件通知設置nginx
gitlab_rails['smtp_enable'] = truec++
gitlab_rails['smtp_address'] = "smtp.qq.com"git
gitlab_rails['smtp_port'] = 465web
gitlab_rails['smtp_user_name'] = "961161266@qq.com"
gitlab_rails['smtp_password'] = "受權碼"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = :login
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "961161266@qq.com"
user["git_user_email"] = "961161266@qq.com"
執行配置並啓動服務:
gitlab-ctl reconfigure #修改完配置文件要執行此操做
# gitlab-rails #用於啓動控制檯進行特殊操做,好比修改管理員密碼、打開數據庫控制
臺( gitlab-rails dbconsole)等
# gitlab-psql #數據庫命令行
root@s1:~# gitlab-psql
psql (9.6.11)
Type "help" for help.
gitlabhq_production=# \db
List of tablespaces
Name | Owner | Location
------------+-------------+----------
pg_default | gitlab-psql |
pg_global | gitlab-psql |
(2 rows)
# gitlab-rake #數據備份恢復等數據操做
# gitlab-ctl #客戶端命令行操做行
# gitlab-ctl stop #中止 gitlab
# gitlab-ctl start #啓動 gitlab
# gitlab-ctl restar #重啓 gitlab
# gitlab-ctl status #查看組件運行狀態
# gitlab-ctl tail nginx #查看某個組件的日誌
設置密碼。
默認用戶爲root,口令沒有第一次登錄時須要設置口令
使用管理員 root 建立組,一個組裏面能夠有多個項目分支,能夠將開發添加到組裏
面進行設置權限,不一樣的組就是公司不一樣的開發項目或者服務模塊,不一樣的組添加不
同的開發便可實現對開發設置權限的管理。
git 客戶端測試 clone 項目:
編輯文件並測試提交:root@jenkins:/source
# cd test-project/
# git config --global user.name #配置一個用戶名
# git config --global user.email 2973707860@qq.com #配置一個郵箱
# vim index.html
cat index.html
11111111111
22222222222
# git add index.html #添加至本地
# git commit -m "v1"#將代碼打上標籤
git config --global user.name 「name「 #設置全局用戶名
git config --global user.email xxx@xx.com #設置全局郵箱
git config --global --list #列出用戶全局設置
git add index.html / . #添加指定文件、目錄或當前目錄下全部數據到暫存區
git commit -m 「11「 #提交文件到工做區
git status #查看工做區的狀態
git push #提交代碼到服務器
git pull #獲取代碼到本地
git log #查看操做日誌
vim .gitignore #定義忽略文件
git reset --hard HEAD^^ #git 版本回滾, HEAD 爲當前版本,加一個^爲上一個,^^爲上上一個版本
git reflog # #獲取每次提交的 ID,可使用--hard 根據提交的 ID 進行版本回退
git reset --hard 5ae4b06 #回退到指定 id 的版本
# git branch #查看當前所處的分支
#git checkout -b develop #建立並切換到一個新分支
二、安裝配置jenkins,並實現jenkins和gitlab集成,實現jenkins自動化更新回滾
配置 java 環境並部署 jenkins:
https://jenkins.io/zh/download/下載地址
vim /etc/sysconfig/jenkins #修改配置文件,爲了後面的權限設置問題,將用戶改成root
JENKINS_USER="root"
systemctl start jenkins #啓動服務
tail -f /var/log/jenkins/jenkins.log
查看日誌,在第一次啓動jenkins時,會生成一串祕鑰。後面安裝時須要提供
/var/lib/jenkins/secrets/initialAdminPassword #在此文件中也會生成字符串
4.1.11:jenkins 插件管理及安裝:
4.1.11.1:插件安裝目錄:
插件下載地址:http://updates.jenkins-ci.org/download/plugins/
4.1.12:配置 jenkins 權限管理:
基於角色的權限管理,先建立角色和用戶,給角色受權,而後把用戶管理到角色。
4.1.12.3:更改認證方式:
Jenkins—系統管理—全局安全配置
默認建立的用戶登陸後能夠作任何操做,取決於默認的認證受權方式。
在系統配置中能夠添加郵箱認證
新建自由風格項目
在憑據中添加私鑰,用於連接gitlab倉庫
在項目中能夠添加gitlab倉庫。使用ssh祕鑰認證
啓動tomcat 使用jenkins自動部署代碼至tomcat
yum install java-1.8.0-openjdk-devel #安裝devel版本,會自動解決其餘依賴關係
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.45/bin/apache-tomcat-8.5.45.tar.gz #下載tomcat二進制安裝包
tar xf apache-tomcat-8.5.45.tar.gz -C /usr/local/ #解壓至usr/local目錄中
ln -s apache-tomcat-8.5.45.tar.gz tomcat #建立軟鏈接方便之後修改
useradd tomcat #添加用戶,修改屬組 ,tomcat默認以普通身份運行,須要修改文件權限
chown -R .tomcat .
chmod g+r conf/*
chmod g+rx conf/
chown -R tomcat logs/ temp/ work/
vim /etc/profile.d/cols.sh #修改tomcat命令行配置。
PS1='[\e[32;40m\u@\h \W\e[m]$ '
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/tomcat/bin
更好一下tomcat中的appbase目錄
啓動catalina.sh start
配置gitlab倉庫地址,使用祕鑰認證連過去
使用shell執行命令
進入jenkins中。系統自帶目錄(默認建立項目時會建立根項目同名的目錄在/var/lib/jenkins/workspace中)
拉取代碼至本地。遠程中止tomcat服務。在將文件替換至目標目錄,以後啓動服務
輸出信息
添加slave節點
4.3:pipline:
官方介紹;https://jenkins.io/2.0/
pipline 是幫助 Jenkins 實現 CI 到 CD 轉變的重要角色,是運行在 jenkins 2.X 版本的核
心插件,簡單來講 Pipline 就是一套運行於 Jenkins 上的工做流框架,將本來獨立運行
於單個或者多個節點的任務鏈接起來,實現單個任務難以完成的複雜發佈流程,從而
實現單個任務很難實現的複雜流程編排和任務可視化,Pipeline 的實現方式是一套
Groovy DSL,任何發佈流程均可以表述爲一段 Groovy 腳本。
4.3.2:pipline 語法:
Stage:階段,一個 pipline 能夠劃分爲若干個 stage,每一個 stage 都是一個操做,好比
clone 代碼、代碼編譯、代碼測試和代碼部署,階段是一個邏輯分組,能夠跨多個 node
執行。
Node:節點,每一個 node 都是一個 jenkins 節點,能夠是 jenkins master 也能夠是
jenkins agent,node 是執行 step 的具體服務器。
Step:步驟,step 是 jenkins pipline 最基本的操做單元,從在服務器建立目錄到構建容
4.3.2.2:測試簡單 pipline job 運行:
node {
stage("clone 代碼"){
echo "代碼 clone"
}
stage("代碼構建"){
echo "代碼構建"
}
stage("代碼測試"){
echo "代碼測試"
}
stage("代碼部署"){
echo "代碼部署"
}
}
5.1:代碼測試工具 SonarQube:
官方網站:http://www.sonarqube.org/
SonarQube 是一個用於代碼質量管理的開放平臺,經過插件機制,SonarQube能夠
集成不一樣的測試工具,代碼分析工具,以及持續集成工具。與持續集成工具(例如
Hudson/Jenkins 等)不一樣,SonarQube 並非簡單地把不一樣的代碼檢查工具結果(例
如 FindBugs,PMD 等)直接顯示在 Web 頁面上,而是經過不一樣的插件對這些結果進行再
加工處理,經過量化的方式度量代碼質量的變化,從而能夠方便地對不一樣規模和種
類的工程進行代碼質量管理。在對其餘工具的支持方面,Sonar不只提供了對 IDE的支
持,能夠在 Eclipse 和 IntelliJ IDEA 這些工具裏聯機查看結果;同時 Sonar 還對大量的持
續集成工具提供了接口支持,能夠很方便地在持續集成中使用 SonarQube,此外,
SonarQube 的插件還能夠對 Java之外的其餘編程語言提供支持,對國際化以及報告
文檔化也有良好的支持。
5.2.1:編輯配置文件:
https://www.sonarqube.org/downloads/下載地址
sonar 依賴於 java 環境,並且 java 版本必須是 1.8 版本或更高,不然 sonar 啓動失敗
6.7.X 版本的 sonar 須要調用 elasticsearch,並且默認須要使用普通用戶啓動
$ unzip sonarqube-6.7.6.zip
$ln -sv /usr/local/src/sonarqube-6.7.6 /usr/local/sonarqube
$grep "^[za-Z]" /usr/local/sonarqube/conf/sonar.properties
[root@s1 local]# grep "^[za-Z]" /usr/local/sonarqube/conf/sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://192.168.7.106:3306/sonar?useUnicode=true&characterEncodi
ng=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.web.host=0.0.0.0
sonar.web.port=9000
5.2.2:啓動 sonarqube:
# /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
默認登陸名及密碼爲admin
5.1.2:Mysql 數據庫建立及受權:
#yum install vim gcc gcc-c++ wget autoconf net-tools lrzsz iotop lsof iotop bash-completion
curl policycoreutils openssh-server openssh-clients postfix -y
#cd mysql-5.6.42-linux-glibc2.12-x86_64
#ln -sv /usr/local/src/mysql-5.6.42-linux-glibc2.12-x86_64 /usr/local/mysql
#useradd mysql -s /sbin/nologin
#chown mysql.mysql /usr/local/mysql/ -R
#/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --
basedir=/usr/local/mysql/
#cp /usr/local/src/mysql-5.6.42-linux-glibc2.12-x86_64/support-files/mysql.server
/etc/init.d/mysqld
#cp my.cnf /etc/my.cnf
#chmod a+x /etc/init.d/mysqld
#/etc/init.d/mysqld start
#ln -sv /usr/local/mysql/bin/* /usr/bin/
#mkdir /var/lib/mysql
#ln -sv /data/mysql/mysql.sock /var/lib/mysql/
5.3:部署掃描器 sonar-scanner:
5.3.1:部署 sonar-scanner:
sonarqube 經過調用掃描器 sonar-scanner 進行代碼質量分析,即掃描器的具體工做就
是掃描代碼:
#下載地址:http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
# unzip sonar-scanner-2.6.1.zip
# ln -sv /usr/local/src/sonar-scanner-2.6.1 /usr/local/sonar-scanner
# cd /usr/local/sonar-scanner/
# grep "^[a-Z]" conf/sonar-scanner.properties
sonar.host.url=http://localhost:8800
sonar.sourceEncoding=UTF-8
sonar.jdbc.username=sonar
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:mysql://192.168.1.195:3306/sonar?useUnicode=true&character
Encoding=utf8
5.3.4:jenkins 關聯到 SonarQube :
首先安裝插件,在 jenkins 插件安裝界面安裝 Sonar 插件 SonarQubePlugin
其次配置 SonarQube server,系統管理-系統設置
5.3.5:讓 jenkins 關聯到 Sonarscanner
添加掃描器:
系統管理-Global Tool Configuration
5.3.6:配置掃描:
選擇本身的項目(demo)-構建-execute sonarqube scanner,將配置文件的內容修改爲如
下格式填寫完成後點保存:
sonar.projectKey=test-demo1
sonar.projectName=test-demo1
sonar.projectVersion=1.0
sonar.sources=./
sonar.language=php
sonar.sourceEncoding=UTF-8
5.3.6:配置掃描:
選擇本身的項目(demo)-構建-execute sonarqube scanner,將配置文件的內容修改爲如
下格式填寫完成後點保存:
sonar.projectKey=test-demo1
sonar.projectName=test-demo1
sonar.projectVersion=1.0
sonar.sources=./
sonar.language=php
sonar.sourceEncoding=UTF-8
使用腳本自動更新,須要傳遞參數,
#!/bin/bash
DATE=`date +%Y-%m-%d_%H-%M-%S`
GROUP_LIST=$2
function IP_list(){
if [[ ${GROUP_LIST} == "online-group1" ]];then
Server_IP="192.168.8.4"
echo ${Server_IP}
elif [[ ${GROUP_LIST} == "online-group2" ]];then
Server_IP="192.168.8.5"
ssh root@192.168.8.2 ""echo enable server web_port/192.168.8.4" | socat stdio /var/lib/haproxy/haproxy.sock"
ssh root@192.168.8.6 ""echo enable server web_port/192.168.8.5" | socat stdio /var/lib/haproxy/haproxy.sock"
echo ${Server_IP}
elif [[ ${GROUP_LIST} == "online-all" ]];then
Server_IP="192.168.8.4 192.168.8.5"
echo ${Server_IP}
fi
}
#function pull_code(){
# rm -rf /home/tomcat/.jenkins/workspace/testweb
# cd /home/tomcat/.jenkins/workspace
# git clone git@192.168.7.202:testgroup/testweb.git
#}
function make_zip(){
cd /var/lib/jenkins/workspace/test-demo1 && zip testweb.war.zip ./*
}
function down_node(){
for node in ${Server_IP};do
echo ${node}
ssh root@192.168.8.2 ""echo disable server web_port/${node}" | socat stdio /var/lib/haproxy/haproxy.sock"
ssh root@192.168.8.6 ""echo disable server web_port/${node}" | socat stdio /var/lib/haproxy/haproxy.sock"
echo "${node} 關閉成功!"
done
}
function stop_tomcat(){
for node in ${Server_IP};do
ssh tomcat@${node} "/etc/init.d/tomcat stop"
echo "${node} 關閉成功!"
done
}
function start_tomcat(){
for node in ${Server_IP};do
ssh tomcat@${node} "/etc/init.d/tomcat start"
echo "${node} 關閉成功!"
done
}
function scp_codefile(){
cd /var/lib/jenkins/workspace/test-demo1
for node in ${Server_IP};do
scp testweb.war.zip tomcat@${node}:/apps/tomcat_appdir
ssh tomcat@${node} "cd /apps/tomcat_appdir && unzip testweb.war.zip -d /apps/tomcat_webdir/testweb-${DATE} && rm -rf /data/tomcat_webdir/testweb && ln -sv /apps/tomcat_webdir/testweb-${DATE} /data/tomcat_webdir/testweb"
done
}
function web_test(){
#sleep 30
for node in ${Server_IP};do
NUM=`curl -s -I -m 10 -o /dev/null -w %{http_code} http://${node}:8080/testweb/index.html`
if [[ ${NUM} -eq 200 ]];then
echo "${node} 測試經過,即將添加到負載"
add_node ${node}
else
echo "${node} 測試失敗,請檢查該服務器是否成功啓動tomcat"
fi
done
}
function add_node(){
node=$1
echo ${node},"----->"
if [ ${node} == "192.168.8.100" ];then
echo "192.168.8.100部署完畢,請進行代碼測試!"
else
ssh root@192.168.8.2 ""echo enable server web_port/${node}" | socat stdio /var/lib/haproxy/haproxy.sock"
ssh root@192.168.8.6 ""echo enable server web_port/${node}" | socat stdio /var/lib/haproxy/haproxy.sock"
fi
}
function rollback_last_version(){
for node in ${Server_IP};do
NOW_VERSION=`ssh tomcat@${node} ""/bin/ls -l -rt /data/tomcat_webdir/ | awk -F"->" '{print $2}' | tail -n1 | awk -F"/apps/tomcat_webdir/" '{print $2}'""`
NAME=`ssh tomcat@${node} ""/bin/ls -l -rt -d /apps/tomcat_webdir/testweb* | grep -B 1 ${NOW_VERSION} | head -n1 | awk '{print $9}'""`
ssh tomcat@${node} "rm -rf /data/tomcat_webdir/testweb && ln -sv ${NAME} /data/tomcat_webdir/testweb"
done
}
main(){
case $1 in
deploy)
IP_list;
#pull_code;
make_zip;
down_node;
stop_tomcat;
scp_codefile;
start_tomcat;
web_test;
;;
rollback_last_version)
IP_list;
echo ${Server_IP}
down_node;
stop_tomcat;
rollback_last_version;
start_tomcat;
web_test;
;;
esac
}
main $1 $2
#須要自建兩個參數