1.创建pipline工程
2.准备工程Jenkinsfile文件(java目录)
1.文件脚本内容
env.fileName = "planetflix-app.jar"
env.configName = "planetflix_prod"
env.remoteDirectory = "/data/project/java"
env.sourceFile = "/var/jenkins_home/workspace/release_planetxflix_pipline/planetflix-api/target/${env.fileName}"
env.shellName="devops.sh"
env.shellDirectory = "/home/planetflix/shell"
pipeline{
agent any
//定义流水线的加工流程
stages {
stage('打JAR包') {
steps {
sh label:'',script: 'mvn clean package -Dmaven.test.skip=true'
}
}
stage('推送部署文件到远程服务器') {
steps {
sh """
cp -f ${env.sourceFile} ./
"""
sshPublisher(publishers: [sshPublisherDesc(configName: "${env.configName}", transfers: [sshTransfer(cleanRemote: false, excludes: '',
execCommand: 'echo "...成功推送planetflix-app.jar..."',
execTimeout: 120000, flatten: false,
makeEmptyDirs: false, noDefaultExcludes: false,
patternSeparator: '[, ]+',
remoteDirectory: "${env.remoteDirectory}",
remoteDirectorySDF: false, removePrefix: '',
sourceFiles: "${env.fileName}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '推送成功'
}
}
stage('运行部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: "${env.configName}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand:
"sh ${env.shellDirectory}/${env.shellName}", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo "部署完成"
}
}
}
}
2.devops.sh文件
#!/bin/bash
#服务名称
SERVER_NAME=planetflix-app
#端口信息
PORT=10021
# 镜像tag
IMAGE_TAG=1.0.0-SNAPSHOT
#dockerFile存放路径
DOCKER_PATH=/home/planetflix/shell
#容器id
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $SERVER_NAME}')
#镜像id
IID=$(docker images | grep "$SERVER_NAME" | awk '{print $3}')
# 构建docker镜像
function build(){
if [ -n "$IID" ]; then
echo "存在$SERVER_NAME镜像,IID=$IID"
else
echo "不存在$SERVER_NAME镜像,开始构建镜像"
cd $DOCKER_PATH
echo "------ 开始构建镜像:${SERVER_NAME} ------"
docker build -t $SERVER_NAME:$IMAGE_TAG -f Dockerfile . --no-cache
fi
}
build
# 运行docker容器
if [[ -n $(docker ps -q -f "name=^${SERVER_NAME}$") ]];then
echo "------ 容器正在运行:${SERVER_NAME} ------"
echo "------ 停止容器:$SERVER_NAME ------"
docker stop $SERVER_NAME
echo "------ 删除容器:$SERVER_NAME ------"
docker rm $SERVER_NAME
else
echo "------ 容器未在运行:${SERVER_NAME} ------"
echo "------ 删除容器:$SERVER_NAME ------"
docker rm $SERVER_NAME
fi
echo "------ docker run创建$SERVER_NAME容器 ------"
docker-compose -f /home/planetflix/compose/app.yml --compatibility up -d
echo "------ $SERVER_NAME容器创建完成 ------"
3.app.yml(docker-compose)
version: '3'
#统一管理服务
services:
#数据库服务
app:
container_name: planetflix-app
image: planetflix-app:1.0.0-SNAPSHOT
ports:
- 10021:10021
restart: always
networks:
- mid_network
volumes:
- /data/project/java/logs:/data/project/java/logs
- /data/project/java/planetflix-app.jar:/data/project/java/planetflix-app.jar
networks:
mid_network:
4.Java服务Dockerfile
# 使用 OpenJDK 17 的 slim 版本作为基础镜像
FROM openjdk:17-jdk-slim
MAINTAINER yangyanwei "yanwei_0818@163.com"
# 设置镜像的时区,避免出现8小时的误差
ENV TZ=America/New_York
# 合并 RUN 命令,清理不需要的缓存
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
tzdata \
; \
cp /usr/share/zoneinfo/$TZ /etc/localtime; \
echo "$TZ" > /etc/timezone; \
apt-get clean; \
rm -rf /var/lib/apt/lists/*
# 容器暴露的端口号和 Spring Boot 的 yml 文件暴露的端口号要一致
EXPOSE 10021
ENV JAVA_OPTS "-Xms512m -Xmx512m --add-opens java.base/java.lang=ALL-UNNAMED"
ENV SPRING_PROFILES_ACTIVE test
# 设置工作目录
WORKDIR /data/project/java
# 复制项目 JAR 文件
COPY target/planetflix-app.jar /data/project/java/planetflix-app.jar
# 使用 ENTRYPOINT 代替 CMD
CMD ["/bin/sh", "-c", "java $JAVA_OPTS -Dname=planetflix-app -Dfile.encoding=UTF-8 -jar /data/project/java/planetflix-app.jar --spring.profiles.active=$SPRING_PROFILES_ACTIVE"]
3.配置工程git访问,填写Jenkinsfile文件路径
4.配置远程服务器ssh访问
系统管理>系统配置>
可以通过生成ssh公钥密钥,远程连接ecs密码连接保存