參考網站:https://www.jenkins.io/zh/
下面的練習基於jenkins版本: 2.249.1
1. 簡介
Jenkins是開源CI&CD軟件領導者, 提供超過1000個插件來支持構建、部署、自動化, 滿足任何項目的需要。
jenkins有一系列的插件,可以用於拉取代碼、打包(java\vue)項目、以及傳輸文件之后shell腳本等操作。這些插件組合起來可以滿足大部分自動部署需求。而且jenkins提供了獲取構建信息等的接口,可以用客戶端獲取到jenkins構建信息等操作。
2.安裝
當然如果采用docker安裝的話會非常簡單。
1. 安裝JDK
直接yum安裝 openjdk
yum install java-1.8.0-openjdk*
2. 下載jenkins
http://mirrors.jenkins.io/war-stable/latest/jenkins.war
如下:
[root@localhost jenkins]# ls jenkins.war
3. 啟動jenkins
[root@localhost jenkins]# java -jar jenkins.war --httpPort=8088 Running from: /opt/jenkins/jenkins.war webroot: $user.home/.jenkins 2020-09-13 16:50:16.155+0000 [id=1] INFO org.eclipse.jetty.util.log.Log#initialized: Logging initialized @7478ms to org.eclipse.jetty.util.log.JavaUtilLog 2020-09-13 16:50:18.705+0000 [id=1] INFO winstone.Logger#logInternal: Beginning extraction from war file 2020-09-13 16:50:20.551+0000 [id=1] WARNING o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath 2020-09-13 16:50:21.299+0000 [id=1] INFO org.eclipse.jetty.server.Server#doStart: jetty-9.4.30.v20200611; built: 2020-06-11T12:34:51.929Z; git: 271836e4c1f4612f12b7bb13ef5a92a927634b0d; jvm 1.8.0_262-b10 2020-09-13 16:50:26.740+0000 [id=1] INFO o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet 2020-09-13 16:50:28.164+0000 [id=1] INFO o.e.j.s.s.DefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0 2020-09-13 16:50:28.168+0000 [id=1] INFO o.e.j.s.s.DefaultSessionIdManager#doStart: No SessionScavenger set, using defaults 2020-09-13 16:50:28.254+0000 [id=1] INFO o.e.j.server.session.HouseKeeper#startScavenging: node0 Scavenging every 600000ms 2020-09-13 16:50:36.340+0000 [id=1] INFO hudson.WebAppMain#contextInitialized: Jenkins home directory: /root/.jenkins found at: $user.home/.jenkins 2020-09-13 16:50:38.555+0000 [id=1] INFO o.e.j.s.handler.ContextHandler#doStart: Started w.@3e78b6a5{Jenkins v2.249.1,/,file:///root/.jenkins/war/,AVAILABLE}{/root/.jenkins/war} 2020-09-13 16:50:39.332+0000 [id=1] INFO o.e.j.server.AbstractConnector#doStart: Started ServerConnector@2d554825{HTTP/1.1, (http/1.1)}{0.0.0.0:8088} 2020-09-13 16:50:39.333+0000 [id=1] INFO org.eclipse.jetty.server.Server#doStart: Started @30672ms 2020-09-13 16:50:39.387+0000 [id=20] INFO winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled 2020-09-13 16:50:59.598+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Started initialization 2020-09-13 16:51:00.488+0000 [id=29] INFO jenkins.InitReactorRunner$1#onAttained: Listed all plugins 2020-09-13 16:51:30.556+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Prepared all plugins 2020-09-13 16:51:30.629+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Started all plugins 2020-09-13 16:51:30.777+0000 [id=29] INFO jenkins.InitReactorRunner$1#onAttained: Augmented all extensions 2020-09-13 16:51:56.187+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: System config loaded 2020-09-13 16:51:56.188+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: System config adapted 2020-09-13 16:51:56.190+0000 [id=28] INFO jenkins.InitReactorRunner$1#onAttained: Loaded all jobs 2020-09-13 16:51:56.474+0000 [id=29] INFO jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated 2020-09-13 16:52:03.166+0000 [id=40] INFO hudson.model.AsyncPeriodicWork#lambda$doRun$0: Started Download metadata 2020-09-13 16:52:03.660+0000 [id=40] INFO hudson.util.Retrier#start: Attempt #1 to do the action check updates server 2020-09-13 16:52:16.771+0000 [id=28] INFO o.s.c.s.AbstractApplicationContext#prepareRefresh: Refreshing org.springframework.web.context.support.StaticWebApplicationContext@2bc0226: display name [Root WebApplicationContext]; startup date [Sun Sep 13 12:52:16 EDT 2020]; root of context hierarchy 2020-09-13 16:52:16.773+0000 [id=28] INFO o.s.c.s.AbstractApplicationContext#obtainFreshBeanFactory: Bean factory for application context [org.springframework.web.context.support.StaticWebApplicationContext@2bc0226]: org.springframework.beans.factory.support.DefaultListableBeanFactory@fc9646 2020-09-13 16:52:16.994+0000 [id=28] INFO o.s.b.f.s.DefaultListableBeanFactory#preInstantiateSingletons: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@fc9646: defining beans [authenticationManager]; root of factory hierarchy 2020-09-13 16:52:23.053+0000 [id=28] INFO o.s.c.s.AbstractApplicationContext#prepareRefresh: Refreshing org.springframework.web.context.support.StaticWebApplicationContext@58dd076f: display name [Root WebApplicationContext]; startup date [Sun Sep 13 12:52:23 EDT 2020]; root of context hierarchy 2020-09-13 16:52:23.054+0000 [id=28] INFO o.s.c.s.AbstractApplicationContext#obtainFreshBeanFactory: Bean factory for application context [org.springframework.web.context.support.StaticWebApplicationContext@58dd076f]: org.springframework.beans.factory.support.DefaultListableBeanFactory@25f19f6c 2020-09-13 16:52:23.077+0000 [id=28] INFO o.s.b.f.s.DefaultListableBeanFactory#preInstantiateSingletons: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25f19f6c: defining beans [filter,legacy]; root of factory hierarchy 2020-09-13 16:52:26.119+0000 [id=28] INFO jenkins.install.SetupWizard#init: ************************************************************* ************************************************************* ************************************************************* Jenkins initial setup is required. An admin user has been created and a password generated. Please use the following password to proceed to installation: 9f01f93af3fa4695ae8aabd0b00cb228 This may also be found at: /root/.jenkins/secrets/initialAdminPassword ************************************************************* ************************************************************* *************************************************************
4. 瀏覽器訪問 8088端口

