diff --git a/.gitea/workflows/build-dev.yaml b/.gitea/workflows/build-dev.yaml new file mode 100644 index 0000000..2760728 --- /dev/null +++ b/.gitea/workflows/build-dev.yaml @@ -0,0 +1,109 @@ +name: paipai-h5-dev + +on: + push: + branches: [main] + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver-opts: | + network=host + buildkitd-flags: | + --allow-insecure-entitlement security.insecure + + - name: Log in to container registry + uses: docker/login-action@v3 + with: + registry: code.paipai.life + username: ${{ secrets.DEV_DOCKER_USERNAME }} + password: ${{ secrets.DEV_DOCKER_PASSWORD }} + insecure: true + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: code.paipai.life/frontend/rn-h5 + tags: | + type=raw,value=latest + + - name: Build Docker image + run: | + docker build -f Dockerfile-dev -t code.paipai.life/frontend/rn-h5:latest . + + - name: Push Docker image + run: | + docker push code.paipai.life/frontend/rn-h5:latest + env: + DOCKER_CONTENT_TRUST: 0 + + deploy: + runs-on: ubuntu-latest + needs: build-and-push + environment: test + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Copy files and deploy via SSH + uses: appleboy/scp-action@v0.1.3 + with: + host: ${{ secrets.DEV_SERVER_HOST }} + username: ${{ secrets.DEV_SERVER_USERNAME }} + password: ${{ secrets.DEV_SERVER_PASSWORD }} + port: 22 + source: "docker-compose-dev.yaml" + target: "/home/paipai/data/rn-h5" + override: true + + - name: Deploy with Docker Compose + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.DEV_SERVER_HOST }} + username: ${{ secrets.DEV_SERVER_USERNAME }} + password: ${{ secrets.DEV_SERVER_PASSWORD }} + port: 22 + script: | + cd /home/paipai/data/rn-h5 + + docker pull code.paipai.life/frontend/rn-h5:latest + + docker compose -f docker-compose-dev.yaml down + docker compose -f docker-compose-dev.yaml up -d + + echo "Deployment completed successfully!" + + cleanup: + runs-on: ubuntu-latest + needs: deploy + if: always() + + steps: + - name: Clean up Docker resources on server + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.DEV_SERVER_HOST }} + username: ${{ secrets.DEV_SERVER_USERNAME }} + password: ${{ secrets.DEV_SERVER_PASSWORD }} + port: 22 + script: | + docker image prune -f + docker container prune -f + docker builder prune -f + docker network prune -f + echo "Cleanup completed!" + + - name: Clean up GitHub Actions workspace + run: | + echo "Cleaning up GitHub Actions workspace..." + docker system df \ No newline at end of file diff --git a/.gitea/workflows/build-prod.yaml b/.gitea/workflows/build-prod.yaml new file mode 100644 index 0000000..30a19f5 --- /dev/null +++ b/.gitea/workflows/build-prod.yaml @@ -0,0 +1,111 @@ +name: rn-h5-prod + +on: + push: + tags: ['v*'] + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + driver-opts: | + network=host + buildkitd-flags: | + --allow-insecure-entitlement security.insecure + + - name: Log in to container registry + uses: docker/login-action@v3 + with: + registry: 626064810415.dkr.ecr.us-west-1.amazonaws.com + username: ${{ secrets.PROD_DOCKER_USERNAME }} + password: ${{ secrets.PROD_DOCKER_PASSWORD }} + + - name: Extract tag name + id: tag + run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT + + - name: Build Docker image + run: | + docker build -t 626064810415.dkr.ecr.us-west-1.amazonaws.com/raveai/rn-h5:${{ steps.tag.outputs.TAG }} . + + - name: Push Docker image + run: | + docker push 626064810415.dkr.ecr.us-west-1.amazonaws.com/raveai/rn-h5:${{ steps.tag.outputs.TAG }} + env: + DOCKER_CONTENT_TRUST: 0 + + deploy: + runs-on: ubuntu-latest + needs: build-and-push + environment: test + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Extract tag name + id: tag + run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT + + - name: Copy files and deploy via SSH + uses: appleboy/scp-action@v0.1.3 + with: + host: ${{ secrets.PROD_SERVER_HOST }} + username: ${{ secrets.PROD_SERVER_USERNAME }} + key: ${{ secrets.PROD_SERVER_KEY }} + port: 22 + source: "docker-compose.yaml" + target: "/home/ubuntu/docker-compose/rn-h5" + override: true + + - name: Deploy with Docker Compose + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.PROD_SERVER_HOST }} + username: ${{ secrets.PROD_SERVER_USERNAME }} + key: ${{ secrets.PROD_SERVER_KEY }} + port: 22 + script: | + # 切换到项目目录 + cd /home/ubuntu/docker-compose/rn-h5 + + aws ecr get-login-password --region us-west-1 | docker login --username AWS --password-stdin 626064810415.dkr.ecr.us-west-1.amazonaws.com + + # 拉取指定tag的镜像 + docker pull 626064810415.dkr.ecr.us-west-1.amazonaws.com/raveai/rn-h5:${{ steps.tag.outputs.TAG }} + + TAG=${{ steps.tag.outputs.TAG }} docker compose down + TAG=${{ steps.tag.outputs.TAG }} docker compose up -d + + + cleanup: + runs-on: ubuntu-latest + needs: deploy + if: always() + + steps: + - name: Clean up Docker resources on server + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.PROD_SERVER_HOST }} + username: ${{ secrets.PROD_SERVER_USERNAME }} + key: ${{ secrets.PROD_SERVER_KEY }} + port: 22 + script: | + docker image prune -f + docker container prune -f + docker builder prune -f + docker network prune -f + echo "Cleanup completed!" + + - name: Clean up GitHub Actions workspace + run: | + echo "Cleaning up GitHub Actions workspace..." + docker system df \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b4696b6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM node:20-alpine AS build + +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm install +COPY . . +RUN npm run build:h5 + + +FROM nginx:stable-alpine AS production + +COPY --from=build /app/dist/build/h5 /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/Dockerfile-dev b/Dockerfile-dev new file mode 100644 index 0000000..026e4be --- /dev/null +++ b/Dockerfile-dev @@ -0,0 +1,16 @@ +FROM node:20-alpine AS build + +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm install +COPY . . +RUN npm run build:h5 + + +FROM nginx:stable-alpine AS production + +COPY --from=build /app/dist/build/h5 /usr/share/nginx/html +COPY nginx.dev.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/docker-compose-dev.yaml b/docker-compose-dev.yaml new file mode 100644 index 0000000..52e1c8e --- /dev/null +++ b/docker-compose-dev.yaml @@ -0,0 +1,13 @@ +services: + web: + image: code.paipai.life/frontend/rn-h5 + container_name: rn-h5 + networks: + - app-net + ports: + - "8300:80" + restart: unless-stopped + +networks: + app-net: + external: true \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..8ea7b10 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,13 @@ +services: + web: + image: 626064810415.dkr.ecr.us-west-1.amazonaws.com/raveai/rn-h5:${TAG} + container_name: rn-h5 + networks: + - rn-network + ports: + - "8300:80" + restart: unless-stopped + +networks: + rn-network: + external: true \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..afa81fa --- /dev/null +++ b/nginx.conf @@ -0,0 +1,41 @@ +server { + listen 80; + server_name h5.paipai.life; + + root /usr/share/nginx/html; + index index.html; + + # 设置客户端请求体超时时间 + client_body_timeout 300s; + + # API 代理配置 - 对应 proxy.ts 中的配置 + location /api/v1/ { + proxy_pass https://rn-master:8088/api/v1/; # 根据实际后端服务地址调整 + 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; + + # 代理超时设置 + proxy_connect_timeout 300s; + proxy_send_timeout 300s; + proxy_read_timeout 300s; + + # 代理缓冲区设置 + proxy_buffering off; + proxy_request_buffering off; + } + + # 单页应用路由配置 + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + # 静态资源缓存配置 + location /static { + expires 7d; + add_header Cache-Control "public, no-transform"; + } +} diff --git a/nginx.dev.conf b/nginx.dev.conf new file mode 100644 index 0000000..b873593 --- /dev/null +++ b/nginx.dev.conf @@ -0,0 +1,41 @@ +server { + listen 80; + server_name h5.dev.paipai.life; + + root /usr/share/nginx/html; + index index.html; + + # 设置客户端请求体超时时间 + client_body_timeout 300s; + + # API 代理配置 - 对应 proxy.ts 中的配置 + location /api/v1/ { + proxy_pass https://rn-master:8088/api/v1/; # 根据实际后端服务地址调整 + 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; + + # 代理超时设置 + proxy_connect_timeout 300s; + proxy_send_timeout 300s; + proxy_read_timeout 300s; + + # 代理缓冲区设置 + proxy_buffering off; + proxy_request_buffering off; + } + + # 单页应用路由配置 + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + # 静态资源缓存配置 + location /static { + expires 7d; + add_header Cache-Control "public, no-transform"; + } +}