Jenkins Pipeline 构建 Docker 镜像

一. 安装插件 及 环境配置:

1.插件:

Docker
Docker Pipeline
Pipeline Maven Integration Plugin

2.Jenkins运行环境
Docker 启动的 Jenkins 调用宿主机 docker 的目录挂载

3.全局工具配置中预配置
JDK , Maven , Docker (如果pipeline中使用tool工具,则需要与当前全局工具名称相同)

4.Manage Credentials 凭据
添加 gitlab 用户密码, 获取唯一标识

二.java - pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-test</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <docker.image>${name}</docker.image>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--   dockerfile-maven-plugin      -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <repository>${docker.image}/${project.artifactId}</repository>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

三.Dokcerfile

#FROM java:8
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} test-service.jar
EXPOSE 50008
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/test-service.jar"]

四.Pipeline script


pipeline {
    agent any
    environment { 
		def imageName = "springboot-test"
    }
    tools { 
        maven "maven3.6.1"
    }
    stages {

        stage('check out') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '777c8849-bd87-423a-838c-474f40dff409', url: 'http://47.111.81.224:9600/jenkins-test/springboot-test.git']]])
            }
        }
        stage('building dependency') {
            steps {
                sh '''
					mvn clean install -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true
				'''	
            }
        }
        stage('building selected project') {
            steps {
                sh '''
                
                    containerStr=`docker ps -a | grep -w ${imageName} | awk '{print $1}'`
					imageStr=`docker images | grep -w $imageName  | awk '{print $3}'`
					echo "container id:$containerStr"
					echo "image id:$imageStr"
					
					if [ "$imageStr" !=  "" ] ; then
						if [ "$containerStr" !=  "" ] ; then
							#停掉容器
							docker stop `docker ps -a | grep -w ${imageName}  | awk '{print $1}'`
						
							#删除容器
							docker rm `docker ps -a | grep -w ${imageName}  | awk '{print $1}'`
						
							#删除镜像
							docker rmi --force ${imageName}
						else
							 #删除镜像
							docker rmi --force ${imageName}
						
						fi
					fi
                
					mvn clean install dockerfile:build -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true
					
				'''	
            }
        }
        stage('push images') {
            steps {
                sh '''
                    prefix="reg.hub.com/library"
                    containerPushID=`docker images | grep -w ${prefix}/${imageName} | awk '{print $3}'`
                    if [ "$containerPushID" !=  "" ] ; then
                        #删除镜像
						docker rmi --force ${containerPushID}
                    fi
                
                    containerStr=`docker images | grep -w ${imageName} | awk '{print $1}'`
                    tagStr=`docker images | grep -w ${imageName} | awk '{print $2}'`
                    
                    if [ "$containerStr" !=  "" ] ; then
						if [ "$tagStr" !=  "" ] ; then
						   
						    containerTag="$containerStr:$tagStr"
						    echo "image tag:$containerTag"
						    
						    #标记镜像
						    docker image tag $containerTag $prefix/$containerTag
                            
                                                    #推送镜像
                                                    docker push $prefix/$containerTag
                            
						fi
					fi	
                    
                '''
            }
        }
    }
}
	
	

五.点击构建


查看镜像

docker images


运行容器:

#运行:
docker run --name newtest -p 50008:50008 -d springboot-test/springboot-test:latest

#查看容器
docker ps -a 

#查看容器服务 启动日志
docker logs [options]

访问服务连接:

六. pull 至镜像仓库

修改Pipeline script, 添加 stage, 可以直接 在POM 中添加 docker image 的仓库地址, 就不需要 进行tag 重新进行标签了。
若出现以下报错,则需要登陆容器 进行登陆 harbor仓库,登陆一次后, 自动保存仓库地址

#登陆容器
docker exec -it --user root [containerId] bash

#登陆远程仓库 ,输入用户名/密码即可,下次操作就不需要认证了
docker login [URL/IP]

换一台机器,进行pull 拉起镜像

#本案例中 reg.hub.com域名自签 映射的 192.168.0.205
docker pull 192.168.0.205/library/springboot-test/springboot-test

posted @ 2021-12-14 11:03  栋_RevoL  阅读(2175)  评论(1编辑  收藏  举报