📎

GitHub Pagesに別リポジトリのファイルを転載・自動更新

に公開

この記事の目標

別リポジトリで作成したファイルをGitHub Pagesに転載し、別リポジトリでのファイル更新のたびにGitHub Pagesでも更新を反映させます。ここでは反映するファイルとしてPDFを例に取ります。[1]

フォルダツリー

ファイル作成側(maker)

maker
├── main
│   └── main.pdf
└── .github/workflows/deploy-to-pages.yml

GitHub Pages側(username.github.io)

username.github.io
├── _headers
├── note
│   └── main.pdf
└── .github/workflows/deploy to pages.yml

username.github.io/note/main.pdfとしてmaker/main/main.pdfを転載・更新します。

手順

1. makerリポジトリでGitHub Actionsを設定

maker/.github/workflows/deploy-to-pages.ymlを作成

name: Deploy PDF to GitHub Pages

on:
  push:
    branches: [ main ]  # 監視対象ブランチ
    paths:
      - "main/*.pdf"  # 監視対象PDF(必要に応じて変更)

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout masterthesis repo
        uses: actions/checkout@v4

      - name: Prepare PDF file
        run: |
          mkdir -p exported
          cp main/*.pdf exported/  # コピー元ファイルのパス

      - name: Push to username.github.io  # 必要に応じて変更
        uses: peaceiris/actions-gh-pages@v4
        with:
          external_repository: username/username.github.io  # 必要に応じて変更
          publish_branch: main  # 変更先ブランチ
          publish_dir: ./exported
          destination_dir: note
          personal_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}  # 後述

2. Personal Access Tokenを発行・登録

  1. GitHub → Setings → Developer Settings → Personal Access Tokens → Tokens (classic)
  2. Generate new tokenに移って
    • repo
    • workflow
      を含める
  3. 出てきたトークンをコピーして、makerリポジトリのSettings → Secrets → Actions → New repository secretに名前 PERSONAL_ACCESS_TOKENとして登録。maker/.github/workflows/deploy-to-pages.ymlの最終行に合わせる。
  4. makerリポジトリでPDFを更新して反映を確認
脚注
  1. 個人的には、\LaTeXのコンパイルで生成したPDFを更新のたびに個人HPに掲載しているPDFを更新したいというのが動機でした。PDFに限らず応用が可能なはずです。 ↩︎

Discussion