🐱

github-readme-statsとgithub-profile-trophyが使えなくなったのでGitHub Actionsで代用する

に公開

経緯

GitHubアカウントのプロフィールを彩る手段の一つにgithub-readme-statsとgithub-profile-trophyがあります。実際に私も利用しています。


実際の私のGitHub profile

元々はhttps://github-readme-stats.vercel.app/api?username=ro80tのようなURLをimgタグとして読み込んでいたのですがVercelの制限によってgithub-readme-statsとgithub-profile-trophyがともに使えなくなってしまい途方に暮れていました。
self-hostすることも考えましたがVercelを今後も無料で使えるとも限りませんし他人が悪用しないように設定するのも面倒だと思っていたところにGitHub Actionsでsvgとして生成できることを知り、この記事に至るというわけです。

料金面

個人利用かつパブリックリポジトリだとGitHub Actionsは無料なのでGitHubアカウントのプロフィール用途でしたら無料で利用することが可能です。

実践

your-profile-repo
│  README.md
├─.github
│  │  dependabot.yml
│  │
│  └─workflows
│          images.yml

上記のような構成で以下の二つのファイルを配置するとimagesフォルダにstats.svg、top-langs.svg、trophy.svg、wakatime.svgの四つが作成されます。
なのでREADME.md上で![](./images/[file_name].svg)と記載すれば以前のようにgithub-readme-statsとgithub-profile-trophyを使う事が可能です。

.github/workflows/images.yml
name: Update README images

on:
  pull_request: # dependabotを使わない場合は削除
  schedule:
    - cron: "0 3 * * *"
  workflow_dispatch:

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v6

      - name: Generate stats card
        uses: readme-tools/github-readme-stats-action@v1
        with:
          card: stats
          options: username=${{ github.repository_owner }}&show_icons=true&count_private=true&theme=onedark
          path: images/stats.svg
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Generate top langs card
        uses: readme-tools/github-readme-stats-action@v1
        with:
          card: top-langs
          options: username=${{ github.repository_owner }}&show_icons=true&count_private=true&layout=compact&theme=onedark
          path: images/top-langs.svg
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Generate wakatime card
        uses: readme-tools/github-readme-stats-action@v1
        with:
          card: wakatime
          options: username=${{ github.repository_owner }}&layout=compact&theme=onedark&count_private=true # github idとwakatime idが違う場合はこちらの${{ github.repository_owner }}の部分を変更してください
          path: images/wakatime.svg
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Generate trophy
        uses: Erik-Donath/github-profile-trophy@feature/generate-svg
        with:
            username: ${{ github.repository_owner }}
            token: ${{ secrets.GITHUB_TOKEN }}
            file: images/trophy.svg
            theme: onedark
            max-rows: 1

      - name: Commit cards
        if: contains(fromJSON('["schedule", "workflow_dispatch"]'), github.event_name) # dependabotを使わない場合は削除
        run: |
          git config user.name "github-actions"
          git config user.email "github-actions@users.noreply.github.com"
          git add images/*.svg
          git commit -m "Update README images" || exit 0
          git push
.github/dependabot.yml
# dependabotを使わない場合このファイルは消していただいて構いません
version: 2
enable-beta-ecosystems: true

updates:
  - package-ecosystem: github-actions
    directory: /
    schedule:
      interval: weekly

解説

dependabot

.github/dependabot.ymlはdependabot用のconfigです。
今回の場合はgithub-actionsのみを検知対象に入れています。
しかしdependabot.ymlの追加のみだと.github/workflows/images.ymlが動かなくなる可能性もあるためevent triggerにpull_requestを追加しpull_requestの場合commitは必要ないためcommitの実行に条件を課しています。(今回の場合トリガーがscheduleとworkflow_dispatchの時のみ実行しています。)

.github/workflows/images.yml
on:
+  pull_request:
  schedule:
    - cron: "0 3 * * *"
  workflow_dispatch:

...

    - name: Commit cards
+        if: contains(fromJSON('["schedule", "workflow_dispatch"]'), github.event_name) # dependabotを使わない場合は削除
        run: |
          git config user.name "github-actions"
          git config user.email "github-actions@users.noreply.github.com"
          git add images/*.svg
          git commit -m "Update README images" || exit 0
          git push

event trigger

イベントトリガーは全部で三つです。pull_requestに関してはひとつ前のdependabotで解説しているので割愛させていただきます。

.github/workflows/images.yml
on:
  pull_request: # dependabotを使わない場合は削除
  schedule:
    - cron: "0 3 * * *"
  workflow_dispatch:

scheduleイベントは今回の場合、毎日朝3時に実行するようになっています。

実行タイミングを変更したい場合はcron triggerで検索してください

workflow_dispatchは任意のタイミングでGitHub上から実行できることを指します。
該当リポジトリのActionsタブから該当ワークフロー(今回の場合Update README images)を選択するとRun workflowというボタンが出ていると思いますのでブランチを選択しRun workflowボタンを押せば任意のタイミングで実行することが可能です。

update job

Checkout repoはリポジトリのソースコードをチェックアウトし、GitHub Actions上でファイルを操作可能にするために必要です。難しい場合はおまじないだと思ってください。これの解説は本筋からあまりにも脱線してしまうため割愛させていただきます。

Generate stats card ~ Generate trophyまでのstepsは全てimagesフォルダ内にsvgを生成するためのstepsです。
Generate stats card、Generate top langs card、Generate wakatime cardの三つはクエリ形式で書かれているOptionsを変更することでデザインを変えることが出来ます。
以下にOptionsの一覧が載っていますのでカスタマイズしたい場合は参照していただけると幸いです。

Generate trophyに関しては少し特殊で公式リポジトリにワークフローの例が記載されているのですが、このままではエラーが出てしまいます。
どうやらGitHub Actions用のComponentを作っている方がinputを変更し、それがまだ本体のREADME.mdに反映されてないようです。
クエリ形式でのデザイン変更が出来ないなどgithub-readme-statsよりも癖が強いので公式リポジトリではなくActions用リポジトリのreadmeを参照してください

Commit cards stepは生成された四つのsvgを実際にpushし反映するためのステップです。
今回はGitHub Actionsからのcommitとpushなのでconfigのuser.nameをgithub-actionsにuser.emailをgithub-actions@users.noreply.github.comにしています。
残りはステージしてコミットしてプッシュするだけです。

    - name: Commit cards
        if: contains(fromJSON('["schedule", "workflow_dispatch"]'), github.event_name) # dependabotを使わない場合は削除
        run: |
          git config user.name "github-actions"
          git config user.email "github-actions@users.noreply.github.com"
          git add images/*.svg
          git commit -m "Update README images" || exit 0
          git push

最後に

不定期で記事を投稿していますのでフォローしていただけると幸いです。
TwitterやGitHubも是非フォローお願いします。

https://twitter.com/ro80t_dev
https://github.com/ro80t

Discussion