Skip to content

Commit 30f5d0a

Browse files
committed
🔧 GitHub Actions: デプロイジョブを分離し条件付き実行を改善
- デプロイを別ジョブに分離(GitHub Actionsのベストプラクティス) - job outputs を使用してジョブ間でデータ共有 - → に更新(最新仕様) - 重複した bundle install を削除(bundler-cache で自動実行) - 環境変数名を FOUND_NEWS に統一(大文字表記) - ニュースが見つかった場合のみデプロイを実行 これにより、ワークフローの構造が改善され、効率的な条件付きデプロイが実現されます。
1 parent 7b0f1ce commit 30f5d0a

File tree

2 files changed

+86
-22
lines changed

2 files changed

+86
-22
lines changed

.github/workflows/#daily.yml#

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Daily Workflow
2+
3+
on:
4+
schedule:
5+
# 毎朝 9:00 JST
6+
- cron: '0 0 * * *'
7+
# Allows you to run this workflow manually from the Actions tab
8+
# https://docs.github.com/en/actions/managing-workflow-runs/manually-running-a-workflow
9+
workflow_dispatch:
10+
11+
jobs:
12+
daily:
13+
runs-on: ubuntu-latest
14+
outputs:
15+
found_news: ${{ steps.check_news.outputs.FOUND_NEWS }}
16+
17+
steps:
18+
- name: ☑️ Checkout code
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 2
22+
23+
- name: 💎 Setup Ruby
24+
uses: ruby/setup-ruby@v1
25+
with:
26+
bundler-cache: true
27+
28+
- name: 📰 Run news:fetch task
29+
run: bin/rails news:fetch
30+
31+
- name: 🆙 Commit updated news.yml
32+
id: check_news
33+
run: |
34+
git config user.name "Yohei Yasukawa"
35+
git config user.email "yohei@yasslab.jp"
36+
git checkout main
37+
git add db/news.yml
38+
if ! git diff --cached --quiet; then
39+
git commit -m '🤖 Upsert db/news.yml'
40+
git push origin main
41+
echo "🆕 Found news in db/news.yml"
42+
echo "found_news=true" >> $GITHUB_OUTPUT
43+
else
44+
echo "✅ No news in db/news.yml"
45+
echo "found_news=false" >> $GITHUB_OUTPUT
46+
fi
47+
48+
deploy:
49+
needs: daily
50+
if: ${{ needs.daily.outputs.found_news == 'true' }}
51+
runs-on: ubuntu-latest
52+
53+
steps:
54+
- name: ☑️ Checkout code
55+
uses: actions/checkout@v4
56+
with:
57+
ref: main
58+
59+
- name: 🚀 Deploy to Heroku
60+
uses: akhileshns/heroku-deploy@v3.13.15
61+
with:
62+
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
63+
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }}
64+
heroku_email: ${{ secrets.HEROKU_EMAIL }}

.github/workflows/daily.yml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ on:
1111
jobs:
1212
daily:
1313
runs-on: ubuntu-latest
14+
outputs:
15+
FOUND_NEWS: ${{ steps.check_news.outputs.FOUND_NEWS }}
1416

1517
steps:
1618
- name: ☑️ Checkout code
@@ -23,13 +25,11 @@ jobs:
2325
with:
2426
bundler-cache: true
2527

26-
- name: 🧪 Install dependencies
27-
run: bundle install
28-
2928
- name: 📰 Run news:fetch task
3029
run: bin/rails news:fetch
3130

3231
- name: 🆙 Commit updated news.yml
32+
id: check_news
3333
run: |
3434
git config user.name "Yohei Yasukawa"
3535
git config user.email "yohei@yasslab.jp"
@@ -39,26 +39,26 @@ jobs:
3939
git commit -m '🤖 Upsert db/news.yml'
4040
git push origin main
4141
echo "🆕 Found news in db/news.yml"
42-
echo "FOUND_NEWS=true" >> $GITHUB_ENV
42+
echo "FOUND_NEWS=true" >> $GITHUB_OUTPUT
4343
else
4444
echo "✅ No news in db/news.yml"
45-
echo "FOUND_NEWS=false" >> $GITHUB_ENV
45+
echo "FOUND_NEWS=false" >> $GITHUB_OUTPUT
4646
fi
4747
48-
- name: ✅ Do nothing if no news found
49-
if: ${{ env.FOUND_NEWS == 'false' }}
50-
run: |
51-
echo "No news found."
52-
53-
- name: 🚀 Deploy to Heroku if news found
54-
if: ${{ env.FOUND_NEWS == 'true' }}
55-
# TODO: This workflows depend on Ubuntu version.
56-
# https://github.com/AkhileshNS/heroku-deploy/issues/186
57-
runs-on: ubuntu-22.04
58-
steps:
59-
- uses: actions/checkout@v3
60-
- uses: akhileshns/heroku-deploy@v3.13.15
61-
with:
62-
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
63-
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }}
64-
heroku_email: ${{ secrets.HEROKU_EMAIL }}
48+
deploy:
49+
needs: daily
50+
if: ${{ needs.daily.outputs.FOUND_NEWS == 'true' }}
51+
runs-on: ubuntu-latest
52+
53+
steps:
54+
- name: ☑️ Checkout code
55+
uses: actions/checkout@v4
56+
with:
57+
ref: main
58+
59+
- name: 🚀 Deploy to Heroku
60+
uses: akhileshns/heroku-deploy@v3.14.15
61+
with:
62+
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
63+
heroku_app_name: ${{ secrets.HEROKU_APP_NAME }}
64+
heroku_email: ${{ secrets.HEROKU_EMAIL }}

0 commit comments

Comments
 (0)