feat: 添加 Dockerfile、docker-compose 和 nginx 配置以支持前后端构建和部署
This commit is contained in:
1 parent
0d78e4c25d
commit
7309520063
5 files changed
+88
-64
No files matched your search
Vendored
+10
-64
@@ -1,14 +1,6 @@
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
tools {
|
||||
nodejs 'nodejs'
|
||||
}
|
||||
|
||||
environment {
|
||||
DEPLOY_DIR = '/opt/1panel/www/sites/workstation.xiaozhou233.cn/index'
|
||||
}
|
||||
|
||||
options {
|
||||
timestamps()
|
||||
disableConcurrentBuilds()
|
||||
@@ -30,66 +22,21 @@ environment {
|
||||
}
|
||||
}
|
||||
|
||||
stage('Install') {
|
||||
stage('Build Images') {
|
||||
steps {
|
||||
// Install frontend dependencies
|
||||
dir('dashboard') {
|
||||
sh 'npm ci'
|
||||
script {
|
||||
env.BUILD_TIME = sh(script: "date '+%Y-%m-%d %H:%M:%S'", returnStdout: true).trim()
|
||||
env.COMMIT_SHORT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Build') {
|
||||
steps {
|
||||
// Build frontend
|
||||
dir('dashboard') {
|
||||
sh 'npm run build'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stage('Version') {
|
||||
steps {
|
||||
dir('dashboard') {
|
||||
sh '''
|
||||
cat > dist/version.json <<EOF
|
||||
{
|
||||
"buildNumber": "${BUILD_NUMBER}",
|
||||
"commit": "$(git rev-parse --short HEAD)",
|
||||
"branch": "${GIT_BRANCH}",
|
||||
"time": "$(date '+%Y-%m-%d %H:%M:%S')"
|
||||
}
|
||||
EOF
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stage('Package') {
|
||||
steps {
|
||||
// Package frontend
|
||||
sh 'zip -r dashboard.zip dashboard/dist'
|
||||
}
|
||||
}
|
||||
|
||||
stage('Build Backend Image') {
|
||||
steps {
|
||||
// Build backend Docker image
|
||||
sh 'docker build -t workstation-server:${BUILD_NUMBER} backend/'
|
||||
sh 'docker compose build backend frontend'
|
||||
}
|
||||
}
|
||||
|
||||
stage('Deploy') {
|
||||
steps {
|
||||
sh '''
|
||||
echo "Deploying to ${DEPLOY_DIR}"
|
||||
|
||||
mkdir -p ${DEPLOY_DIR}
|
||||
|
||||
rsync -av --delete dashboard/dist/ ${DEPLOY_DIR}/
|
||||
|
||||
echo "Deploy completed"
|
||||
docker compose up -d
|
||||
docker compose ps
|
||||
'''
|
||||
}
|
||||
}
|
||||
@@ -98,16 +45,15 @@ EOF
|
||||
|
||||
post {
|
||||
success {
|
||||
archiveArtifacts artifacts: 'dashboard.zip', fingerprint: true
|
||||
echo 'Build succeeded'
|
||||
echo 'Build and deploy succeeded'
|
||||
}
|
||||
|
||||
failure {
|
||||
echo 'Build failed'
|
||||
echo 'Build or deploy failed'
|
||||
}
|
||||
|
||||
always {
|
||||
echo 'Pipeline finished'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
dist
|
||||
@@ -0,0 +1,22 @@
|
||||
# ---- 构建阶段 ----
|
||||
FROM node:22-alpine AS build
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci
|
||||
|
||||
COPY . .
|
||||
RUN npm run build
|
||||
|
||||
ARG BUILD_NUMBER="latest"
|
||||
ARG COMMIT="unknown"
|
||||
ARG BRANCH="unknown"
|
||||
ARG BUILD_TIME="unknown"
|
||||
RUN node -e "const fs=require('fs');fs.writeFileSync('dist/version.json',JSON.stringify({buildNumber:process.env.BUILD_NUMBER||'',commit:process.env.COMMIT||'',branch:process.env.BRANCH||'',time:process.env.BUILD_TIME||''}))"
|
||||
|
||||
# ---- 运行阶段 ----
|
||||
FROM nginx:alpine
|
||||
COPY --from=build /app/dist /usr/share/nginx/html
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
EXPOSE 80
|
||||
@@ -0,0 +1,29 @@
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
gzip on;
|
||||
gzip_types text/css application/javascript application/json image/svg+xml;
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://backend:9018;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|woff2?)$ {
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, immutable";
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
services:
|
||||
backend:
|
||||
build:
|
||||
context: ./backend
|
||||
image: workstation-server:${BUILD_NUMBER:-latest}
|
||||
container_name: workstation-server
|
||||
restart: unless-stopped
|
||||
expose:
|
||||
- "9018"
|
||||
|
||||
frontend:
|
||||
build:
|
||||
context: ./dashboard
|
||||
args:
|
||||
BUILD_NUMBER: ${BUILD_NUMBER:-latest}
|
||||
COMMIT: ${COMMIT_SHORT:-unknown}
|
||||
BRANCH: ${GIT_BRANCH:-unknown}
|
||||
BUILD_TIME: ${BUILD_TIME:-unknown}
|
||||
image: workstation-dashboard:${BUILD_NUMBER:-latest}
|
||||
container_name: workstation-dashboard
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
- backend
|
||||
ports:
|
||||
- "9019:80"
|
||||
Reference in new issue
Block a user