使用Docker 部署jenkins 实现自动化部署

使用Docker部署jenkins实现自动化部署ruoyi-vue

  • [docker jenkins](#docker jenkins)
  • [Java jenkinsfile](#Java jenkinsfile)
  • [vue jenkinsfile](#vue jenkinsfile)
  • [Dockerfile 部署脚本](#Dockerfile 部署脚本)
    • [Java Dockerfile](#Java Dockerfile)

使用docker部署Jenkins,项目: https://gitee.com/y_project/RuoYi-Vue 作为部署项目示范

docker jenkins

shell 复制代码
docker run  \
--name jenkins   \
-d   \
-u root   -p 58080:8080 -p 50000:50000   \
-e TZ="Asia/Shanghai" \
-v /etc/localtime:/etc/localtime:ro   \
-v /var/run/docker.sock:/var/run/docker.sock   \
-v  /mydata/myjenkins/home:/home  \
-v /mydata/myjenkins/jenkins_home:/var/jenkins_home    \
jenkinsci/blueocean

mkdir -p /mydata/myjenkins/jenkins_home
mkdir -p /mydata/myjenkins/home

sudo chown -R 1000:1000 /mydata/myjenkins/home
sudo chown -R 1000:1000 /mydata/myjenkins/jenkins_home

# 将最新Jar copy 容器
docker cp /mydata/jenkins.war myjenkins:/usr/share/jenkins/jenkins.war

# 配置加速
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

Java jenkinsfile

jenkinsfile

sh 复制代码
import java.text.SimpleDateFormat
node {
    try {
        //名字这么写是为了可以发布到腾讯docker仓库,可随意更改
        def dockerId='tengxun'
        def dockerUrl='registry.cn-hangzhou.aliyuncs.com'
        def dockerNamespace='smartdress'
        def dockerName='smartdress-ht'
        def env='dev'
        
        def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
        def dockerTag = dateFormat.format(new Date())
        
        // maven Home
        def mvnHome
        stage('git clone'){
            sh 'pwd'
            git (credentialsId: '005', url: 'https://gitee.com/xxx/xxx.git',  branch: "dev", changelog: true)
        }

        stage('mvn package') {
            sh 'pwd'
            docker.image('maven:3.6.0-jdk-8-alpine').inside('-v /mydata/maven/m2:/root/.m2 -v /mydata/maven/settings.xml:/usr/share/maven/ref/settings.xml') {
                sh 'java -version'
                sh 'mvn --version'
                sh 'mvn clean install -Dmavan.test.skip=true'
            }
        }
        stage('docker run') {
			sh 'pwd'
			def imageUrl = "${dockerUrl}/${dockerNamespace}/${dockerName}:${dockerTag}"
			def customImage = docker.build(imageUrl)
			
			sh "docker rm -f ${dockerName} | true"
			// 创建相关网络
			// docker network create ruoyi
			// docker network connect ruoyi redis
			// docker network connect ruoyi mysql
			// docker network connect ruoyi ruoyi
			customImage.run("-it -d --name ${dockerName} -p 9898:9898 --network ruoyi")
			//only retain last 3 images,自动删除老的容器,只保留最近3个
			sh """docker rmi \$(docker images | grep ${dockerName} | sed -n  '3,\$p' | awk '{print \$2}') || true"""
        }
        currentBuild.result="SUCCESS"
    } catch (e) {
        currentBuild.result="FAILURE"
        throw e
    } finally {
        //此处若想发布邮件,需要在系统管理-系统设置中配置邮件服务器
        //mail to: 'xxxx@qq.com',subject: "Jenkins: ${currentBuild.fullDisplayName},${currentBuild.result}",body:"${currentBuild.result}"
    }
}

vue jenkinsfile

shell 复制代码
import java.text.SimpleDateFormat
node {
    try {
        // 名字这么写是为了可以发布到腾讯docker仓库,可随意更改
        def dockerId='tengxun'
        def dockerUrl='registry.cn-hangzhou.aliyuncs.com'
        def dockerNamespace='amewin'
        def dockerName='ruoyi-vue'
        def env='dev'
        
        def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
        def dockerTag = dateFormat.format(new Date())
        
        stage('git clone'){
            sh 'pwd'
            git (credentialsId: '005', url: 'https://gitee.com/xxx/xxx.git',  branch: "0324", changelog: true)
        }
        
        stage('Node Install') {
             sh 'ls'
            dir('./ruoyi-ui') {
                sh 'pwd'
                docker.image('node:16.20.2-alpine').inside('-v /mydata/node:/root/.npm/') {
    
                    sh 'pwd'
                    sh "node -v"
                    sh "npm -v"
    				// 设置sass下载地址
    				sh 'npm config set registry http://registry.npm.taobao.org/'
                    sh "npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/"

                    sh 'npm install --registry=https://registry.npmmirror.com'
                    sh "npm run build:prod"
                    sh "tar -zcvf dist.tar.gz dist"
                    stash name: "dist", includes: "dist.tar.gz"
                    
                    sh "echo '<---ok--->'"
                }
            }
        }
        
        stage('Docker deploy') {
            dir("ruoyi-ui") {
                sh 'pwd'
                def imageUrl = "${dockerUrl}/${dockerNamespace}/${dockerName}:${dockerTag}"
                def customImage = docker.build(imageUrl)
                
                sh 'echo ${imageUrl}'

                sh "docker rm -f ${dockerName} | true"

                customImage.run("-it -d --name ${dockerName} -p 3000:80 --network ruoyi ")

                //only retain last 3 images,自动删除老的容器,只保留最近3个
                sh """docker rmi \$(docker images | grep ${dockerName} | sed -n  '4,\$p' | awk '{print \$3}') || true"""
            }
     
        }
        currentBuild.result="SUCCESS"
    } catch (e) {
        currentBuild.result="FAILURE"
        throw e
    } finally {
        //此处若想发布邮件,需要在系统管理-系统设置中配置邮件服务器
        //mail to: 'xxxx@qq.com',subject: "Jenkins: ${currentBuild.fullDisplayName},${currentBuild.result}",body:"${currentBuild.result}"
    }
}

Dockerfile 部署脚本

Java Dockerfile

sh 复制代码
FROM java:8

MAINTAINER Mr.Chen zsdfcc@163.com

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && mkdir -p /ruoyi

WORKDIR /ruoyi

EXPOSE 9898

ADD ./ruoyi-admin/target/ruoyi-admin.jar /ruoyi/ruoyi-admin.jar

CMD java -jar -Xmx512M -Xms256M  -Dfile.encoding=UTF8   -Duser.timezone=GMT+08 -Dspring.profiles.active=jenkins  ruoyi-admin.jar

nginx Dockerfile

shell 复制代码
FROM nginx:latest

# 传参数据
# ARG PROFILE

# 将dist文件中的内容复制到 `/usr/share/nginx/html/` 这个目录下面
COPY dist/ /usr/share/nginx/html/
# 用本地配置文件来替换nginx镜像里的默认配置
COPY nginx-dev.conf /etc/nginx/nginx.conf

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx-dev.conf

sh 复制代码
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 80;
        server_name vue.xxx.xyz;

        charset utf-8;
        client_max_body_size 20m;

        location / {
            root /usr/share/nginx/html;
            try_files $uri $uri/ /index.html;
            index index.html index.htm;
        }

        location ~ .*\.(gif|jpg|jpeg|png)$ {
            expires 24h;
            root /usr/share/nginx/uploadPath/upload/img;#指定图片存放路径
            # access_log /www/server/nginx/logs/images.log;#图片 日志路径
            proxy_store on;
            proxy_store_access user:rw group:rw all:rw;
            proxy_temp_path /usr/share/nginx/uploadPath;#代理临时路径
            proxy_redirect off;

            proxy_set_header Host smartdress-ht;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size 10m;
            client_body_buffer_size 1280k;
            proxy_connect_timeout 900;
            proxy_send_timeout 900;
            proxy_read_timeout 900;
            proxy_buffer_size 40k;
            proxy_buffers 40 320k;
            proxy_busy_buffers_size 640k;
            proxy_temp_file_write_size 640k;
            if ( !-e $request_filename) {
                proxy_pass http://smartdress-ht:9898;#代理访问地址
            }
        }

        location /prod-api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://smartdress-ht:9898/;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            # root html;
            root /usr/share/nginx/html;
        }
    }
}
相关推荐
铁锤妹妹头发多1 小时前
新手用docker真**难受
运维·docker·容器
徐浪老师1 小时前
OSPF动态路由配置实验:实现高效网络自动化
网络·自动化·智能路由器
娃哈哈_3 小时前
基于Testng + Playwright的H5自动化巡检工具
测试开发·测试工具·自动化·html5·可用性测试·testng·playwright
2401_857600953 小时前
SpringBoot框架的企业资产管理自动化
spring boot·后端·自动化
z千鑫3 小时前
【人工智能】利用大语言模型(LLM)实现机器学习模型选择与实验的自动化
人工智能·gpt·机器学习·语言模型·自然语言处理·自动化·codemoss
zhd15306915625ff3 小时前
化工厂主要涉及的自动化备件有哪些?
运维·自动化·化工厂
Jason-河山3 小时前
利用API返回值实现商品信息自动化更新:技术与实践
运维·自动化
_.Switch8 小时前
高级Python自动化运维:容器安全与网络策略的深度解析
运维·网络·python·安全·自动化·devops
南猿北者10 小时前
docker容器
docker·容器
二十雨辰11 小时前
[linux]docker基础
linux·运维·docker