study repoにpushしたらstudy_gatsby repoのデプロイアクションが動くようにした

GitHubActionsSSG

背景

study_gatsbyからsubmoduleでstudyを参照している。

before

alt

studyリポジトリにデプロイアクションを記述していたのだが、gatsbyビルド環境はstudy_gatsbyリポジトリにあり、分かれていて辛い状態だった。

after

alt

デプロイアクションをstudy_gatsbyリポジトリに移し、studyリポジトリpush時にstudy_gatsbyリポジトリのデプロイアクションをディスパッチするように変更した。
これによりgatsbyビルド環境とデプロイアクションが一箇所にまとまり管理しやすくなった。

詳細

https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#repository_dispatch

study_gatsbyのワークフローyaml

  on:
    push:
      branches:
        - master
+   repository_dispatch:
+     types:
+       - "study blog contents updated"

repository_dispatch"study blog contents updated"イベントをサブスクライブする。

studyのワークフローyaml

name: "dispatch study_gatsby repo's Actions"

on:
  push:
    branches:
      - master

jobs:
  publish:
    runs-on: ubuntu-16.04
    steps:
    - name: Create GitHub dispatch event
      run: |
        curl --request POST 'https://api.github.com/repos/wand2016/study_gatsby/dispatches' \
        --header 'Authorization: Bearer ${{secrets.ACCESS_TOKEN_FOR_DISPATCH}}' \
        --header 'Content-Type: application/json' \
        --data-raw '{
          "event_type": "study blog contents updated"
        }'

GitHubアカウントの設定からrepo権限でPersonal Access Tokenを発行して
studyリポジトリのsecretsに登録しておく。

        --header 'Authorization: Bearer ${{secrets.ACCESS_TOKEN_FOR_DISPATCH}}' \

event_typeにはstudy_gatsby でサブスクライブしたイベントと同名のものを指定する。

        --data-raw '{
          "event_type": "study blog contents updated"
        }'