【CI/CD】Jenkins + GitLab + Harbor实现自动化打包docker镜像

1.jenkins日志

 

 

 2. dockerfile

FROM openjdk:8u171-jre-alpine
ENV JAR_NAME=spring-boot-01-helloworld-quick-0.0.1-SNAPSHOT.jar
COPY ./target/$JAR_NAME  /apps/srv/instance/$JAR_NAME
EXPOSE 80
#ENTRYPOINT ["java","-jar","/apps/srv/instance/spring-boot-01-helloworld-quick-0.0.1-SNAPSHOT.jar","--server.port=80"]
ENTRYPOINT java -jar /apps/srv/instance/$JAR_NAME --server.port=80

3. jenkins pipeline

#!/usr/bin/env groovy

final def releaseTag = '1.1'


pipeline {
    agent any 
    tools {
        maven 'maven3.8.4'
    }


    stages {
        stage('Checkout') { 
            steps {
                echo 'checkout'
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'SubmoduleOption', disableSubmodules: true, parentCredentials: false, recursiveSubmodules: false, reference: '', trackingSubmodules: false], [$class: 'RelativeTargetDirectory', relativeTargetDir: 'spring-boot-demo']], submoduleCfg: [], userRemoteConfigs: [[url: 'http://192.168.10.99/root/spring-boot-demo.git']]]) 
            }
        }
        stage("maven 编译") {
            steps {
                echo "releaseTag:${releaseTag}"
                echo 'Building..'
                sh 'cd ./spring-boot-demo && mvn clean package -Dmaven.test.skip'
            }
        }
        stage('镜像编译') {
            steps {
                echo 'image build'
                sh 'pwd && cd ./spring-boot-demo && ls'
                sh "cd ./spring-boot-demo && sudo docker build -f ./dockerfile -t username/spring_docker:${releaseTag} . "
                sh 'sudo docker images'
            }
        }
        stage('镜像发布') {
            steps {
                echo 'image push docker hub'
                echo "releaseTag:${releaseTag}"
                sh 'sudo docker tag username/spring_docker:1.1 192.168.10.99:8090/test/spring_docker:1.1'
                sh 'sudo docker login 192.168.10.99:8090 -u admin -p Harbor12345'
                sh "sudo docker push 192.168.10.99:8090/test/spring_docker:1.1"
                sh 'sudo docker logout'
            }
        }
        stage('停止旧容器') {
            steps {
                sh 'sudo docker rmi --force $(sudo docker images -f "dangling=true" -q)'
                sh 'sudo docker rm -f spring_docker &> /dev/null'
                sh 'sudo docker system prune -a'
                sh 'sudo docker images'
            }
        }
        stage('部署') {
            steps {
                echo 'deloy'
                sh "sudo docker run -it -d -p 8881:80 --name spring_docker username/spring_docker:1.1"
            }
        }
    }
    post {
        success {
            send_email('success', "xxxxxxxxx")
        }
        failure {
            send_email('failed', "xxxxxxxxxx")
        }
        aborted {
            send_email('aborted', "xxxxxxxxxx")
        }
    }
}


def send_email(String result, String receiver) {
    def subject = '$PROJECT_NAME - Build # $BUILD_NUMBER - ' + "$result!"
    emailext (
        subject: "$subject",
        body: '$DEFAULT_CONTENT',
        to: "$receiver"
    )
}

 

4. 镜像仓库

 

 

 

 

 

 

 

参考链接:

https://blog.csdn.net/nklinsirui/article/details/99193760

https://www.freesion.com/article/32191046603/

posted @ 2022-02-25 15:43  代码诠释的世界  阅读(659)  评论(0编辑  收藏  举报