Docker容器使用jenkins部署web項目--總結(二)


(1)需要安裝Docker容器,在Docker容器內安裝jenkins,gogs,tomcat。   新建maven項目,添加findbugs plugin。

使用docker啟動jenkins,gogs,Tomcat的命令gogs :

 docker run -itd -p 10022:22 -p 10080:3000 --restart=always --privileged=true --name=gogs -v /var/gogs:/data gogs/gogs jenkins: docker run -itd -p 8800:8080 -p 50000:50000 --restart=always --privileged=true --name=jenkins -v /home/jenkins:/var/jenkins_home jenkins tomcat: docker run -itd -p 8080:8080  --restart=always --privileged=true --name=tomcat -v /usr/local/tomcat:/var/tomcat_home tomcat:8.0
后來啟動tomcat的命令:
 docker run -itd -p 8080:8080  --restart=always --privileged=true --name=tomcat -v /usr/local/tomcat:/home/tomcat/tomcat_home tomcat:8.0
解釋:
-i :表示以交互形式打開
-d :后台運行
-t :偽終端
-p :指定端口 前面的是你指定用戶用來訪問的端口號,后面的是指該軟件本來默認的端口號
--restart=always : 使得程序總是處於運行狀態,自動啟動
--privileged=true : 和防火牆有關,selinux權限 (設置這個程序不會受防火牆的影響)
--name : 指定容器運行的名稱
-v : 容器掛載,前面是實實在在存在的數據卷,后面是掛載目錄
最后的 gogs/gogs jenkins tomcat:8.0 是鏡像名,docker pull命令后面跟的參數

 

 

(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創建倉庫時,記得私有化,配置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時,如果推送的時候出現了 403錯誤,要查看jenkins中是否安裝了 gogs-plugin這個插件(因為我當時出錯了半天,就是因為沒有安裝gogs-plugin)

webhook示例:http://172.150.15.9:8800/gogs-webhook/?job=WebdemoIn7   //WebdemoIn7是我的enkins項目名

 

(5)創建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>
            <!-- 指定執行的主類(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插件 :靜態檢查代碼的錯誤-->
        <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 階段觸發執行findbugs檢查,比如執行 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構建項目時,前面的配置一如往常,可以查看其它的案例

主要配置 源碼管理,構建觸發器,build,構建后操作

 

然后部署可以訪問了

http://172.150.12.32:8080/WebdemoIn7

 


免責聲明!

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



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