Skip to content

Commit 6ebaa73

Browse files
authored
Merge pull request #250 from coderdojo-japan/automate-to-initialize-server-in-actions
feat: サーバー初期化依頼への自動応答ワークフロー計画
2 parents f2894a4 + 288ef6b commit 6ebaa73

File tree

6 files changed

+2070
-14
lines changed

6 files changed

+2070
-14
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
name: サーバー初期化依頼への自動応答
2+
# Rakeの依存関係管理を活用した改善版
3+
4+
on:
5+
issues:
6+
types: [opened]
7+
8+
jobs:
9+
check_and_respond:
10+
# Issue タイトルに「初期化依頼」が含まれている場合のみ実行
11+
if: contains(github.event.issue.title, '初期化依頼')
12+
runs-on: ubuntu-latest
13+
14+
permissions:
15+
issues: write
16+
contents: read
17+
18+
steps:
19+
- name: リポジトリをチェックアウト
20+
uses: actions/checkout@v4
21+
22+
- name: Ruby環境をセットアップ
23+
uses: ruby/setup-ruby@v1
24+
with:
25+
bundler-cache: true
26+
27+
- name: Issue本文からIPアドレスを抽出
28+
id: extract_ip
29+
run: |
30+
# Issue本文を抽出
31+
ISSUE_BODY="${{ github.event.issue.body }}"
32+
33+
# IPアドレスパターンを抽出(数字とドットのみ)
34+
IP_ADDRESS=$(echo "$ISSUE_BODY" | grep -oE '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' | head -1)
35+
36+
if [ -z "$IP_ADDRESS" ]; then
37+
echo "❌ Issue本文にIPアドレスが見つかりませんでした"
38+
echo "ip_found=false" >> $GITHUB_OUTPUT
39+
exit 0
40+
fi
41+
42+
echo "📍 IPアドレスを発見: $IP_ADDRESS"
43+
echo "ip_address=$IP_ADDRESS" >> $GITHUB_OUTPUT
44+
echo "ip_found=true" >> $GITHUB_OUTPUT
45+
46+
- name: IPアドレスを検証してサーバー情報を検索
47+
if: steps.extract_ip.outputs.ip_found == 'true'
48+
id: find_server
49+
env:
50+
SACLOUD_ACCESS_TOKEN: ${{ secrets.SACLOUD_ACCESS_TOKEN }}
51+
SACLOUD_ACCESS_TOKEN_SECRET: ${{ secrets.SACLOUD_ACCESS_TOKEN_SECRET }}
52+
CI: true
53+
run: |
54+
# 統一命名パターンに従ったfind_by_ipタスクを使用
55+
# IPアドレスを明示的にパラメータとして渡す(ENV経由ではなく)
56+
# セキュリティ: トークン情報をマスキング
57+
OUTPUT=$(bundle exec rake "server:find_by_ip[${{ steps.extract_ip.outputs.ip_address }}]" 2>&1 | \
58+
sed 's/SACLOUD_ACCESS_TOKEN=.*/SACLOUD_ACCESS_TOKEN=***/' | \
59+
sed 's/SACLOUD_ACCESS_TOKEN_SECRET=.*/SACLOUD_ACCESS_TOKEN_SECRET=***/' ) || EXIT_CODE=$?
60+
61+
# GitHub Actions用に出力をエスケープ
62+
OUTPUT="${OUTPUT//'%'/'%25'}"
63+
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
64+
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
65+
66+
if [ -z "$EXIT_CODE" ] || [ "$EXIT_CODE" = "0" ]; then
67+
echo "server_found=true" >> $GITHUB_OUTPUT
68+
echo "server_info<<EOF" >> $GITHUB_OUTPUT
69+
echo "$OUTPUT" >> $GITHUB_OUTPUT
70+
echo "EOF" >> $GITHUB_OUTPUT
71+
72+
# 削除準備タスクも実行(インクリメンタル実行用)
73+
# IPアドレスを明示的にパラメータとして渡す
74+
bundle exec rake "server:prepare_deletion[${{ steps.extract_ip.outputs.ip_address }}]" 2>&1 || true
75+
else
76+
echo "server_found=false" >> $GITHUB_OUTPUT
77+
echo "error_message<<EOF" >> $GITHUB_OUTPUT
78+
echo "$OUTPUT" >> $GITHUB_OUTPUT
79+
echo "EOF" >> $GITHUB_OUTPUT
80+
fi
81+
82+
- name: Issueにコメント - サーバーが見つかった場合
83+
if: |
84+
steps.extract_ip.outputs.ip_found == 'true' &&
85+
steps.find_server.outputs.server_found == 'true'
86+
uses: actions/github-script@v6
87+
with:
88+
script: |
89+
// ヘルパー関数: コメント作成をDRY化
90+
const createIssueComment = (comment) => {
91+
return github.rest.issues.createComment({
92+
owner: context.repo.owner,
93+
repo: context.repo.repo,
94+
issue_number: context.issue.number,
95+
body: comment
96+
});
97+
};
98+
99+
const serverInfo = `${{ steps.find_server.outputs.server_info }}`.replace(/%0A/g, '\n').replace(/%0D/g, '\r').replace(/%25/g, '%');
100+
101+
const comment = `## 🔍 サーバー情報を確認しました
102+
103+
${serverInfo}
104+
105+
---
106+
107+
### 📋 次のステップ
108+
109+
@yasulab このサーバーを初期化する場合は、以下のコマンドを実行してください:
110+
111+
#### Rakeタスクを使用した完全なフロー(推奨)
112+
\`\`\`bash
113+
# 全ステップを自動実行(依存関係管理付き)
114+
bundle exec rake server:initialize[${{ steps.extract_ip.outputs.ip_address }},${{ github.event.issue.number }}]
115+
git push
116+
\`\`\`
117+
118+
#### または個別ステップ実行
119+
\`\`\`bash
120+
# 1. 削除準備(情報確認)
121+
bundle exec rake server:prepare_deletion[${{ steps.extract_ip.outputs.ip_address }}]
122+
123+
# 2. サーバー削除
124+
bundle exec rake server:execute_deletion[${{ steps.extract_ip.outputs.ip_address }},true]
125+
126+
# 3. 空コミット作成
127+
bundle exec rake server:create_empty_commit[${{ github.event.issue.number }}]
128+
129+
# 4. プッシュでCI/CD実行
130+
git push
131+
\`\`\`
132+
133+
⚠️ **注意**: サーバー削除は取り消せません。削除前に必ず確認してください。
134+
`;
135+
136+
await createIssueComment(comment);
137+
138+
- name: Issueにコメント - サーバーが見つからなかった場合
139+
if: |
140+
steps.extract_ip.outputs.ip_found == 'true' &&
141+
steps.find_server.outputs.server_found == 'false'
142+
uses: actions/github-script@v6
143+
with:
144+
script: |
145+
// ヘルパー関数: コメント作成をDRY化
146+
const createIssueComment = (comment) => {
147+
return github.rest.issues.createComment({
148+
owner: context.repo.owner,
149+
repo: context.repo.repo,
150+
issue_number: context.issue.number,
151+
body: comment
152+
});
153+
};
154+
155+
const errorMessage = `${{ steps.find_server.outputs.error_message }}`.replace(/%0A/g, '\n').replace(/%0D/g, '\r').replace(/%25/g, '%');
156+
157+
const comment = `## ❌ サーバー情報の取得に失敗しました
158+
159+
IPアドレス: \`${{ steps.extract_ip.outputs.ip_address }}\`
160+
161+
### エラー詳細
162+
\`\`\`
163+
${errorMessage}
164+
\`\`\`
165+
166+
@yasulab 手動での確認が必要です。
167+
168+
### 確認コマンド
169+
\`\`\`bash
170+
# Rakeタスクを使用(統一命名パターン)
171+
bundle exec rake server:find_by_ip[${{ steps.extract_ip.outputs.ip_address }}]
172+
173+
# または直接スクリプト実行
174+
ruby scripts/initialize_server.rb --find ${{ steps.extract_ip.outputs.ip_address }}
175+
\`\`\`
176+
`;
177+
178+
await createIssueComment(comment);
179+
180+
- name: Issueにコメント - IPアドレスが見つからなかった場合
181+
if: steps.extract_ip.outputs.ip_found == 'false'
182+
uses: actions/github-script@v6
183+
with:
184+
script: |
185+
// ヘルパー関数: コメント作成をDRY化
186+
const createIssueComment = (comment) => {
187+
return github.rest.issues.createComment({
188+
owner: context.repo.owner,
189+
repo: context.repo.repo,
190+
issue_number: context.issue.number,
191+
body: comment
192+
});
193+
};
194+
195+
const comment = `## ⚠️ IPアドレスが見つかりませんでした
196+
197+
Issueの本文からIPアドレスを抽出できませんでした。
198+
199+
### 正しいフォーマット例
200+
\`\`\`
201+
CoderDojo【道場名】の【申請者名】です。
202+
サーバー(IPアドレス:192.168.1.1)の初期化をお願いします。
203+
\`\`\`
204+
205+
@yasulab 手動での確認をお願いします。
206+
`;
207+
208+
await createIssueComment(comment);

0 commit comments

Comments
 (0)