Docker /git 结合 Jenkins

docker+Jenkins

结合Jenkins大体思路:将springboot应用打包并发布成docker镜像,然后再运行。Jenkins主要执行的步骤是:拉取代码--->打包--->发布镜像---->运行镜像

  1. spring boot 应用配置

    添加下面maven插件:

    dockerfile 位置根据具体情况修改!!!

         
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
                
                    com.spotify
                    dockerfile-maven-plugin
                    1.4.13
                    
                        
                            default
                            
                                build
                                push
                            
                        
                    
                    
                        ${project.name}
                        ${project.version}
                        
                            ${project.build.finalName}.jar
                        
                        ./Dockerfile
                    
                
            
    
  1. 编写docker file

    FROM openjdk:8-jre-slim
    MAINTAINER xuxueli
    
    ENV PARAMS=""
    
    ENV TZ=PRC
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    
    COPY target/xxl-job-executor-0.0.1-SNAPSHOT.jar /app.jar
    
    ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS /app.jar $PARAMS"]
    
    

    只需要改变下target(打包完之后的包名)

  2. 新建Jenkins应用,并配置好git信息。仓库地址和验证信息和分支等

image-20210312095650376.png
  1. 加入maven构建

[图片上传中...(image-20210312095914050.png-98c522-1615859185694-0)]

image-20210312095759829.png
  1. 设置maven的setting文件【非必须】

    因为默认的setting下载会很慢,所以需要更改setting文件

    a. 进入manage jenkins----> manage files

image-20210312095914050.png
b. 新增config
image-20210312095929282.png
c. 选择mave setting.xml,并提交
image-20210312100335611.png
    d. settting 复制进去
image-20210312100501205.png
  1. 选择刚刚设置好的maven setting.xml,打开maven的高级设置

    maven 目标:clean -U -Dmaven.skip.test=true package

    image-20210312101451622.png
  1. 添加脚本:

    docker stop xxl-job-executor
    docker start xxl-job-executor
    
  1. 测试脚本,直接build,一步一个脚印,一行一行脚本调试

问题记录:

  1. docker运行提示:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication

    原因:包没有引入

    解决方案:精简maven插件:

  2. no main manifest attribute

    原因:

    1. plugins 中是按顺序执行的,顺序反了必然会出现问题。

    2. springboot结构不完整。需要有完整的父依赖(https://stackoverflow.com/questions/54867295/springboot-no-main-manifest-attribute-maven)

    3. maven执行springboot的repackge目标:mvn package spring-boot:repackage

    4. 或者maven添加插件:

      
              org.springframework.boot
              spring-boot-maven-plugin
              
                  com.places.Main
              
      
              
                  
                      
                          repackage
                      
                  
             
      
      
      
    5. springboot 打包运行成功要素

      3 things:
      - You have the parent entry in your pom.
      - Verify that your plugin is in the build portion of the pom.
      - You have a class with the @SpringBootApplicaion annotation.
      
      pom.xml:
      
      ...  
        
          org.springframework.boot
          spring-boot-starter-parent
          2.1.8.RELEASE
        
      
         
          
            
              org.springframework.boot
              spring-boot-maven-plugin
            
          
        
      ...
      And a class that looks something like this:
      
      @SpringBootApplication
      public class Application {
      
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      
      }
      

你可能感兴趣的:(Docker /git 结合 Jenkins)