diff --git a/Jenkinsfile b/Jenkinsfile index 8275df4..5838138 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,13 +1,10 @@ pipeline { - agent { - docker { - image 'node:18-alpine' // 使用包含 Node.js 的 Docker 镜像 - } - } + agent any + environment { // 环境变量定义 PROJECT_NAME = 'timeline-frontend' - REGISTRY = 'timeline-registry:5000' + DOCKER_REGISTRY = 'timeline-registry:5000' DOCKER_IMAGE = "${DOCKER_REGISTRY}/${PROJECT_NAME}" } @@ -39,12 +36,21 @@ pipeline { echo "当前构建的 Git Commit: ${env.GIT_COMMIT}" } } + stage('Build timeline-frontend dist') { + agent { + label 'docker-agent' // 假设有一个安装了 Docker 和 Node.js 的节点 + } steps { script { - // 确保先执行前端构建命令 - sh 'npm install' - sh 'npm run build' // 这会生成 dist 目录 + // 使用 Docker 运行 Node.js 环境 + sh ''' + docker run -v $PWD:/app -w /app node:18-alpine sh -c " + npm install && + npm run build + " + ''' + // 验证构建产物 sh 'ls -la dist/' } @@ -56,6 +62,37 @@ pipeline { script { def imageTag = "${BUILD_NUMBER}-${env.GIT_COMMIT.take(7)}" env.IMAGE_TAG = imageTag + + // 创建一个简单的 nginx 配置(可选) + sh ''' + cat > nginx.conf << 'EOF' + events { + worker_connections 1024; + } + + http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + server { + listen 80; + server_name localhost; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + } + } + EOF + ''' + sh """ docker build -t ${DOCKER_IMAGE}:${imageTag} . docker tag ${DOCKER_IMAGE}:${imageTag} ${DOCKER_IMAGE}:latest @@ -188,7 +225,7 @@ def deployToEnvironment(String env) { docker run -d \ --name ${containerName} \ --restart unless-stopped \ - -p ${getPortForEnvironment(env)}:3000 \ + -p ${getPortForEnvironment(env)}:80 \ ${imageToDeploy} """ @@ -210,8 +247,8 @@ def getPortForEnvironment(String env) { case 'staging': return '3002' case 'prod': - return '3000' + return '80' default: - return '3000' + return '80' } }