🍴

GitHub Actions で Fork を実行する方法

2024/01/18に公開

github actions fork とかで調べても全然情報が見つからなくて苦労したので、備忘録として記事にしました。

(そもそもワークフローで Fork する需要がないのかもしれませんが……😂)

同じ悩みに至った方の助けになれば幸いです。

手順まとめ

  1. GitHub App を作成する
    • Permission は [Repository permissions] 以下の AdministrationContentsRead and write に設定
  2. 作成した GitHub App をリポジトリにインストール
  3. リポジトリの secrets に GitHub App の情報を設定
  4. Fork ワークフローを作成し実行(以下サンプルコード)
name: ForkTest

# 手動実行トリガー
on: [workflow_dispatch]

jobs:
  integration:
    runs-on: ubuntu-latest
    steps:
      - name: Generate a token
        id: generate_token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}

      # fork コマンドの使い方は https://cli.github.com/manual/gh_repo_fork 参照
      - run: gh repo fork tigrig-private/ys-workflow-to-create-gamedev-repo --clone=false --default-branch-only --fork-name=fork-test --org=tigrig-private
        env:
          GH_TOKEN: ${{ steps.generate_token.outputs.token }}

手順詳細

1. GitHub App を作成する

https://docs.github.com/ja/enterprise-cloud@latest/apps/creating-github-apps/registering-a-github-app/registering-a-github-app

こちらの記事に従って App を作成する。

Permissions については、以下の通り有効化する。

  • Repository permissions > AdministrationRead and write に設定
  • Repository permissions > ContentsRead and write に設定

他の設定は自由(Forkするにあたって特別必要な設定は上記 Permissions のみ)。

2. 作成した GitHub App をリポジトリにインストール

ワークフローを作成するリポジトリに対して、先程の GitHub App をインストールする。

手順は以下。

https://docs.github.com/ja/enterprise-cloud@latest/apps/using-github-apps/installing-your-own-github-app#installing-your-private-github-app-on-your-repository

最終的に、 Settings タブ > GitHub Apps で下図のように表示されていればOK。

3. リポジトリの secrets に GitHub App の情報を設定

リポジトリの Settings > サイドバーの Secrets and variables > Actions を開き、Secrets タブから [New repository secret] を押して secrets を追加する。

secrets は以下の2つ登録する

  • APP_ID
  • APP_PRIVATE_KEY

APP_ID には、こちらの手順で確認できる GitHub App の [App ID] を設定

APP_PRIVATE_KEY には、こちらの手順 に従って作成した秘密鍵のテキスト全文を設定

4. Fork ワークフローを作成し実行

リポジトリ直下に .github/workflows フォルダを作成し、適当な .yml ファイルを作成し、以下のようなコードを記述。

name: ForkTest

# 手動実行トリガー
on: [workflow_dispatch]

jobs:
  integration:
    runs-on: ubuntu-latest
    steps:
      - name: Generate a token
        id: generate_token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}

      # fork コマンドの使い方は https://cli.github.com/manual/gh_repo_fork 参照
      - run: gh repo fork tigrig-private/ys-workflow-to-create-gamedev-repo --clone=false --default-branch-only --fork-name=fork-test --org=tigrig-private
        env:
          GH_TOKEN: ${{ steps.generate_token.outputs.token }}
ワークフローの書き方で参考にしたページ

ワークフローを作ったら、コミット&プッシュ。

GitHub のリポジトリページから Actions タブを開き、サイドバーに表示されている {ワークフロー名} を選択 → [Run workflow] を実行すると動作確認できる。

詰まったところメモ

secrets.GITHUB_TOKEN では Fork できない

https://docs.github.com/ja/actions/security-guides/automatic-token-authentication

このトークンの権限は、ワークフローを含むリポジトリに限定されます。

とのことで、自分のユーザー領域であったり、organization 下に Fork することはできない模様。

→ GitHub App を使って解決。

https://docs.github.com/ja/enterprise-cloud@latest/apps/creating-github-apps/authenticating-with-a-github-app/making-authenticated-api-requests-with-a-github-app-in-a-github-actions-workflow

GitHub App の Permissions どれをつければいいか

以下のページでやりたいこと(例えば fork とか)を検索し、必要な権限を見つけられる。

https://docs.github.com/en/rest/authentication/permissions-required-for-github-apps?apiVersion=2022-11-28

VSCode で secrets.APP_ID とか書いたら警告が出てくる

Context access might be invalid という警告。

GitHub Actions 拡張機能 をインストールしたら消えた。

※↓を見て解決した

https://github.com/github/vscode-github-actions/issues/222#issuecomment-1742181463

actions/create-github-app-token@v1 でワークフロー元リポジトリ以外にアクセスする場合の設定

https://github.com/marketplace/actions/create-github-app-token#create-a-token-for-all-repositories-in-the-current-owners-installation

公式サイトに記載の通り、デフォルトでは Current repository にしかアクセスできないようで、 ownerrepositories といったパラメータを別途指定しなければならなかった。

シンプルな Fork にあたっては、 owner の指定だけで行けた。

organization の private リポジトリはデフォルトで Fork 無効化されている

そもそも Fork できない状態なのに、ずっとワークフローが間違えてるのかとか、GitHub App の設定が間違ってるのかとか、余計に悩んでしまいました……。

以下の手順に従って Fork 有効化して解決しました。

https://docs.github.com/ja/organizations/managing-organization-settings/managing-the-forking-policy-for-your-organization

Discussion