diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml new file mode 100644 index 0000000..b96230c --- /dev/null +++ b/.github/workflows/e2e.yml @@ -0,0 +1,129 @@ +name: E2E + +on: + push: + branches: + - main + paths: + - "src/**" + - "tests/e2e/**" + - "playwright.config.ts" + - "package.json" + - "package-lock.json" + - ".github/workflows/e2e.yml" + pull_request: + branches: + - main + +jobs: + playwright: + runs-on: ubuntu-latest + timeout-minutes: 25 + + env: + NEZHA_REPO: nezhahq/nezha + NEZHA_REF: master + NZ_LISTENHOST: 127.0.0.1 + NZ_LISTENPORT: "8008" + NZ_AGENTSECRETKEY: e2e-agent-secret-32-bytes-long-ok + NZ_JWTSECRETKEY: e2e-jwt-secret-key-min-32-chars-long-ok + NZ_SITENAME: nezha-e2e + NZ_DEBUG: "true" + E2E_ADMIN_USER: admin + E2E_ADMIN_PASS: admin + E2E_BASE_URL: http://127.0.0.1:5173 + + steps: + - name: Checkout admin-frontend + uses: actions/checkout@v4 + with: + path: admin-frontend + + - name: Checkout nezha backend + uses: actions/checkout@v4 + with: + repository: ${{ env.NEZHA_REPO }} + ref: ${{ env.NEZHA_REF }} + path: nezha + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: npm + cache-dependency-path: admin-frontend/package-lock.json + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: "1.26.x" + cache-dependency-path: nezha/go.sum + + - name: Install frontend dependencies + working-directory: admin-frontend + run: npm ci + + - name: Install Playwright browsers + working-directory: admin-frontend + run: npx playwright install --with-deps chromium + + - name: Prepare backend stubs (dist + swagger docs) + working-directory: nezha + run: | + mkdir -p cmd/dashboard/admin-dist cmd/dashboard/user-dist + touch cmd/dashboard/admin-dist/.gitkeep cmd/dashboard/user-dist/.gitkeep + go install github.com/swaggo/swag/cmd/swag@latest + "$(go env GOPATH)/bin/swag" init --pd -d cmd/dashboard -g main.go -o cmd/dashboard/docs + + - name: Start backend + working-directory: nezha + env: + GIN_MODE: release + run: | + mkdir -p data + nohup go run ./cmd/dashboard -c data/config.yaml -db data/sqlite.db \ + > /tmp/dashboard.log 2>&1 & + echo $! > /tmp/dashboard.pid + + - name: Wait for backend health + run: | + for i in {1..60}; do + if curl -fsS http://127.0.0.1:8008/api/v1/setting > /dev/null; then + echo "backend is up" + exit 0 + fi + sleep 1 + done + echo "backend failed to start within 60s" + echo "---- dashboard.log ----" + cat /tmp/dashboard.log || true + exit 1 + + - name: Run Playwright tests + working-directory: admin-frontend + env: + CI: "true" + run: npm run e2e + + - name: Upload Playwright report + if: always() + uses: actions/upload-artifact@v4 + with: + name: playwright-report + path: admin-frontend/playwright-report + retention-days: 14 + + - name: Upload dashboard log + if: always() + uses: actions/upload-artifact@v4 + with: + name: dashboard-log + path: /tmp/dashboard.log + retention-days: 7 + + - name: Stop backend + if: always() + run: | + if [ -f /tmp/dashboard.pid ]; then + kill "$(cat /tmp/dashboard.pid)" || true + fi