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

被折叠的 条评论
为什么被折叠?



