Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📊 Automate to update profile upon dojo stats #1

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions .github/workflows/scheduler_daily.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Daily Update

# NOTE: GitHub Action's scheduler is always set to UTC+0. So 9am should be set at 0am for JST (UTC+9)
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows#scheduled-events-schedule

# '0 23 * * *' == 8am in JST (UTC+9)
# '0 0 * * *' == 9am in JST (UTC+9)
# '0 1 * * *' == 10am in JST (UTC+9)
# '59 23 * * *' task will be completed after 9am in JST
on:
schedule:
- cron: '59 20 * * *'

# [DEBUG ONLY] Every 5 minutes
# https://github.blog/changelog/2019-11-01-github-actions-scheduled-jobs-maximum-frequency-is-changing
#- cron: '*/5 * * * *'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: 📥 Download codes from GitHub
uses: actions/checkout@v3
with:
fetch-depth: 2

- name: 💎 Set up Ruby
uses: ruby/setup-ruby@v1
#with:
# bundler-cache: true
#ruby-version: 3.2 # Not necessary if .ruby-version is given

- name: 🧪 Install Ruby gems
run: |
bundle install

- name: 📊 Update dojo stats of Japan
run: |
bundle exec ruby update_profile.rb

- name: 🆙 Update profile if updated
run: |
if [ -n "$(git status --porcelain)" ]; then
git config --global user.name "Yohei Yasukawa"
git config --global user.email "[email protected]"
git checkout main
git add _data/stats.yml
git commit -m '🤖 Update dojo stats'
git push origin main
fi
env:
GITHUB_TOKEN:

- name: 🔧 Build & Test
run: |
JEKYLL_ENV=production bundle exec jekyll build
JEKYLL_ENV=production bundle exec jekyll doctor
SKIP_BUILD=true bundle exec rake test

- name: 🚀 Deploy to GitHub Pages
if: github.ref == 'refs/heads/main' && job.status == 'success'
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./_site
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
with:
bundler-cache: true

- name: 🧪 Install gems
run: |
bundle install
- name: 🔧 Build & Test
run: |
JEKYLL_ENV=test bundle exec jekyll build
Expand All @@ -49,8 +53,6 @@ jobs:

- name: 🧪 Install gems
run: |
bundle config set with 'test'
bundle config set path 'vendor/bundle'
bundle install
- name: 🔧 Build
Expand Down
2 changes: 2 additions & 0 deletions _data/stats.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
active_dojos: 180
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ <h3 class="headline rich_font" style='margin-top: 70px;'>DojoCon Japan とは?

<h3 class="headline rich_font" style='margin-top: 70px;'>CoderDojo とは?</h3>
<div class="desc">
<p>CoderDojo は7〜17歳を対象とした非営利のプログラミング道場です。2011年にアイルランドで始まり、世界では100カ国・2,000の道場、<a href='https://coderdojo.jp/'>日本には180以上の道場</a>があります。</p>
<p>CoderDojo は7〜17歳を対象とした非営利のプログラミング道場です。2011年にアイルランドで始まり、世界では100カ国・2,000の道場、<a href='https://coderdojo.jp/'>日本には{{ site.data.stats['active_dojos'] }}以上の道場</a>があります。</p>
</div>
</div>
</div>
Expand Down
13 changes: 13 additions & 0 deletions update_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby

require 'net/http'
require 'json'
require 'yaml'

BASE_URL = 'https://coderdojo.jp'
DOJO_STATS = JSON.parse Net::HTTP.get(URI.parse "#{BASE_URL}/stats.json"), symbolize_names: true
STATS_PATH = '_data/stats.yml'

stats = YAML.load_file(STATS_PATH)
stats['active_dojos'] = DOJO_STATS[:active_dojos]
YAML.dump stats, File.open(STATS_PATH, 'w')