jenkins+docker部署springboot+vue项目

发布于:2024-12-20 ⋅ 阅读:(20) ⋅ 点赞:(0)

后端的Dockerfile和docker启动脚本

FROM openjdk:17

COPY . /home/ProjectSpace/cli/extranet/server

COPY simsun.ttf /usr/share/fonts/chinese/simsun.ttf

WORKDIR /home/ProjectSpace/cli/extranet/server

CMD java -Dspring.config.location=/home/ProjectSpace/cli/extranet/server/config/ -jar /home/ProjectSpace/cli/extranet/server/extranet-0.0.1-SNAPSHOT.jar
#! /bin/bash
echo '搭建关机服务平台---后端服务'

# 从Jenkinsfile所在目录进入Dockerfile所在目录
cd /home/ProjectSpace/cli/extranet/server/

# 编译 cli-extranet-server镜像
docker build --tag cli-extranet-server:latest .
# 关掉 cli-extranet-server(服务)
echo '关掉 旧cli-extranet-server'
docker stop cli-extranet-server
# 移除 cli-extranet-server(服务)
echo '移除 旧cli-extranet-server'
docker rm cli-extranet-server
# 运行镜像
echo '运行新镜像cli-extranet-server'
docker run -d --name cli-extranet-server -p 9002:9002 -v /etc/localtime:/etc/localtime:ro -v /home/ProjectSpace/cli/extranet/server/logs:/home/ProjectSpace/cli/extranet/server/logs -v /home/ProjectSpace/cli/extranet/server/lib:/home/ProjectSpace/cli/extranet/server/lib -e TZ=Asia/Shanghai cli-extranet-server:latest 


docker ps | head  # 查看正在运行的容器列表(只看前面几个)

jenkins后端流水线脚本

pipeline {
    agent any
 
    // 禁用默认的代码检出
    options {
        skipDefaultCheckout() 
    }

    stages {
        stage('拉取代码') {
            steps {
                echo '拉取代码Hello World'
                checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: '455cd43b-fd1a-4a7a-b8aa-218f35facaac', url: 'http://192.168.1.42:8090/cli/digital_police_tation_server.git']])
                script {
                    def commitMessage = sh(returnStdout: true, script: 'git log --format=%B -n 1').trim()
                    echo commitMessage
					echo "触发构建"
                }
            }
        }
         stage('代码打包') {
            steps {
                echo '代码打包并移动到指定目录'
                withEnv(["PATH+MAVEN=/home/maven/bin"]) {
					sh "chmod -R +x /home/maven/bin"
                    sh "mvn -Dmaven.test.skip=true clean package"
                    sh "mv target/*.jar /home/ProjectSpace/cli/digital/server/"
                }
            }
        }
        stage('打包镜像并部署') {
            steps {
                echo '执行 build_docker.sh 脚本'
                sh "chmod +x /home/ProjectSpace/cli/digital/server/build_docker.sh"
                sh "/home/ProjectSpace/cli/digital/server/build_docker.sh"
            }
        }
    }
}

jenkins前端流水线脚本

pipeline {
    agent any

    
    // 禁用默认的代码检出
    options {
        skipDefaultCheckout() 
    }


    stages {
        stage('拉取代码') {
            steps {
                echo '拉取代码'
                checkout scmGit(branches: [[name: '*/nei_dev']], extensions: [], userRemoteConfigs: [[credentialsId: '455cd43b-fd1a-4a7a-b8aa-218f35facaac', url: 'http://192.168.1.42:8090/cli/cooperative_web.git']])
                
                script {
                    def commitMessage = sh(returnStdout: true, script: 'git log --format=%B -n 1').trim()
                    echo commitMessage
					echo "触发构建"
                }
            }
        }
        
        stage('安装依赖') {
            steps {
                echo '安装依赖'
                dir('/home/jenkins/home_work/workspace/cooperative_web/') {
                    sh 'npm install --registry=https://registry.npmmirror.com/'
                }
            }
        }
        
        stage('构建项目') {
            steps {
                echo '构建项目'
                dir('/home/jenkins/home_work/workspace/cooperative_web/') {
					sh 'rm -rf dist/*'
                    sh 'npm run build'
                }
            }
        }
        
        stage('部署') {
            steps {
                echo '部署'
                 // 移除原来的包
                dir('/home/ProjectSpace/cli/cooperative_resource/web/') {
                    sh 'rm -rf ./*'
                }
				dir('/home/ProjectSpace/cli_nei/cooperative_resource/web/') {
                    sh 'rm -rf ./*'
                }
                // 将构建产物复制到指定目录
                dir('/home/jenkins/home_work/workspace/cooperative_web/') {
                    sh 'cp -R /home/jenkins/home_work/workspace/cooperative_web/dist /home/ProjectSpace/cli/cooperative_resource/web/'
                    sh 'cp -R /home/jenkins/home_work/workspace/cooperative_web/dist /home/ProjectSpace/cli_nei/cooperative_resource/web/'
                }
            }
        }
    }
}