注意:本片配置:jenkins服務器和應用服務器不在同一臺機器上,且應用服務器 負責流執行程 (推薦)。 流程:代碼上傳->gitlab通知->jenkins服務器執行構建->遠程應用服務器git下拉->遠程應用服務器maven打包->發送到遠程應用服務器的tomcat->發佈html
如下是配置說明: 須要兩臺服務器:一臺部署jenkins服務器、另外一臺部署應用服務器。 要求:兩臺服務器能夠互相通訊。 部署jenkins服務器(Linux1): jdk、jenkins、tomcat(可選,但不推薦) 部署應用服務器(Linux2): jdk、git、maven、tomcatjava
說明一下:部署的jenkins服務器能夠是內網本地,應用服務器能夠是外網環境的雲服務器。Jenkins也是能夠實現手動或輪詢的方式構建到應用服務器的,但,不能使用gitlab通知jenkins自動構建,因,gitlab找不到你的內網地址。python
yum install java
複製代碼
前提環境:jdk1.8 下載地址:jenkins.war 穩定版linux
[root@VM_0_11_centos ~]# ls
jdk1.8 jdk-8u221-linux-x64.tar.gz jenkins.war
[root@VM_0_11_centos ~]# java -jar jenkins.war
複製代碼
訪問:ip:8080 http://49.235.71.163:8080 建立用戶:admin/admin 用戶名:zhengjagit
Safe Restart 方便重啓jenkins,每次配置jenkins須要重啓讓配置生效 Rebuilder 方便從新構建 Git Parameters 肯定構建哪一個分支github
1.去掉 容許用戶註冊 2.選用 安全矩陣 ->Add user or group... ->輸入剛建立的admin,賦予所有權限,保存web
環境: jdk1.8,git,maven,tomcat8spring
[root@VM_0_6_centos ~]# netstat -anp|grep :22
tcp 0 0 0.0.0.0:22 0.0.0.0:*
tcp 0 36 172.16.0.6:22 211.161.248.60:22447
[root@VM_0_6_centos ~]# service sshd start
Redirecting to /bin/systemctl start sshd.service
複製代碼
安裝iptables 若是你要改用iptables的話,須要安裝iptables服務:docker
#安裝
sudo yum install iptables-services
#開啓iptables
sudo systemctl enable iptables
sudo systemctl enable ip6tables
#啓動服務
sudo systemctl start iptables
sudo systemctl start ip6tables
複製代碼
關閉防火牆:apache
[root@VM_0_6_centos ~]# service iptables stop
Redirecting to /bin/systemctl stop iptables.service
[root@VM_0_6_centos ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled) Active: inactive (dead) since Thu 2019-09-26 21:28:33 CST; 6s ago Process: 9537 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS) Process: 9354 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS) Main PID: 9354 (code=exited, status=0/SUCCESS) Sep 26 21:27:28 VM_0_6_centos systemd[1]: Starting IPv4 firewall with iptables... Sep 26 21:27:28 VM_0_6_centos iptables.init[9354]: iptables: Applying firewall rules: [ OK ] Sep 26 21:27:28 VM_0_6_centos systemd[1]: Started IPv4 firewall with iptables. Sep 26 21:28:33 VM_0_6_centos systemd[1]: Stopping IPv4 firewall with iptables... Sep 26 21:28:33 VM_0_6_centos iptables.init[9537]: iptables: Setting chains to policy ACCEPT: filte... ] Sep 26 21:28:33 VM_0_6_centos iptables.init[9537]: iptables: Flushing firewall rules: [ OK ] Sep 26 21:28:33 VM_0_6_centos systemd[1]: Stopped IPv4 firewall with iptables. Hint: Some lines were ellipsized, use -l to show in full. [root@VM_0_6_centos ~]# 複製代碼
Stopped IPv4 firewall with iptables 說明防火牆已關閉
yum install java
java -version
複製代碼
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm
複製代碼
chmod +x jdk-8u131-linux-x64.rpm
複製代碼
rpm -ivh jdk-8u131-linux-x64.rpm
複製代碼
java -version
複製代碼
默認安裝路徑爲 /usr/java/jdk1.8.0_131
vim /etc/profile
複製代碼
環境變量爲
export JAVA_HOME=/usr/java/jdk1.8.0_131
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
複製代碼
source /etc/profile
複製代碼
javac
java -version
echo $PATH
複製代碼
下載:jdk-8u221-linux-x64.tar.gz 放到 /usr/local ,解壓
tar -zxvf jdk-8u221-linux-x64.tar.gz
複製代碼
更名 jdk1.8
mv jdk1.8.0_221 jdk1.8
複製代碼
[root@VM_0_6_centos local]# ls
bin etc games include jdk1.8 lib lib64 libexec qcloud sbin share src yd.socket.server
複製代碼
配置jdk1.8環境變量
[root@VM_0_6_centos jdk1.8]# pwd
/usr/local/jdk1.8
[root@VM_0_6_centos jdk1.8]# vi /etc/profile
複製代碼
環境變量爲
export JAVA_HOME=/usr/local/jdk1.8
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
複製代碼
退出文件,保存 按Esc鍵 輸入:wq 保存並退出 讓配置文件生效:
[root@VM_0_6_centos jdk1.8]# source /etc/profile
複製代碼
查看是否配置成功:
[root@VM_0_6_centos jdk1.8]# java -version
java version "1.8.0_221"
複製代碼
若出現jdk版本號,則安裝並配置環境變量成功 若是提示命令找不到的話,查看一下jdk的配置路徑是否錯誤。 刪除安裝包(可選):rm -rf jdk-8u221-linux-x64.tar.gz
[root@VM_0_6_centos local]# yum install git
[root@VM_0_6_centos local]# git --version
git version 1.8.3.1
複製代碼
一、獲取github最新的Git安裝包下載連接,下載新版本 git-2.9.5.tar.gz 上傳到linux; 二、壓縮包解壓:
sudo tar -zxvf git-2.9.5.tar.gz
複製代碼
三、進入解壓後的文件夾:
cd git-2.9.5
複製代碼
四、執行編譯:耐心等待編譯便可
make prefix=/usr/local/git all
複製代碼
五、安裝Git至/usr/local/git路徑,命令爲
make prefix=/usr/local/git install
複製代碼
六、打開環境變量配置文件,命令 ,在底部加上Git相關配置信息:
vim /etc/profile
複製代碼
PATH=$PATH:/usr/local/git/bin export PATH
而後wq保存,退出! 讓配置文件生效:
source /etc/profile
複製代碼
七、輸入命令 git --version ,查看安裝的git版本,校驗經過,安裝成功。 八、刪除安裝包(可選):rm -rf git-2.9.5.tar.gz 九、查看git安裝路徑: Linux-若是不清楚本身的Git安裝在哪一個路徑下,終端執行
whereis git
複製代碼
配置:/usr/local/git/bin/git
ssh公鑰能夠配置gitlab、github等, ssh私鑰可配置jenkins憑證等。 用戶和郵箱不要求真實
[root@VM_0_6_centos ~]# git config --global user.name "zhengja"
[root@VM_0_6_centos ~]# git config --global user.email "zhengja@dist.com"
[root@VM_0_6_centos ~]# ssh-keygen -t rsa -C "zhengja@dist.com"
Enter file in which to save the key (/root/.ssh/id_rsa):
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:EqKVJUBpxVYpswJifGiXyplLTUcBYFNaMxaODZQPUmc zhengja@dist.com
The key's randomart image is:
+---[RSA 2048]----+
|oOO&E++. |
|=B%BB=. |
|*=@+=+. |
| *.=.. . |
|. o. . S |
| . . |
| |
| |
| |
+----[SHA256]-----+
複製代碼
拿到公鑰配置gitlab
[root@VM_0_6_centos local]# cd /root/.ssh/
[root@VM_0_6_centos .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@VM_0_6_centos .ssh]# vim id_rsa.pub
複製代碼
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDcArp9fBpi7CWJ50dQQTHbLfRkMyhVuox7bFEeYu3kaFYO3ECInfiBEK8PdYAKPjGWRirYQGCBQlsSzGd4F3sVTQH4ePk0bk/iAwnZsgOBgMgyZW6c00u+afJ/h8PNRK2xr60kB3n4no4UDov4hsuGdCKhavrHRzs3s6H+b7WKxILdSwZ7BpduGZGCOug8JtcnwF1mmMKi0YH0KrIxnrOR9GHT+wHAyJ5WjYU4Xzr+ZIGN6MfrM7X99y/uyMsrv8nzliMxWpFxduYAkKkfrqf46Y18oZfn5VdXhfnvyd+FqWTPdtJF2EZtKdhwGnouuyTdESQP3PPacFM1ZeW58R/T zhengja@dist.com
測試:git命令克隆sshUrl到本地
git clone sshURL
複製代碼
下載 apache-maven-3.6.2-bin.zip ,上傳到linux系統並解壓
[root@VM_0_6_centos local]# ls
apache-maven-3.6.2-bin.zip etc include lib libexec sbin src
bin games jdk1.8 lib64 qcloud share yd.socket.server
[root@VM_0_6_centos local]# unzip apache-maven-3.6.2-bin.zip
[root@VM_0_6_centos local]# ls
apache-maven-3.6.2 bin games jdk1.8 lib64 qcloud share yd.socket.server
apache-maven-3.6.2-bin.zip etc include lib libexec sbin src
複製代碼
配置maven環境變量
[root@VM_0_6_centos apache-maven-3.6.2]# pwd
/usr/local/apache-maven-3.6.2
[root@VM_0_6_centos apache-maven-3.6.2]# vim /etc/profile
複製代碼
末尾添加(路徑要正確)
export MAVEN_HOME=/usr/local/apache-maven-3.6.2
export PATH=$MAVEN_HOME/bin:$PATH
複製代碼
從新加載配置-->測試maven是否配置成功
[root@VM_0_6_centos apache-maven-3.6.2]# . /etc/profile
[root@VM_0_6_centos apache-maven-3.6.2]# mvn -version
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T23:06:16+08:00)
Maven home: /usr/local/apache-maven-3.6.2
Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8/jre
複製代碼
刪除安裝包(可選):rm -rf apache-maven-3.6.2-bin.zip
下載 tomcat8 ,上傳到linux,並解壓
[root@VM_0_6_centos local]# unzip apache-tomcat-8.5.46.zip
[root@VM_0_6_centos local]# ls
apache-maven-3.6.2 bin include lib64 sbin yd.socket.server
apache-tomcat-8.5.46 etc jdk1.8 libexec share
apache-tomcat-8.5.46.zip games lib qcloud src
複製代碼
配置tomcat 給tomcat下的全部文件授給任何人均可執行的權限
[root@VM_0_6_centos local]# cd apache-tomcat-8.5.46/
[root@VM_0_6_centos apache-tomcat-8.5.46]# chmod a+x -R *
複製代碼
修改tomcat端口爲8090
[root@VM_0_6_centos apache-tomcat-8.5.46]# vim conf/server.xml
複製代碼
啓動tomcat:
[root@VM_0_6_centos apache-tomcat-8.5.46]# bin/startup.sh
複製代碼
查看tomcat是否啓動成功
[root@VM_0_6_centos apache-tomcat-8.5.46]# ps -ef|grep tomcat
複製代碼
root 22857 1 29 19:12 pts/0 00:00:02 /usr/local/jdk1.8/bin/java -Dcatalina.base=/usr/local/apache-tomcat-8.5.46 -Dcatalina.home=/usr/local/apache-tomcat-8.5.46 -Djava.io.tmpdir=/usr/local/apache-tomcat-8.5.46/temp org.apache.catalina.startup.Bootstrap start root 27474 19080 0 10:10 pts/0 00:00:00 grep --color=auto tomcat
啓動成功標識!
關閉tomcat: 首先,進入Tomcat下的bin目錄
cd /usr/local/tomcat/bin
使用./shutdown.sh或者是sh shutdown.sh關閉Tomcat
複製代碼
想直接直接殺死Tomcat進程(進程號: 22857),使用kill命令
kill -9 22857
jenkins主機環境:java、jenkins
做爲slave的Linux機器爲centos系統. ip: 129.204.207.127 環境:java1.八、maven、git、tomcat:8090 路徑:
java : /usr/local/jdk1.8 maven : /usr/local/apache-maven-3.6.2 git : /usr/local/git tomcat: /usr/local/apache-tomcat-8.5.46
環境變量:vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8 export PATH=
PATH export CLASSPATH=.:
JAVA_HOME/lib/tools.jar PATH=
MAVEN_HOME/bin:$PATH
用戶:root/123 生成ssh:私鑰、公鑰 [Linux生成SSH密鑰方式](其它/SSH 證書生成(Linux、windows).md) 節點Slave生成:authorized_keys 並受權
[root@VM_0_6_centos ~]# cd .ssh
[root@VM_0_6_centos .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@VM_0_6_centos .ssh]# cat id_rsa.pub > authorized_keys
[root@VM_0_6_centos .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@VM_0_6_centos .ssh]# chmod 700 authorized_keys
複製代碼
獲取節點Slave私鑰:id_rsa
[root@VM_0_6_centos ~]# cd /root/.ssh
[root@VM_0_6_centos .ssh]# vim id_rsa
複製代碼
建立節點憑證:root/私鑰
三、建立節點slave 系統管理->節點管理->新建節點: 節點名稱:Linux_127 、 選着固定節點 -->肯定
查看節點列表:顯示節點磁盤大小說明配置成功
測試節點: 新建任務:任務名稱:springboot-test-gitlab、選擇:構建一個自由風格的軟件項目
控制檯輸出:
成功控制檯輸出
Started by user zhengja
Running as SYSTEM
Building remotely on Linux_127 in workspace /root/.jenkins/workspace/springboot-test-gitlab
[springboot-test-gitlab] $ /bin/sh -xe /tmp/jenkins1901112003247678190.sh
+ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:edff:fe35:28c8 prefixlen 64 scopeid 0x20<link> ether 02:42:ed:35:28:c8 txqueuelen 0 (Ethernet) RX packets 510 bytes 75763 (73.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 489 bytes 56366 (55.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 group default link/ether c2:51:e9:ca:47:56 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::c051:e9ff:feca:4756/64 scope link valid_lft forever preferred_lft forever Finished: SUCCESS 複製代碼
中文解釋:
成功控制檯輸出 由用戶zhengja發起 做爲系統運行 上遠程構建Linux_127在工做區/root/.jenkins/workspace/springboot-test-gitlab [springboot-test-gitlab] $ / bin / sh -xe /tmp/jenkins1901112003247678190.sh + ifconfig docker0:標誌= 4163 <UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1網絡掩碼255.255.0.0廣播172.17.255.255 inet6 fe80 :: 42:edff:fe35:28c8 prefixlen 64 scopeid 0x20 <link> 以太網02:42:ed:35:28:c8 txqueuelen 0(以太網) RX數據包510字節75763(73.9 KiB) 接收錯誤0掉線0超限0幀0 TX數據包489字節56366(55.0 KiB) TX錯誤0掉線0超限0載波0衝突0 組默認 連接/以太c2:51:e9:ca:47:56 brd ff:ff:ff:ff:ff:ff:ff連接netnsid 0 inet6 fe80 :: c051:e9ff:feca:4756/64做用域連接 永久有效_lft永久首選_lft 已完成:成功 複製代碼
配置節點成功!
目的:代碼上傳->gitlab通知->jenkins服務器執行構建->遠程應用服務器git下拉->遠程應用服務器maven打包->發送到遠程應用服務器的tomcat->發佈
deploy.sh腳本在springboot-test-gitlab/deploy.sh 項目根目錄下 其中項目中的腳本:deploy.sh
#!/usr/bin/env bash
#編譯+部署項目站點
#須要配置以下參數
# 項目路徑, 在Execute Shell中配置項目路徑, pwd 就能夠得到該項目路徑
# export PROJ_PATH=這個jenkins任務在部署機器上的路徑
# export PROJ_PATH=/root/.jenkins
# 輸入你的環境上tomcat的全路徑
# export TOMCAT_APP_PATH=tomcat在部署機器上的路徑
# export TOMCAT_APP_PATH=/usr/local/apache-tomcat-8.5.46
### base 函數
killTomcat()
{
pid=`ps -ef|grep tomcat|grep java|awk '{print $2}'`
echo "tomcat Id list :$pid"
if [ "$pid" = "" ]
then
echo "no tomcat pid alive"
else
kill -9 $pid
fi
#上面註釋的或者下面的
#cd $TOMCAT_APP_PATH/bin
#sh shutdown.sh
}
cd $PROJ_PATH/workspace/springboot-test-gitlab2/springboot
mvn clean install
# 停tomcat
killTomcat
# 刪除原有工程
#rm -rf $TOMCAT_APP_PATH/webapps/ROOT
#rm -f $TOMCAT_APP_PATH/webapps/ROOT.war
rm -f $TOMCAT_APP_PATH/webapps/springboot-test-gitlab.war
# 複製新的工程到tomcat上
cp $PROJ_PATH/workspace/springboot-test-gitlab2/springboot/target/springboot-test-gitlab-0.0.1-SNAPSHOT.war $TOMCAT_APP_PATH/webapps/
cd $TOMCAT_APP_PATH/webapps/
mv springboot-test-gitlab-0.0.1-SNAPSHOT.war springboot-test-gitlab.war
# 啓動Tomcat
cd $TOMCAT_APP_PATH/
sh bin/startup.sh
複製代碼
建立一個任務 名稱:springboot-test-gitlab2 類型: 一個自由風格的任務
源碼管理: gitlab配置節點的ssh公鑰,配置參考:[SSH 證書生成(Linux、windows).md](其它/SSH 證書生成(Linux、windows).md) 中的 「2、Linux生成證書-->3.證書使用-例如:配置gitlab」。
構建觸發器配置: 請參考:jenkins構建觸發器配置中的_代碼提交—>gitlab通知jenkins構建.
構建配置:
BUILD_ID=DONTKILLME . /etc/profile export PROJ_PATH=/root/.jenkins export TOMCAT_APP_PATH=/usr/local/apache-tomcat-8.5.46 sh $PROJ_PATH/workspace/springboot-test-gitlab2/springboot/deploy.sh
保存->執行構建 執行構建結果:
Started by GitLab push by zhengja
Started by user zhengja
Rebuilds build #7
Running as SYSTEM
Building remotely on Linux_127 in workspace /root/.jenkins/workspace/springboot-test-gitlab2
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url ssh://git@elbgit-1200450932.cn-northwest-1.elb.amazonaws.com.cn:5337/zhengja/springboot-test-gitlab.git # timeout=10
Fetching upstream changes from ssh://git@elbgit-1200450932.cn-northwest-1.elb.amazonaws.com.cn:5337/zhengja/springboot-test-gitlab.git
> git --version # timeout=10
> git fetch --tags --progress ssh://git@elbgit-1200450932.cn-northwest-1.elb.amazonaws.com.cn:5337/zhengja/springboot-test-gitlab.git +refs/heads/*:refs/remotes/origin/*
...........
.........
..........
09:28:22.884 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE) 2019-09-30 09:28:23.541 INFO 29490 --- [ main] d.s.SpringbootTestGithubApplicationTests : Starting SpringbootTestGithubApplicationTests on VM_0_6_centos with PID 29490 (started by root in /root/.jenkins/workspace/springboot-test-gitlab2/springboot) ................ ............ ........ [INFO] Installing /root/.jenkins/workspace/springboot-test-gitlab2/springboot/pom.xml to /root/.m2/repository/com/dist/springboot-test-gitlab/0.0.1-SNAPSHOT/springboot-test-gitlab-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 16.105 s [INFO] Finished at: 2019-09-30T09:28:31+08:00 [INFO] ------------------------------------------------------------------------ tomcat Id list :29272 Tomcat started. Finished: SUCCESS 複製代碼
訪問:http://129.204.207.127:8090/springboot-test-gitlab/
html頁面顯示:
進入首頁 測試自動構建是否成功 測試gitlab-->通知jenkins自動構建
到此,項目的自動部署及發佈完成!
目的:代碼上傳->gitlab通知->jenkins服務器執行構建->遠程應用服務器git下拉->遠程應用服務器maven打包->發送到遠程應用服務器的tomcat->發佈->郵箱通知
參考 :jenkins 郵箱通知配置