然后用上面生成的密碼進行登錄(如果沒有可以到上面提示的文件中查找密碼),登錄之后查看信息如下:

選擇第二個指定的插件進行安裝。之后不選擇安裝插件。
進入主頁面:

5. 安裝插件
Jenkins >> Manage Jenkins >> Manage Plugins (插件管理也是在這個選項卡)
這里可以看見已經安裝的plugin以及可以安裝的plugin。這里我們安裝:
Maven項目插件:Maven Integration plugin,這個插件可以讓我們創建一個maven構建任務。
ssh傳輸工具插件:Publish Over SSH 項目打包完成后,使用這個插件,通過ssh的方式傳輸到遠程服務器。
Gitlab插件:允許Jenkins訪問gitlab服務器,拉取代碼
3. 簡單使用
maven + gitlab + maven 實現自動部署。
1. 安裝git
yum install git
測試:
[root@localhost ~]# git --version git version 1.8.3.1
2.安裝maven
http://mirrors.cnnic.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz tar xf apache-maven-3.3.9-bin.tar.gz
(1)設置環境變量:
vi ~/.bashrc
(2)重新加載
source ~/.bashrc
(3)查看mvn版本:
[root@localhost bin]# mvn -v Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T11:41:47-05:00) Maven home: /opt/mvn/apache-maven-3.3.9 Java version: 1.8.0_262, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-1127.el7.x86_64", arch: "amd64", family: "unix"
最后最好是修改mvn的本地倉庫地址和默認的中央倉庫。如果可以, 將自己本地倉庫的文件上傳到服務器倉庫。參考文末的補充信息。
3.到jenkins中設置
(1) 設置憑據

(2)配置賬號:Jenkins >> Manage Jenkins >> System Configuration >> Configure System 中設置:

4. 創建項目:NewItem選擇maven Project

(1)設置git代碼以及使用的憑證

(2)設定maven的時候需要設定一些參數
主要如下:JDK參數

git如下:

maven如下:

5.保存之后進行構建
(1) 點擊立即構建

