From 5a3f2dd8adef40007179732a0afcbdb556da00db Mon Sep 17 00:00:00 2001 From: phattt2901 Date: Mon, 5 May 2025 20:59:27 +0700 Subject: [PATCH] Test CI v2.1.41 --- .gitea/workflows/ci.yml | 85 ++++++++++++++++++++++++++++++++++++++++- Dockerfile.prod | 25 ++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 Dockerfile.prod diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 0b25423..dab90bf 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -115,11 +115,92 @@ jobs: APP_VERSION="${{ gitea.ref_name }}-${{ format('{0}', gitea.sha) }}" # Thay main.version bằng đường dẫn thực tế đến biến version trong code của bạn go build -v -ldflags="-s -w -X main.version=${APP_VERSION}" -o ./bin/server ./cmd/server - # Step 6: Upload artifact binary # Step 2: Cập nhật action versions - name: Upload build artifact uses: actions/upload-artifact@v3 with: name: server-binary # Tên artifact rõ ràng hơn - path: ./bin/server # Đường dẫn tới file binary \ No newline at end of file + path: ./bin/server # Đường dẫn tới file binary + # ---- Job: Build & Push Docker Image ---- + docker-build-push: + name: Build and Push Docker Image + runs-on: ubuntu-latest + # Chạy sau khi job 'build' (tạo binary) thành công + needs: [build] + # Chỉ chạy khi push vào nhánh master (không chạy cho PR) + # Kiểm tra lại biến context của Gitea Actions, có thể là 'gitea.ref_name' hoặc 'github.ref_name' + if: github.ref_name == 'master' || gitea.ref_name == 'master' + steps: + - name: Checkout code # Cần checkout để lấy Dockerfile.prod + uses: actions/checkout@v4 + + - name: Download build artifact # Tải artifact binary từ job 'build' + uses: actions/download-artifact@v3 # Dùng v3 cho Gitea + with: + name: server-binary # Tên artifact đã upload ở job build + path: ./bin # Giải nén vào thư mục ./bin + + - name: Set up QEMU # Cần cho build đa kiến trúc (tùy chọn) + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx # Công cụ build image nâng cao + uses: docker/setup-buildx-action@v3 + + - name: Login to Gitea Container Registry # Đăng nhập vào registry + uses: docker/login-action@v3 + with: + registry: ${{ gitea.server_url }} # Biến context chứa URL Gitea (vd: gitea.tuvanwebsite.com) - Kiểm tra lại biến đúng + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . # Build context là thư mục gốc + file: ./Dockerfile.prod # Chỉ định Dockerfile cho production + push: true # Đẩy image lên registry + tags: | # Đặt tên và tag cho image + ${{ gitea.server_url }}/${{ gitea.repository }}:${{ gitea.sha }} # Tag bằng commit SHA (duy nhất) + ${{ gitea.server_url }}/${{ gitea.repository }}:latest # Tag là latest + # cache-from: type=gha # Bật cache build layer (tùy chọn) + # cache-to: type=gha,mode=max + # ---- Job: Deploy to VPS ---- + deploy: + name: Deploy to VPS + runs-on: ubuntu-latest # Job này phải chạy trên runner có quyền truy cập Docker socket của host + # Chạy sau khi build và push image thành công + needs: [docker-build-push] + # Chỉ chạy khi push vào nhánh master + if: github.ref_name == 'master' || gitea.ref_name == 'master' + steps: + # Lấy thông tin image vừa push + # Lưu ý: Kiểm tra lại biến context chính xác của Gitea Actions + - name: Set image name + run: echo "IMAGE_NAME=${{ gitea.server_url }}/${{ gitea.repository }}:${{ gitea.sha }}" >> $GITHUB_ENV + + - name: Deploy using Docker commands + run: | + echo "Deploying image: ${{ env.IMAGE_NAME }}" + # Kéo image mới nhất về VPS host (thông qua runner) + docker pull ${{ env.IMAGE_NAME }} + + # Dừng container cũ nếu đang chạy (bỏ qua lỗi nếu chưa có) + docker stop my-go-app-container || true + + # Xóa container cũ nếu tồn tại (bỏ qua lỗi nếu chưa có) + docker rm my-go-app-container || true + + # Chạy container mới từ image vừa kéo + # Quan trọng: Thêm các biến môi trường cần thiết cho ứng dụng (-e) + # và đảm bảo nó kết nối đúng network để thấy DB (-network) + docker run -d \ + --name my-go-app-container \ + --network gitea_network \ + --restart always \ + -p 8080:8080 \ + -e DB_HOST=db \ + -e DB_USER=gitea \ + -e DB_PASSWORD=gitea \ + -e DB_NAME=gitea \ + ${{ env.IMAGE_NAME }} # Sử dụng image vừa build/push diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 0000000..0419583 --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,25 @@ +# File: Dockerfile.prod + +# Stage 1: Use a minimal base image like Alpine +FROM alpine:latest + +# Có thể cần cài đặt các dependency tối thiểu mà app cần khi chạy (ví dụ: ca-certificates) +# RUN apk update && apk add --no-cache ca-certificates tzdata + +# Thiết lập thư mục làm việc +WORKDIR /app + +# Copy file binary đã được build từ artifact vào thư mục làm việc trong image +# Đường dẫn "/app/server" này phải khớp với nơi bạn đặt artifact khi download ở bước CI +COPY ./bin/server /app/server + +# Thiết lập quyền thực thi cho file binary +RUN chmod +x /app/server + +# Khai báo cổng mà ứng dụng của bạn lắng nghe bên trong container +# (Ví dụ: 8080, phải khớp với code Go của bạn) +EXPOSE 8080 + +# Lệnh để chạy ứng dụng khi container khởi động +# Sử dụng đường dẫn tuyệt đối hoặc tương đối từ WORKDIR +CMD ["/app/server"] \ No newline at end of file