使用Docker部署jenkins实现自动化部署ruoyi-vue
- [docker jenkins](#docker jenkins)
- [Java jenkinsfile](#Java jenkinsfile)
- [vue jenkinsfile](#vue jenkinsfile)
- [Dockerfile 部署脚本](#Dockerfile 部署脚本)
-
- [Java Dockerfile](#Java Dockerfile)
-
- [nginx Dockerfile](#nginx Dockerfile)
- nginx-dev.conf
使用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;
}
}
}