(2)點擊構建會在左下角顯示構建的進度
(3) 查看構建狀態

可以看到拉取的代碼是最新的代碼,帶gitlab查看也可以看出來。
(4)查看構建日志如下:(Console Output)
Started by user root Running as SYSTEM Building in workspace /root/.jenkins/workspace/publisSSmTemplate The recommended git tool is: NONE using credential gitlab > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://192.168.1.129:8080/root/ssmTemplate.git # timeout=10 Fetching upstream changes from http://192.168.1.129:8080/root/ssmTemplate.git > git --version # timeout=10 > git --version # 'git version 1.8.3.1' using GIT_ASKPASS to set credentials 登錄gitlab > git fetch --tags --progress http://192.168.1.129:8080/root/ssmTemplate.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision 36fef432e1a56fec22a138eeeed933620cbeb4fe (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 36fef432e1a56fec22a138eeeed933620cbeb4fe # timeout=10 Commit message: "xx" > git rev-list --no-walk 737c9a7f654bbd67e404980c2de51cca0d58a025 # timeout=10 Parsing POMs Discovered a new module cn.qlq:ssmtemplate ssmtemplate Modules changed, recalculating dependency graph Established TCP socket on 43537 [publisSSmTemplate] $ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64/bin/java -cp /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-agent-1.13.jar:/opt/mvn/apache-maven-3.3.9/boot/plexus-classworlds-2.5.2.jar:/opt/mvn/apache-maven-3.3.9/conf/logging jenkins.maven3.agent.Maven33Main /opt/mvn/apache-maven-3.3.9 /root/.jenkins/war/WEB-INF/lib/remoting-4.5.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-interceptor-1.13.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.13.jar 43537 <===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f /root/.jenkins/workspace/publisSSmTemplate/pom.xml clean package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building ssmtemplate 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ ssmtemplate --- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ssmtemplate --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 5 resources [INFO] [INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ ssmtemplate --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 4 source files to /root/.jenkins/workspace/publisSSmTemplate/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ssmtemplate --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ ssmtemplate --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ ssmtemplate --- [JENKINS] Recording test results [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ ssmtemplate --- [INFO] Building jar: /root/.jenkins/workspace/publisSSmTemplate/target/ssmtemplate-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ ssmtemplate --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:20 min [INFO] Finished at: 2020-09-15T18:34:01-04:00 [INFO] Final Memory: 28M/69M [INFO] ------------------------------------------------------------------------ Waiting for Jenkins to finish collecting data [JENKINS] Archiving /root/.jenkins/workspace/publisSSmTemplate/pom.xml to cn.qlq/ssmtemplate/0.0.1-SNAPSHOT/ssmtemplate-0.0.1-SNAPSHOT.pom [JENKINS] Archiving /root/.jenkins/workspace/publisSSmTemplate/target/ssmtemplate-0.0.1-SNAPSHOT.jar to cn.qlq/ssmtemplate/0.0.1-SNAPSHOT/ssmtemplate-0.0.1-SNAPSHOT.jar channel stopped Finished: SUCCESS
(5) 到項目的工作空間查看:
[root@localhost publisSSmTemplate]# pwd /root/.jenkins/workspace/publisSSmTemplate [root@localhost publisSSmTemplate]# ls target/ classes generated-sources maven-archiver maven-status ssmtemplate-0.0.1-SNAPSHOT.jar ssmtemplate-0.0.1-SNAPSHOT.jar.original test-classes
可以看到打包成功,生成了指定的jar包。
(6) 用jenkins自動打包的jar包啟動后查看:
[root@localhost publisSSmTemplate]# cd target/ [root@localhost target]# ls classes generated-sources maven-archiver maven-status ssmtemplate-0.0.1-SNAPSHOT.jar ssmtemplate-0.0.1-SNAPSHOT.jar.original test-classes [root@localhost target]# java -jar ssmtemplate-0.0.1-SNAPSHOT.jar --server.port=9000
瀏覽器訪問:

