From fa073c1f7d42ef79cbff4d5fdc604543e0845725 Mon Sep 17 00:00:00 2001 From: phattt2901 Date: Mon, 5 May 2025 14:37:50 +0700 Subject: [PATCH] Test CI v2.1.38 --- .gitea/workflows/ci.yml | 148 +++++++++++++++++++++++++++------------- 1 file changed, 100 insertions(+), 48 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 0ce444b..f93719e 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -1,90 +1,142 @@ -# File: .gitea/workflows/ci.yml +# File: .gitea/workflows/ci.yml (Phiên bản cải thiện) -name: Go CI Pipeline # Tên của workflow +name: Go CI Pipeline -on: # Các sự kiện kích hoạt workflow +# Step 1: Thêm trigger pull_request +on: push: - branches: - - master # Chạy khi push lên nhánh master + branches: [ master ] # Hoặc main + jobs: # ---- Job: Lint Code ---- lint: - name: Lint Code # Tên hiển thị của job - runs-on: ubuntu-latest # Hoặc runner tự host có label tương ứng + name: Lint Code + # Step 3: Chuẩn hóa runs-on + runs-on: ubuntu-latest steps: + # Step 2: Cập nhật action versions - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - - name: Setup Go - uses: actions/setup-go@v4 + # Step 2: Cập nhật action versions + - name: Set up Go + uses: actions/setup-go@v5 with: - go-version: '1.23' + go-version: '1.23' # Đặt phiên bản Go của bạn + cache-dependency-path: go.sum # Bật cache cho Go modules - name: Run golangci-lint uses: golangci/golangci-lint-action@v6 with: - args: --timeout=5m - # version: v1.59 # Chỉ định phiên bản nếu muốn, hoặc dùng latest - # args: --verbose # Thêm tham số nếu cần - # ---- Job: Security Scan (SCA) ---- - security-scan: - name: Security Scan (SCA) - runs-on: ubuntu-latest - # needs: [lint] # Bỏ comment nếu muốn chạy sau khi lint xong, nếu không nó sẽ chạy song song với lint và test - steps: - - name: Checkout code - uses: actions/checkout@v4 + version: latest # Hoặc phiên bản cụ thể + args: --timeout=5m # Giữ timeout + # Gợi ý: Tạo file .golangci.yml trong repo để cấu hình chi tiết, + # bao gồm bật các linter bảo mật như 'gosec' để có SAST cơ bản. + # cache: true # Cân nhắc bật cache của golangci-lint nếu cần - - name: Install Trivy - run: | - curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh - sudo mv trivy /usr/local/bin/ - - - name: Scan source with Trivy - run: | - trivy fs --exit-code 1 --scanners vuln,secret --no-progress . # ---- Job: Run Tests ---- test: name: Run Tests + # Step 3: Chuẩn hóa runs-on runs-on: ubuntu-latest - # needs: [lint] # Bỏ comment nếu muốn chạy sau khi lint xong + # needs: [lint] # Có thể chạy song song với lint steps: + # Step 2: Cập nhật action versions - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 + # Step 2: Cập nhật action versions - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.23' # Đặt phiên bản Go bạn đang dùng - cache-dependency-path: go.sum # Đường dẫn tới file quản lý dependency để cache + go-version: '1.23' + cache-dependency-path: go.sum - - name: Run Go Test & Coverage + # Step 7: Cài đặt tool tạo report JUnit + - name: Install go-junit-report + run: go install github.com/jstemmer/go-junit-report@latest + + # Step 7: Chạy test, tạo coverage và report JUnit + - name: Run Go Test, Coverage and Generate JUnit Report run: | - go test -v -race -coverprofile=coverage.out ./... + # Chạy test với -v để go-junit-report có input, lưu output vào file log + go test -v -race -coverprofile=coverage.out ./... | tee test-output.log + # Tạo report JUnit từ output log + cat test-output.log | ~/go/bin/go-junit-report -set-exit-code > report.xml + # Tạo report coverage dạng text (tùy chọn) go tool cover -func=coverage.out + # Step 7: Upload Test Results (JUnit XML) + - name: Upload JUnit Test Report + # Chạy step này ngay cả khi test thất bại để luôn có report + if: always() + uses: actions/upload-artifact@v4 + with: + name: test-report-junit + path: report.xml + + # Step 7: Upload Coverage Report + - name: Upload Coverage Report + uses: actions/upload-artifact@v4 + with: + name: coverage-report + path: coverage.out + + # ---- Job: Security Scan (SCA - govulncheck) ---- + sca_scan: + name: Security Scan (SCA - govulncheck) + # Step 3: Chuẩn hóa runs-on + runs-on: ubuntu-latest + # needs: [lint, test] # Có thể chạy song song với test và lint + steps: + # Step 2: Cập nhật action versions + - name: Checkout code + uses: actions/checkout@v4 + + # Step 2: Cập nhật action versions + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.23' + cache-dependency-path: go.sum + + # Step 5: Thay Trivy bằng govulncheck action + - name: Run Go Vulnerability Check (govulncheck) + uses: golang/govulncheck-action@v1 + # govulncheck sẽ tự động fail job nếu tìm thấy lỗ hổng có thể bị ảnh hưởng + # ---- Job: Build ---- build: name: Build Application - runs-on: linux-amd64 - # Quan trọng: Job này chỉ chạy sau khi các job kiểm tra thành công - needs: [lint, security-scan, test] + # Step 3: Chuẩn hóa runs-on + runs-on: ubuntu-latest + # Quan trọng: needs phải bao gồm tất cả các job kiểm tra trước đó + needs: [lint, test, sca_scan] steps: + # Step 2: Cập nhật action versions - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 + # Step 2: Cập nhật action versions - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.23' # Đặt phiên bản Go bạn đang dùng + go-version: '1.23' cache-dependency-path: go.sum + # Step 6: Build với các cờ tối ưu và version info - name: Build Go Application - run: go build -v -o ./bin/server ./cmd/server # Đường dẫn tới code main của bạn + run: | + # Lấy thông tin version (ví dụ: tên tag hoặc tên nhánh + SHA ngắn) + # Lưu ý: Biến context của Gitea có thể là gitea.* thay vì github.* + 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 - # (Optional) Bạn có thể thêm bước Upload Artifact ở đây nếu muốn lưu trữ file binary build ra - # - name: Upload build artifact - # uses: actions/upload-artifact@v3 - # with: - # name: server-binary-${{ github.sha }} - # path: ./bin/server \ No newline at end of file + # Step 6: Upload artifact binary + # Step 2: Cập nhật action versions + - name: Upload build artifact + uses: actions/upload-artifact@v4 + 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