Jenkins+docker微服务部署参数化构建实际案例

本文展示了如何使用Jenkins创建参数化流水线,通过Docker进行微服务部署。流程包括代码检出、服务安装、打包、镜像构建与推送、以及文件传输到目标服务器。涉及的工具有Maven、Ansible和Docker。

在Jenkins中创建流水线,并以参数化构建过程。参数参考下方:

 

流水线脚本内容:

pipeline {
    agent any
    tools { 
        maven "maven-3.8.1"
    }
    environment {
        image_path = "/data/glzn"
        file_path = "/data/app/files/iot-core/${profile_active}"
    }    
    stages {
        stage('Check out') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/develop']],
                extensions: [],
                userRemoteConfigs: [[credentialsId: '366f3dc5-074e-4338-8a96-32db1990b9fb', url: 'http://192.168.180.148/iot/iot-core-manager.git']]])
            }
        }
        
//         stage('Install IoT-API') {
//             steps {
//                 sh '''
//                     cd iot-platform/iot-api
//                     mvn clean install -Dmaven.test.skip=true
//                     '''
//             }
//         }
        
        stage('Install IoT-Service') {
            steps {
                sh '''
                    cd iot-platform/iot-service
                    mvn clean install -Dmaven.test.skip=true
                    '''
            }
        }
        
//         rm -rf *.jar *.tar.gz            
        stage('Deploy IoT-Admin') {
            steps {
                sh '''
                    cd iot-platform/iot-admin
                    mvn clean package -P ${profile_active} -Dmaven.test.skip=true
                    mv -f target/iot-admin.jar ${image_path}/iot-admin/app.jar
                    
                    cd ${image_path}/iot-admin
                    tag=`date +'%Y%m%d-%H%M%S'`
                    docker build -t 192.168.180.204:19080/iot/iot-admin/x86:${profile_active}$tag .
                    docker images
                    docker push 192.168.180.204:19080/iot/iot-admin/x86:${profile_active}$tag
                    docker save -o iot-admin.tar 192.168.180.204:19080/iot/iot-admin/x86:${profile_active}$tag
                    tar -zcvf iot-admin.tar.gz iot-admin.tar
                    rm -rf iot-admin.tar
                    docker rmi 192.168.180.204:19080/iot/iot-admin/x86:${profile_active}$tag

                    ansible $file_server_ip -m shell -a "mkdir -p ${file_path}"
                    ansible $file_server_ip -m copy -a "src=iot-admin.tar.gz dest=${file_path}/iot-admin-${profile_active}$tag.tar.gz"                         
                    '''
            }
        }
    
        stage('Deploy TDengine-Component') {
            steps {
                sh '''
                    cd iot-platform/iot-component/tdengine-component
                    mvn clean package -P ${profile_active} -Dmaven.test.skip=true
                    docker images
                    cd ${image_path} && rm -rf tdengine-component.tar.gz
                    docker save -o tdengine-component.tar 192.168.180.204:19080/iot/tdengine-component:1.0.1
                    tar -zcvf tdengine-component.tar.gz tdengine-component.tar
                    rm -rf tdengine-component.tar
                    
                    ansible $file_server_ip -m shell -a "mkdir -p ${file_path}"
                    ansible $file_server_ip -m copy -a "src=tdengine-component.tar.gz dest=${file_path}/tdengine-component.tar.gz"     
                    '''
            }
        }
 
        stage('Deploy IoT-GateWay') {
            steps {
                sh '''
                    cd iot-platform/iot-gateway
                    mvn clean package -P ${profile_active} -Dmaven.test.skip=true
                    docker images
                    cd ${image_path} && rm -rf iot-gateway.tar.gz
                    docker save -o iot-gateway.tar 192.168.180.204:19080/iot/iot-gateway:1.0.1
                    tar -zcvf iot-gateway.tar.gz iot-gateway.tar
                    rm -rf iot-gateway.tar
                    
                    ansible $file_server_ip -m shell -a "mkdir -p ${file_path}"
                    ansible $file_server_ip -m copy -a "src=iot-gateway.tar.gz dest=${file_path}/iot-gateway.tar.gz"                        
                    '''
            }
        }
        
        stage('Deploy MQTT-Component') {
            steps {
                sh '''
                    cd iot-platform/iot-component/mqtt-component
                    mvn clean package -P ${profile_active} -Dmaven.test.skip=true
                    docker images
                    cd ${image_path} && rm -rf mqtt-component.tar.gz
                    docker save -o mqtt-component.tar 192.168.180.204:19080/iot/mqtt-component:1.0.1
                    tar -zcvf mqtt-component.tar.gz mqtt-component.tar
                    rm -rf mqtt-component.tar
                    
                    ansible $file_server_ip -m shell -a "mkdir -p ${file_path}"
                    ansible $file_server_ip -m copy -a "src=mqtt-component.tar.gz dest=${file_path}/mqtt-component.tar.gz"                    
                    '''
            }
        }
        
    }
}
 

Dockerfile文件示例(以iot-admin为例):

FROM 192.168.200.17/software/jre11:openjdk11

LABEL maintainer Ron
ENV JAVA_OPTS="$JAVA_OPTS -Dname=iot-admin -Xms512M -Xmx2048M -Duser.timezone=GMT+08 -Dfile.encoding=UTF8"
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY app.jar /
CMD ["/bin/sh", "-c", "java -jar $JAVA_OPTS /app.jar"]
 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI知识分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值