6. 構建后自動啟動打包后的腳本,編寫shell腳本啟動
(1)在/opt/jar目錄下創建腳本,腳本內容如下: 用於執行管理服務
#!/bin/bash #這里可替換為你自己的執行程序,其他代碼無需更改 APP_NAME=ssmtemplate-0.0.1-SNAPSHOT.jar #使用說明,用來提示輸入參數 usage() { echo "Usage: sh 腳本名.sh [start|stop|restart|status]" exit 1 } #檢查程序是否在運行 is_exist(){ pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } #啟動方法 start(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is already running. pid=${pid} ." else nohup java -jar $APP_NAME --server.port=9000 > log.file 2>&1 & echo "${APP_NAME} start success" fi } #停止方法 stop(){ is_exist if [ $? -eq "0" ]; then kill -9 $pid echo "kill pid ${pid}" else echo "${APP_NAME} is not running" fi } #輸出運行狀態 status(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is running. Pid is ${pid}" else echo "${APP_NAME} is NOT running." fi } #重啟 restart(){ stop start } #根據輸入參數,選擇執行對應方法,不輸入則執行使用說明 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac
(2)啟動測試
[root@localhost jar]# sh ssm.sh start ssmtemplate-0.0.1-SNAPSHOT.jar start success [root@localhost jar]# jps -l 91937 sun.tools.jps.Jps 60695 jenkins.war 91917 ssmtemplate-0.0.1-SNAPSHOT.jar
瀏覽器訪問:

(3)殺掉上面進程
[root@localhost jar]# jps -l 92500 sun.tools.jps.Jps 60695 jenkins.war [root@localhost jar]# ls log.file ssm.sh
(4)jenkins中編寫腳本
jenkins->publisSSmTemplate->configure中Post Steps編寫腳本:
內容如下:
pwd cp ./target/ssmtemplate*.jar /opt/jar/ cd /opt/jar/ sh ssm.sh restart
截圖如下:(添加pwd是為了在日志中查看當前目錄,默認的當前目錄是在 jenkins安裝目錄/workspace/工程名稱下)

(5) 點擊buildnow立即構建后查看構建輸出信息,如下:
Started by user root Running as SYSTEM Building in workspace /root/.jenkins/workspace/publisSSmTemplate The recommended git tool is: NONE using credential gitlab > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://192.168.1.129:8080/root/ssmTemplate.git # timeout=10 Fetching upstream changes from http://192.168.1.129:8080/root/ssmTemplate.git > git --version # timeout=10 > git --version # 'git version 1.8.3.1' using GIT_ASKPASS to set credentials 登錄gitlab > git fetch --tags --progress http://192.168.1.129:8080/root/ssmTemplate.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision 36fef432e1a56fec22a138eeeed933620cbeb4fe (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 36fef432e1a56fec22a138eeeed933620cbeb4fe # timeout=10 Commit message: "xx" > git rev-list --no-walk 36fef432e1a56fec22a138eeeed933620cbeb4fe # timeout=10 Parsing POMs Established TCP socket on 41890 [publisSSmTemplate] $ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64/bin/java -cp /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-agent-1.13.jar:/opt/mvn/apache-maven-3.3.9/boot/plexus-classworlds-2.5.2.jar:/opt/mvn/apache-maven-3.3.9/conf/logging jenkins.maven3.agent.Maven33Main /opt/mvn/apache-maven-3.3.9 /root/.jenkins/war/WEB-INF/lib/remoting-4.5.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-interceptor-1.13.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.13.jar 41890 <===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f /root/.jenkins/workspace/publisSSmTemplate/pom.xml clean package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building ssmtemplate 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ ssmtemplate --- [INFO] Deleting /root/.jenkins/workspace/publisSSmTemplate/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ssmtemplate --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 5 resources [INFO] [INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ ssmtemplate --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 4 source files to /root/.jenkins/workspace/publisSSmTemplate/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ssmtemplate --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ ssmtemplate --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ ssmtemplate --- [JENKINS] Recording test results [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ ssmtemplate --- [INFO] Building jar: /root/.jenkins/workspace/publisSSmTemplate/target/ssmtemplate-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ ssmtemplate --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:14 min [INFO] Finished at: 2020-09-15T19:30:38-04:00 [INFO] Final Memory: 28M/69M [INFO] ------------------------------------------------------------------------ Waiting for Jenkins to finish collecting data [JENKINS] Archiving /root/.jenkins/workspace/publisSSmTemplate/pom.xml to cn.qlq/ssmtemplate/0.0.1-SNAPSHOT/ssmtemplate-0.0.1-SNAPSHOT.pom [JENKINS] Archiving /root/.jenkins/workspace/publisSSmTemplate/target/ssmtemplate-0.0.1-SNAPSHOT.jar to cn.qlq/ssmtemplate/0.0.1-SNAPSHOT/ssmtemplate-0.0.1-SNAPSHOT.jar [publisSSmTemplate] $ /bin/sh -xe /tmp/jenkins7873121033192777679.sh channel stopped + pwd /root/.jenkins/workspace/publisSSmTemplate + cp ./target/ssmtemplate-0.0.1-SNAPSHOT.jar /opt/jar/ + cd /opt/jar/ + sh ssm.sh restart ssmtemplate-0.0.1-SNAPSHOT.jar is not running ssmtemplate-0.0.1-SNAPSHOT.jar start success Finished: SUCCES
瀏覽器訪問測試:
訪問404. 雖然構建成功,但是服務沒啟動,查看java進程如下:
[root@localhost jar]# jps -l 60695 jenkins.war 97678 sun.tools.jps.Jps
原因: Jenkins-Post Steps-Exec command 運行腳本,腳本執行結束,顯示Success,但是服務未啟動。Jenkins build結束后會kill掉衍生進程。
解決辦法:可以嘗試重置變量BUILD_ID,避免Jenkins kill進程。
shell腳本中添加如下命令:
#!/bin/sh export BUILD_ID=anyWordIsOk
重新構建成功后訪問:

