Skip to content

Commit f039b8d

Browse files
gn-t-kclaude
andcommitted
feat: PRクローズ時にプレビュー環境用データベースを自動削除するworkflowを追加
- preview-db-delete.ymlを追加 - PRクローズ時にTurso Platform APIを使用してpreview-pr{number}-authデータベースを削除 - Vercelのプレビュー環境変数も自動削除 - PRにデータベース削除完了のコメントを投稿 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 82d2787 commit f039b8d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Preview Database Delete
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
7+
jobs:
8+
delete-preview-db:
9+
name: Delete Preview Database
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 10
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
15+
16+
- name: Delete Preview Database
17+
env:
18+
TURSO_API_TOKEN: ${{ secrets.TURSO_PLATFORM_API_TOKEN }}
19+
TURSO_ORG_SLUG: ${{ secrets.TURSO_ORG_SLUG }}
20+
PR_NUMBER: ${{ github.event.pull_request.number }}
21+
run: |
22+
DB_NAME="preview-pr${PR_NUMBER}-auth"
23+
24+
# データベースを削除
25+
DELETE_RESPONSE=$(curl -X DELETE \
26+
-H "Authorization: Bearer ${TURSO_API_TOKEN}" \
27+
"https://api.turso.tech/v1/organizations/${TURSO_ORG_SLUG}/databases/${DB_NAME}")
28+
29+
echo "Database deletion response: ${DELETE_RESPONSE}"
30+
31+
# エラーチェック(404は既に削除済みと判断)
32+
if echo "${DELETE_RESPONSE}" | jq -e '.error' > /dev/null; then
33+
ERROR_MSG=$(echo "${DELETE_RESPONSE}" | jq -r '.error')
34+
if [[ "${ERROR_MSG}" == *"not found"* ]]; then
35+
echo "Database ${DB_NAME} was already deleted or does not exist."
36+
exit 0
37+
else
38+
echo "Error deleting database: ${ERROR_MSG}"
39+
exit 1
40+
fi
41+
fi
42+
43+
echo "Successfully deleted database: ${DB_NAME}"
44+
45+
- name: Delete Vercel Environment Variables
46+
env:
47+
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
48+
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
49+
run: |
50+
# プレビュー環境の環境変数を取得
51+
ENV_VARS=$(curl -X GET \
52+
-H "Authorization: Bearer ${VERCEL_TOKEN}" \
53+
"https://api.vercel.com/v10/projects/${VERCEL_PROJECT_ID}/env?gitBranch=${{ github.head_ref }}")
54+
55+
# TURSO_AUTH_DATABASE_URLのIDを取得して削除
56+
URL_ENV_ID=$(echo "${ENV_VARS}" | jq -r '.envs[] | select(.key=="TURSO_AUTH_DATABASE_URL" and .gitBranch=="${{ github.head_ref }}") | .id' | head -n 1)
57+
if [ -n "${URL_ENV_ID}" ] && [ "${URL_ENV_ID}" != "null" ]; then
58+
curl -X DELETE \
59+
-H "Authorization: Bearer ${VERCEL_TOKEN}" \
60+
"https://api.vercel.com/v9/projects/${VERCEL_PROJECT_ID}/env/${URL_ENV_ID}"
61+
echo "Deleted TURSO_AUTH_DATABASE_URL environment variable"
62+
fi
63+
64+
# TURSO_AUTH_DATABASE_AUTH_TOKENのIDを取得して削除
65+
TOKEN_ENV_ID=$(echo "${ENV_VARS}" | jq -r '.envs[] | select(.key=="TURSO_AUTH_DATABASE_AUTH_TOKEN" and .gitBranch=="${{ github.head_ref }}") | .id' | head -n 1)
66+
if [ -n "${TOKEN_ENV_ID}" ] && [ "${TOKEN_ENV_ID}" != "null" ]; then
67+
curl -X DELETE \
68+
-H "Authorization: Bearer ${VERCEL_TOKEN}" \
69+
"https://api.vercel.com/v9/projects/${VERCEL_PROJECT_ID}/env/${TOKEN_ENV_ID}"
70+
echo "Deleted TURSO_AUTH_DATABASE_AUTH_TOKEN environment variable"
71+
fi
72+
73+
- name: Comment PR with Deletion Info
74+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
75+
env:
76+
PR_NUMBER: ${{ github.event.pull_request.number }}
77+
with:
78+
script: |
79+
github.rest.issues.createComment({
80+
issue_number: context.issue.number,
81+
owner: context.repo.owner,
82+
repo: context.repo.repo,
83+
body: `## 🗑️ Preview Database Deleted\n\n✅ プレビュー環境用のデータベースを削除しました!\n\n**Database Name**: preview-pr${process.env.PR_NUMBER}-auth\n\n> PRがクローズされたため、関連するデータベースとVercel環境変数を自動的にクリーンアップしました。`
84+
})

0 commit comments

Comments
 (0)