From 5e30794db14f6139eb1b3875f3a6bcc0bc726a00 Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Tue, 2 Jun 2026 21:14:34 -0700 Subject: [PATCH] Harden staging deploy workflow --- .github/workflows/deploy-staging.yml | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/.github/workflows/deploy-staging.yml b/.github/workflows/deploy-staging.yml index 359987f..4132604 100644 --- a/.github/workflows/deploy-staging.yml +++ b/.github/workflows/deploy-staging.yml @@ -10,6 +10,10 @@ on: permissions: contents: read +concurrency: + group: deploy-staging-${{ github.ref }} + cancel-in-progress: false + env: REGISTRY_HOST: ${{ vars.REGISTRY_HOST || 'registry.kxsw.us' }} IMAGE_NAME: ${{ vars.REGISTRY_IMAGE || 'registry.kxsw.us/vpn-server' }} @@ -21,11 +25,21 @@ jobs: build-and-deploy: name: Build image and deploy to staging runs-on: ubuntu-latest + timeout-minutes: 30 steps: - name: Checkout uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: Test + run: go test ./... + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -154,3 +168,55 @@ jobs: docker_cmd image prune -f || true exit 1 + + - name: Notify Telegram on success + if: success() + env: + TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} + TG_CHAT_ID: ${{ secrets.TG_CHAT_ID }} + run: | + if [ -z "${TG_BOT_TOKEN:-}" ] || [ -z "${TG_CHAT_ID:-}" ]; then + echo "Telegram secrets are not configured; skipping notification." + exit 0 + fi + + MESSAGE="$(cat <<'EOF' + ✅ Staging deploy succeeded + Repository: ${{ github.repository }} + Branch: ${{ github.ref_name }} + Commit: ${{ github.sha }} + Actor: ${{ github.actor }} + Image: ${{ env.IMAGE_NAME }}:${{ github.sha }} + Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + EOF + )" + + curl -fsS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \ + --data-urlencode "chat_id=${TG_CHAT_ID}" \ + --data-urlencode "text=${MESSAGE}" + + - name: Notify Telegram on failure + if: failure() + env: + TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} + TG_CHAT_ID: ${{ secrets.TG_CHAT_ID }} + run: | + if [ -z "${TG_BOT_TOKEN:-}" ] || [ -z "${TG_CHAT_ID:-}" ]; then + echo "Telegram secrets are not configured; skipping notification." + exit 0 + fi + + MESSAGE="$(cat <<'EOF' + ❌ Staging deploy failed + Repository: ${{ github.repository }} + Branch: ${{ github.ref_name }} + Commit: ${{ github.sha }} + Actor: ${{ github.actor }} + Image: ${{ env.IMAGE_NAME }}:${{ github.sha }} + Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + EOF + )" + + curl -fsS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \ + --data-urlencode "chat_id=${TG_CHAT_ID}" \ + --data-urlencode "text=${MESSAGE}"