至此完成了簡單的構建。可以實現自動打包以及執行shell腳本啟動服務,只不過打包和啟動都是在jenkins所在的服務器,下面研究打包后上傳到另一個應用服務器並啟動。
7. 構建完成后上傳到另一台服務器並執行遠程的腳本
這個依賴於Publish Over SSH插件,所以先安裝上這個插件。
(0) 准備另一台服務器,用於發布服務,也就是接收jenkins打包后的文件:(ip是130)
[root@localhost ssm]# pwd /opt/jar/ssm [root@localhost ssm]# ls ssm.sh
ssm.sh內容和上面的一樣,用於啟動服務,如下:
#!/bin/bash export BUILD_ID=anyWordIsOk #這里可替換為你自己的執行程序,其他代碼無需更改 APP_NAME=ssmtemplate-0.0.1-SNAPSHOT.jar #使用說明,用來提示輸入參數 usage() { echo "Usage: sh 腳本名.sh [start|stop|restart|status]" exit 1 } #檢查程序是否在運行 is_exist(){ pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } #啟動方法 start(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is already running. pid=${pid} ." else nohup java -jar $APP_NAME --server.port=9000 > log.file 2>&1 & echo "${APP_NAME} start success" fi } #停止方法 stop(){ is_exist if [ $? -eq "0" ]; then kill -9 $pid echo "kill pid ${pid}" else echo "${APP_NAME} is not running" fi } #輸出運行狀態 status(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is running. Pid is ${pid}" else echo "${APP_NAME} is NOT running." fi } #重啟 restart(){ stop start } #根據輸入參數,選擇執行對應方法,不輸入則執行使用說明 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac
(1) 配置SSHserver:Jenkins -》Configure System -》 SSH Servers 點擊add可以添加多個

點擊后可以測試連接。
(2)Jenkins -》 項目名稱 -》 configure 中在 Post Steps中設置上傳到其他服務器: Post Steps 可以添加多個操作。

