From 8404f4f1cf36d1d9536592f7d6466ad26dc8f2a3 Mon Sep 17 00:00:00 2001 From: KaGaMi_PC Date: Sat, 12 Jul 2025 12:45:33 +0800 Subject: [PATCH] t2 --- .dockerignore | 12 +++++++ .gitea/workflows/test.yml | 76 +++++++++++++++++++++++++++++++++------ Dockerfile | 20 +++++++++++ app.js | 40 +++++++++++++++++++++ package.json | 16 +++++++++ 5 files changed, 154 insertions(+), 10 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 app.js create mode 100644 package.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..36e610d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,12 @@ +node_modules +npm-debug.log +.git +.gitignore +README.md +.env +.nyc_output +coverage +.vscode +.idea +*.log +.DS_Store \ No newline at end of file diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index 756cd08..106059b 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -23,15 +23,71 @@ jobs: - name: 设置Docker构造环境 uses: docker/setup-buildx-action@v3 - - name: 构造Node.js Hello World程序 + - name: 登录到Gitea Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ gitea.server_url }} + username: ${{ gitea.actor }} + password: ${{ gitea.token }} + + - name: 构造Docker镜像 run: | - echo "🐳 构造Node.js Hello World程序..." - cat << 'EOF' > Dockerfile - FROM node:18-alpine - WORKDIR /app - RUN echo '{"name": "hello-world", "version": "1.0.0", "main": "app.js"}' > package.json - RUN echo 'console.log("Hello World from Node.js Docker! 🎉")' > app.js - CMD ["node", "app.js"] - EOF - docker build -t nodejs-hello-world:latest . + echo "🐳 构造Node.js Hello World应用镜像..." + + # 设置镜像标签 + IMAGE_NAME="${{ gitea.server_url }}/${{ gitea.repository_owner }}/hello-world-app" + IMAGE_TAG="latest" + COMMIT_SHA="${{ gitea.sha }}" + + echo "📦 镜像名称: $IMAGE_NAME" + echo "🏷️ 镜像标签: $IMAGE_TAG" + echo "📋 提交SHA: $COMMIT_SHA" + + # 构造镜像 + docker build -t "$IMAGE_NAME:$IMAGE_TAG" -t "$IMAGE_NAME:$COMMIT_SHA" . + echo "✅ Node.js Hello World镜像构造完成!" + + - name: 推送Docker镜像 + run: | + echo "🚀 推送Docker镜像到Gitea registry..." + + # 设置镜像标签 + IMAGE_NAME="${{ gitea.server_url }}/${{ gitea.repository_owner }}/hello-world-app" + IMAGE_TAG="latest" + COMMIT_SHA="${{ gitea.sha }}" + + # 推送镜像 + docker push "$IMAGE_NAME:$IMAGE_TAG" + docker push "$IMAGE_NAME:$COMMIT_SHA" + + echo "✅ Docker镜像推送完成!" + echo "🎉 镜像已推送到: $IMAGE_NAME:$IMAGE_TAG" + echo "🎉 镜像已推送到: $IMAGE_NAME:$COMMIT_SHA" + + - name: 测试镜像运行 + run: | + echo "🧪 测试构造的镜像..." + + IMAGE_NAME="${{ gitea.server_url }}/${{ gitea.repository_owner }}/hello-world-app" + IMAGE_TAG="latest" + + # 运行镜像进行测试 + docker run -d --name test-container -p 3000:3000 "$IMAGE_NAME:$IMAGE_TAG" + + # 等待容器启动 + sleep 5 + + # 测试健康检查 + echo "🔍 测试应用健康状态..." + curl -f http://localhost:3000/health || exit 1 + + # 测试主页 + echo "🔍 测试应用主页..." + curl -f http://localhost:3000/ || exit 1 + + # 停止并删除测试容器 + docker stop test-container + docker rm test-container + + echo "✅ 镜像测试完成!" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2bfe630 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# 使用官方Node.js运行时作为基础镜像 +FROM node:18-alpine + +# 设置工作目录 +WORKDIR /app + +# 复制package.json文件 +COPY package*.json ./ + +# 安装应用依赖 +RUN npm install --only=production + +# 复制应用源代码 +COPY . . + +# 暴露端口 +EXPOSE 3000 + +# 定义容器启动时运行的命令 +CMD ["npm", "start"] \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..186b918 --- /dev/null +++ b/app.js @@ -0,0 +1,40 @@ +const express = require('express'); +const app = express(); +const port = process.env.PORT || 3000; + +// 基本的Hello World路由 +app.get('/', (req, res) => { + res.json({ + message: 'Hello World from Node.js Docker! 🎉', + timestamp: new Date().toISOString(), + version: '1.0.0' + }); +}); + +// 健康检查路由 +app.get('/health', (req, res) => { + res.json({ + status: 'healthy', + uptime: process.uptime(), + timestamp: new Date().toISOString() + }); +}); + +// 启动服务器 +app.listen(port, '0.0.0.0', () => { + console.log(`🚀 Hello World 应用启动成功!`); + console.log(`📡 服务器运行在 http://0.0.0.0:${port}`); + console.log(`🎯 访问 http://localhost:${port} 查看Hello World`); + console.log(`💚 访问 http://localhost:${port}/health 查看健康状态`); +}); + +// 优雅关闭 +process.on('SIGTERM', () => { + console.log('📴 收到SIGTERM信号,正在关闭服务器...'); + process.exit(0); +}); + +process.on('SIGINT', () => { + console.log('📴 收到SIGINT信号,正在关闭服务器...'); + process.exit(0); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f1a8ca4 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "hello-world-app", + "version": "1.0.0", + "description": "简单的Node.js Hello World应用", + "main": "app.js", + "scripts": { + "start": "node app.js", + "dev": "node app.js" + }, + "keywords": ["hello", "world", "nodejs", "docker"], + "author": "Test User", + "license": "MIT", + "dependencies": { + "express": "^4.18.2" + } +} \ No newline at end of file