Docker容器使用jenkins部署web項目(總結)
(1)需要安裝Docker容器,在Docker容器內安裝jenkins,gogs,tomcat。 新建maven項目,添加findbugs plugin。
使用docker啟動jenkins,gogs,Tomcat的命令gogs :
jenkins:
tomcat:
tomcat:8.0
后來啟動tomcat的命令:
解釋:
-i :表示以交互形式打開
-d :后臺運行
-t :偽終端
-p :指定端口 前面的是你指定用戶用來訪問的端口號,后面的是指該軟件本來默認的端口號
--restart=always : 使得程序總是處于運行狀態(tài),自動啟動
--privileged=true : 和防火墻有關,selinux權限 (設置這個程序不會受防火墻的影響)
--name : 指定容器運行的名稱
-v : 容器掛載,前面是實實在在存在的數(shù)據(jù)卷,后面是掛載目錄
最后的 gogs/gogs jenkins tomcat:8.0 是鏡像名,docker pull命令后面跟的參數(shù)
(2)在jenkins上安裝插件: maven Intergration plugin ,gogs-plugin ,publish over ssh, findbugs-plugin,Deploy to a container (jdk ,git 都使用Docker中默認的,安裝jenkins的時候不需要配置這兩項的路徑)
(3)tomcat需要配置用戶: 通過 find / -name "tomcat" ,找到Tomcat的安裝路徑,再將內容添加到 conf/tomcat-users.xml文件中 <tomcat-users>大概這個位置</tomcat-users>
<role rolename="admin"/> <role rolename="manager"/> <role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="tomcat" password="tomcat" roles="admin,manager,manager-gui,manager-script"/>
(4)gogs創(chuàng)建倉庫時,記得私有化,配置git鉤子,在.git/hooks/目錄下添加 pre-commit 文件,pre-commit 文件中的內容如下
#!/bin/sh #execute shell before commit,check the code mvn clean install #recieve the execute result result=$? #output the result ,if the result less or equal 0 ,it proves this project has bugs,otherwise don't. echo $result if [ $result -ne 0 ] then mvn findbugs:gui echo "REGRETFUL! BUILD FAILURE" exit 1 else echo "CONGRATURATION! BUILD SUCCESS" exit 0 fi
注釋: 配置webhook時,如果推送的時候出現(xiàn)了 403錯誤,要查看jenkins中是否安裝了 gogs-plugin這個插件(因為我當時出錯了半天,就是因為沒有安裝gogs-plugin)
webhook示例:http://172.150.15.9:8800/gogs-webhook/?job=WebdemoIn7 //WebdemoIn7是我的enkins項目名
(5)創(chuàng)建maven項目時,pom.xml中的內容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.demo</groupId> <artifactId>WebdemoIn7</artifactId> <packaging>war</packaging> <!-- 打包為war包 --> <version>0.0.1-SNAPSHOT</version> <name>WebdemoIn7 Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>WebdemoIn7</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> <!-- 指定執(zhí)行的主類(main方法所在的類)--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <!-- 添加index則不從mainfest中讀取classpath,而是從Index.list中讀取 --> <!-- <index>true</index> --> <manifest> <mainClass>cn.demo.JavademoIn7.application.ApplicationMain</mainClass> </manifest> </archive> </configuration> </plugin> <!-- findbugs插件 :靜態(tài)檢查代碼的錯誤--> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId> <version>3.0.4</version> <configuration> <!-- 設置分析工作的等級,可以為Min、Default和Max --> <effort>Low</effort> <!-- Low、Medium和High (Low最嚴格) --> <threshold>Medium</threshold> <failOnError>true</failOnError> <includeTests>true</includeTests> <!--findbugs需要忽略的錯誤的配置文件--> <!-- <excludeFilterFile>compile.bat</excludeFilterFile> --> </configuration> <executions> <execution> <id>run-findbugs</id> <!-- 在install 階段觸發(fā)執(zhí)行findbugs檢查,比如執(zhí)行 mvn clean package--> <phase>install</phase> <goals> <goal>check</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <!-- servlet/jsp/EL (2.4/2.0/?)(2.5/2.1/2.1),(3.0/2.2/2.2),(3.1/2.3/3.0) --> <servlet.version>3.1.0</servlet.version> <jsp.version>2.3.1</jsp.version> <jstl.version>1.2</jstl.version> <junit.version>4.12</junit.version> </properties> <dependencies> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> </dependencies> </project>
(6)jenkins構建項目時,前面的配置一如往常,可以查看其它的案例
主要配置 源碼管理,構建觸發(fā)器,build,構建后操作
然后部署可以訪問了
http://172.150.12.32:8080/WebdemoIn7
書寫Shell腳本來構建Java Web鏡像和容器:
1.在Post Steps目錄中選擇
填寫如下Shell腳本
#!/bin/bash imageId=`sudo docker images|grep -i test|awk '{print $3}'` echo "Test鏡像ID = "$imageId containId=`sudo docker ps -a |grep -i test|awk '{print $1}'` echo "Test容器ID = "$containId project=/var/jenkins_home/workspace/test/src/main/resources/docker #判斷是否存在舊的Test鏡像 if test -z "$imageId" then echo "Test鏡像不存在" else if test -z "$containId" then echo "Test容器不存在" else echo "Test容器將要被執(zhí)行stop命令" sudo docker stop test echo "Test容器處于stop狀態(tài)" fi echo "舊Test鏡像將要被刪除" sudo docker rmi -f $imageId echo "成功刪除舊Test鏡像" fi #Dockerfile所在目錄 sudo mv $project/Dockerfile /usr #切換目錄至usr cd /usr #將Tms war包拷貝到Dockerfile所在目錄下 sudo mv /var/jenkins_home/workspace/test/target/test.war . echo "Test鏡像構建中:------->" #構建Tms鏡像 sudo docker build -t test . #判斷是否存在舊的Tms容器 if test -z "$containId" then echo "Test容器不存在" else echo "舊Test容器將要被刪除" sudo docker rm -f $containId echo "成功刪除舊Test容器" fi #創(chuàng)建容器 echo "開始創(chuàng)建新Test容器" sudo docker run -d -p 8088:8080 -v /usr/logs:/usr/tomcat/logs --name test test
echo "新Test容器創(chuàng)建成功"
2.點擊立即保存,立即構建
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ssh 直接登錄docker容器的操作方法(不用通過宿主機進入)
這篇文章主要介紹了ssh 直接登錄docker容器的操作方法(不用通過宿主機進入),本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-06-06zabbix監(jiān)控docker容器狀態(tài)【推薦】
這篇文章主要介紹了zabbix監(jiān)控docker容器狀態(tài)的相關資料,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06docker-compose網(wǎng)絡設置之networks的使用
本文詳細解釋了在使用 Docker Compose時如何配置網(wǎng)絡,包括創(chuàng)建、使用和問題解決等方面,介紹了如何通過docker-compose.yml文件快速編排和部署應用服務,同時解決網(wǎng)絡隔離問題,感興趣的可以了解一下2024-10-10