參數解釋:
Source files: 是上傳的文件,可以用通配符的形式
remove prefix:刪除的目錄,如果不填,會在目錄下創建一個target目錄。官方解釋如下:
if Source files were target/deployment/images/**/ then you may want Remove prefix to be target/deployment This would create the images folder under the remote directory, and not target/deployment Jenkins environment variables can be used in this path.
Remote directory:是上傳的位置(遠程應用服務器位置),是一個相對路徑。相對於上面SSHserver設置的路徑。比如我上面設置的是/opt/jar,並且這個屬性設置為/ssm。則文件上傳后位於/opt/jar/ssm/ 目錄
Exec command: 是上傳后執行的遠程命令。在上面我也是啟動服務,注意默認路徑是當前登錄用戶的根目錄。我是root用戶,所以默認在/root
(3) 重新構建並且查看日志:可以看到構建成功,並且在jenkins所在服務器和遠程130服務器都啟動了服務。瀏覽器也可以訪問到。
Started by user root Running as SYSTEM Building in workspace /root/.jenkins/workspace/publisSSmTemplate The recommended git tool is: NONE using credential gitlab > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url http://192.168.1.129:8080/root/ssmTemplate.git # timeout=10 Fetching upstream changes from http://192.168.1.129:8080/root/ssmTemplate.git > git --version # timeout=10 > git --version # 'git version 1.8.3.1' using GIT_ASKPASS to set credentials 登錄gitlab > git fetch --tags --progress http://192.168.1.129:8080/root/ssmTemplate.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision 36fef432e1a56fec22a138eeeed933620cbeb4fe (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 36fef432e1a56fec22a138eeeed933620cbeb4fe # timeout=10 Commit message: "xx" > git rev-list --no-walk 36fef432e1a56fec22a138eeeed933620cbeb4fe # timeout=10 Parsing POMs Established TCP socket on 45294 [publisSSmTemplate] $ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.262.b10-0.el7_8.x86_64/bin/java -cp /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-agent-1.13.jar:/opt/mvn/apache-maven-3.3.9/boot/plexus-classworlds-2.5.2.jar:/opt/mvn/apache-maven-3.3.9/conf/logging jenkins.maven3.agent.Maven33Main /opt/mvn/apache-maven-3.3.9 /root/.jenkins/war/WEB-INF/lib/remoting-4.5.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven33-interceptor-1.13.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.13.jar 45294 <===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f /root/.jenkins/workspace/publisSSmTemplate/pom.xml clean package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building ssmtemplate 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ ssmtemplate --- [INFO] Deleting /root/.jenkins/workspace/publisSSmTemplate/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ssmtemplate --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 5 resources [INFO] [INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ ssmtemplate --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 4 source files to /root/.jenkins/workspace/publisSSmTemplate/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ssmtemplate --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ ssmtemplate --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ ssmtemplate --- [JENKINS] Recording test results [INFO] [INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ ssmtemplate --- [INFO] Building jar: /root/.jenkins/workspace/publisSSmTemplate/target/ssmtemplate-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ ssmtemplate --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:04 min [INFO] Finished at: 2020-09-15T22:18:16-04:00 [INFO] Final Memory: 29M/69M [INFO] ------------------------------------------------------------------------ Waiting for Jenkins to finish collecting data [JENKINS] Archiving /root/.jenkins/workspace/publisSSmTemplate/pom.xml to cn.qlq/ssmtemplate/0.0.1-SNAPSHOT/ssmtemplate-0.0.1-SNAPSHOT.pom [JENKINS] Archiving /root/.jenkins/workspace/publisSSmTemplate/target/ssmtemplate-0.0.1-SNAPSHOT.jar to cn.qlq/ssmtemplate/0.0.1-SNAPSHOT/ssmtemplate-0.0.1-SNAPSHOT.jar [publisSSmTemplate] $ /bin/sh -xe /tmp/jenkins5772284603317430255.sh channel stopped + pwd /root/.jenkins/workspace/publisSSmTemplate + cp ./target/ssmtemplate-0.0.1-SNAPSHOT.jar /opt/jar/ + cd /opt/jar/ + sh ssm.sh restart ssmtemplate-0.0.1-SNAPSHOT.jar is not running ssmtemplate-0.0.1-SNAPSHOT.jar start success SSH: Connecting from host [localhost.localdomain] SSH: Connecting with configuration [130server] ... SSH: EXEC: STDOUT/STDERR from command [cd /opt/jar/ssm sh ssm.sh restart] ... ssmtemplate-0.0.1-SNAPSHOT.jar is not running ssmtemplate-0.0.1-SNAPSHOT.jar start success SSH: EXEC: completed after 871 ms SSH: Disconnecting configuration [130server] ... SSH: Transferred 1 file(s) Finished: SUCCESS
4.gitlab自動構建
常用的應該有定時構建,cron表達式觸發。token以url方式構建以及webhook自動構建。
1. 通過生成token以url的方式構建
(1) 到jenkins首頁創建一個用戶

創建了一個 test/111222的用戶。
(2) 給用戶賦權:Manage Jenkins->Configure Global Security

(3)yongtest/111222之后登錄,點擊登錄的用戶之后點擊configure -》 add API Token

名稱為:ssmtoken
token串為:11f95b0b68a4f6a4cbb283226a249de4df
(3) 項目設置構建選擇:Trigger builds remotely (e.g., from scripts),例如: api名稱是隨便寫的

(4)遠程訪問,開始構建:
訪問如下url即可:
http://test:11f95b0b68a4f6a4cbb283226a249de4df@192.168.1.129:8088/job/publisSSmTemplate/build?token=pubssmtoken
規則為: http://用戶名:token串@主機/job/項目名稱/build?token=token名稱 需要注意這里的token名稱是在項目名稱填寫的名稱,如果寫錯不允許構建。
(5)查看構建日志
Started by remote host 192.168.1.130 Running as SYSTEM Building in workspace /root/.jenkins/workspace/publisSSmTemplate The recommended git tool is: NONE ...
補充:這種方式需要每次都手動復制URL訪問,可以用gitlab的webhook在代碼提交后自動訪問URL:
settings -> Integrations Settings 增加webhook的url,如下:

2.以webhook自動構建
依賴Generic Webhook Trigger插件,先到jenkins插件管理安裝該插件。
首先我們需要先了解,gitlab在發動webhook請求的時候會攜帶一些請求參數以及請求頭,而且不同的事件有不同的參數和頭。然后我們需要在jenkins中根據不同的請求參數來判斷是否進行構建。
比如push events推送代碼事件發送的數據如下:
Request header:
X-Gitlab-Event: Push Hook
Request body:
{ "object_kind": "push", "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "ref": "refs/heads/master", "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "user_id": 4, "user_name": "John Smith", "user_username": "jsmith", "user_email": "john@example.com", "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80", "project_id": 15, "project":{ "name":"Diaspora", "description":"", "web_url":"http://example.com/mike/diaspora", "avatar_url":null, "git_ssh_url":"git@example.com:mike/diaspora.git", "git_http_url":"http://example.com/mike/diaspora.git", "namespace":"Mike", "visibility_level":0, "path_with_namespace":"mike/diaspora", "default_branch":"master", "homepage":"http://example.com/mike/diaspora", "url":"git@example.com:mike/diaspora.git", "ssh_url":"git@example.com:mike/diaspora.git", "http_url":"http://example.com/mike/diaspora.git" }, "repository":{ "name": "Diaspora", "url": "git@example.com:mike/diaspora.git", "description": "", "homepage": "http://example.com/mike/diaspora", "git_http_url":"http://example.com/mike/diaspora.git", "git_ssh_url":"git@example.com:mike/diaspora.git", "visibility_level":0 }, "commits": [ { "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", "message": "Update Catalan translation to e38cb41.", "timestamp": "2011-12-12T14:27:31+02:00", "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", "author": { "name": "Jordi Mallach", "email": "jordi@softcatala.org" }, "added": ["CHANGELOG"], "modified": ["app/controller/application.rb"], "removed": [] }, { "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "message": "fixed readme", "timestamp": "2012-01-03T23:36:29+02:00", "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7", "author": { "name": "GitLab dev user", "email": "gitlabdev@dv6700.(none)" }, "added": ["CHANGELOG"], "modified": ["app/controller/application.rb"], "removed": [] } ], "total_commits_count": 4 }
(1)到項目中配置Build Triggers 選擇: Generic Webhook Trigger,進行參數設置
Post content parameters添加兩個變量:從請求參數提取ref和project.name賦值到tempref和projectname變量中


下面兩個打上對勾,便於查看日志:

Optional filter配置驗證:驗證上面提取的變量組合起來(Text中)是否滿足Expression表達式(這里驗證是ssmTemplate項目的master分支)。如果滿足會進行下面的構建操作,不滿足不會進行構建

(2)驗證:在另一台服務器上訪問:
直接訪問webhookurl發現沒權限:
[root@localhost ssm]# curl http://192.168.1.129:8088//generic-webhook-trigger/invoke {"jobs":null,"message":"Did not find any jobs with GenericTrigger configured! If you are using a token, you need to pass it like ...trigger/invoke?token=TOKENHERE. If you are not using a token, you need to authenticate like http://user:passsword@jenkins/generic-webhook... "}
可以加用戶名密碼進行訪問,如下: 說明已經訪問到webhook的url,只是參數不匹配
[root@localhost ssm]# curl http://test:111222@192.168.1.129:8088//generic-webhook-trigger/invoke {"jobs":{"publisSSmTemplate":{"regexpFilterExpression":"^(ssmTemplate-refs/heads/master)$","triggered":false,"resolvedVariables":{"projectname":"","tempref":""},"regexpFilterText":"-","id":0,"url":""}},"message":"Triggered jobs."}
(3)到gitlab配置webhook為上面地址:

(4)向master分支提交代碼查看構建記錄:可以看到是通過webhook產生的構建,並且打印了post content和variables

(5)測試向其他分支提交代碼發現不會觸發構建。
基於webhook的自動構建也完成。
補充:構建方式還有基於cron表達式周期性執行以及基於其他項目構建完成之后開始構建等方法,如下:

補充: jenkins的版本是2.249.1
這個可以到 jenkins的安裝目錄下config.xml中查看。
補充: jenkins配置賬戶
Manage Jenkins -》 Manager Users 進行設置,可以創建用戶。
補充:jenkins的工作空間默認在安裝目錄下的workspace目錄
比如我新建了一個任務是 publisSSmTemplate, 在jenkins目錄下會有一個對應的目錄,拉下來的代碼會放在該目錄,比如:
[root@localhost publisSSmTemplate]# pwd
/root/.jenkins/workspace/publisSSmTemplate
補充:mvn倉庫的默認位置是 ${user.home}/.m2/repository 目錄,中央倉庫默認是http://repo1.maven.org/maven2/,可以通過修改 mvn/conf/settings.xml 中修改。
(1)查看默認倉庫:
[root@localhost conf]# head -n 50 settings.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- | This is the configuration file for Maven. It can be specified at two levels: | | 1. User Level. This settings.xml file provides configuration for a single user, | and is normally provided in ${user.home}/.m2/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -s /path/to/user/settings.xml | | 2. Global Level. This settings.xml file provides configuration for all Maven | users on a machine (assuming they're all using the same Maven | installation). It's normally provided in | ${maven.home}/conf/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -gs /path/to/global/settings.xml | | The sections in this sample file are intended to give you a running start at | getting the most out of your Maven installation. Where appropriate, the default | values (values used when the setting is not specified) are provided. | |--> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- localRepository | The path to the local repository maven will use to store artifacts. [root@localhost conf]# head -n 60 settings.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- | This is the configuration file for Maven. It can be specified at two levels: | | 1. User Level. This settings.xml file provides configuration for a single user, | and is normally provided in ${user.home}/.m2/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -s /path/to/user/settings.xml | | 2. Global Level. This settings.xml file provides configuration for all Maven | users on a machine (assuming they're all using the same Maven | installation). It's normally provided in | ${maven.home}/conf/settings.xml. | | NOTE: This location can be overridden with the CLI option: | | -gs /path/to/global/settings.xml | | The sections in this sample file are intended to give you a running start at | getting the most out of your Maven installation. Where appropriate, the default | values (values used when the setting is not specified) are provided. | |--> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> -->
(2) 修改默認倉庫以及默認中央倉庫
<localRepository>/opt/mvn/repository</localRepository>
倉庫:
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
(3) 可以將自己本地倉庫的文件上傳到服務器,可以減少第一次打包下載依賴的時間。
補充:上面的腳本獲取jar包名稱也可以采用腳本的方式獲取,例如:
#!/bin/bash export BUILD_ID=anyWordIsOk # 獲取當前文件夾下面的jar名稱 APP_NAME=tmp basepath=$(cd `dirname $0`; pwd) dir=$(ls -l $basepath ) for i in $dir do if [ "${i##*.}" == "jar" ] then APP_NAME=$i fi done #使用說明,用來提示輸入參數 usage() { echo "Usage: sh 腳本名.sh [start|stop|restart|status]" exit 1 } #檢查程序是否在運行 is_exist(){ pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } #啟動方法 start(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is already running. pid=${pid} ." else nohup java -jar $APP_NAME --server.port=9000 > log.file 2>&1 & echo "${APP_NAME} start success" fi } #停止方法 stop(){ is_exist if [ $? -eq "0" ]; then kill -9 $pid echo "kill pid ${pid}" else echo "${APP_NAME} is not running" fi } #輸出運行狀態 status(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is running. Pid is ${pid}" else echo "${APP_NAME} is NOT running." fi } #重啟 restart(){ stop start } #根據輸入參數,選擇執行對應方法,不輸入則執行使用說明 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac
