prev/nextナビゲーションのスタイルを改善 #49
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Generate OGP Images | |
| on: | |
| pull_request: | |
| jobs: | |
| generate-ogp: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.head_ref }} | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Generate OGP images | |
| run: npm run generate-ogp | |
| - name: Check for changes after generation | |
| id: generation-check | |
| run: | | |
| # 生成後の差分をチェック | |
| if ! git diff --exit-code content/**/cover.png static/cover.png; then | |
| echo "has_changes=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "has_changes=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Commit and push OGP images | |
| if: steps.generation-check.outputs.has_changes == 'true' | |
| run: | | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| git add content/**/cover.png static/cover.png | |
| git commit -m "chore: update OGP images" | |
| git push origin HEAD:${{ github.head_ref }} | |
| - name: Check for OGP changes in PR | |
| id: pr-check | |
| if: github.event_name == 'pull_request' | |
| run: | | |
| # PRのベースブランチとの差分を取得 | |
| git fetch origin ${{ github.base_ref }} | |
| # PRに含まれるcover.pngの変更を検出 | |
| CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | grep 'cover.png' || true) | |
| echo "Changed OGP files in PR:" | |
| echo "$CHANGED_FILES" | |
| if [ -n "$CHANGED_FILES" ]; then | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| FILES_CSV=$(echo "$CHANGED_FILES" | tr '\n' ',' | sed 's/,$//') | |
| echo "files=$FILES_CSV" >> $GITHUB_OUTPUT | |
| else | |
| echo "changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Update or create PR comment with OGP status | |
| if: github.event_name == 'pull_request' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| # コメント識別用マーカー | |
| MARKER="<!-- OGP_STATUS_COMMENT -->" | |
| # コメント本文を作成 | |
| if [ "${{ steps.pr-check.outputs.changed }}" == "true" ]; then | |
| COMMENT="${MARKER}\n## OGP画像が更新されました\n\n" | |
| # 変更されたファイルごとに画像を表示 | |
| IFS=',' read -ra FILES <<< "${{ steps.pr-check.outputs.files }}" | |
| for file in "${FILES[@]}"; do | |
| # ファイルパスから記事名を抽出 | |
| if [[ "$file" == "static/cover.png" ]]; then | |
| POST_NAME="トップページ (Top Page)" | |
| elif [[ "$file" == "content/about/cover.png" ]]; then | |
| POST_NAME="aboutページ (About Page)" | |
| else | |
| POST_NAME=$(echo "$file" | sed 's|content/posts/||' | sed 's|/cover.png||') | |
| fi | |
| IMAGE_URL="https://github.com/${{ github.repository }}/raw/${{ github.head_ref }}/${file}" | |
| COMMENT="${COMMENT}### ${POST_NAME}\n\n\n" | |
| done | |
| else | |
| COMMENT="${MARKER}\n## OGP画像に差分はありません\n\nOGP画像は最新の状態です。" | |
| fi | |
| # 既存のコメントを検索(マーカーまたはタイトルで検索) | |
| COMMENT_ID=$(gh api "repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \ | |
| --jq '.[] | select(.body | contains("<!-- OGP_STATUS_COMMENT -->") or contains("## OGP画像が更新されました") or contains("## OGP画像に差分はありません")) | .id' | head -1) | |
| echo "Found comment ID: $COMMENT_ID" | |
| echo "Changed status: ${{ steps.pr-check.outputs.changed }}" | |
| # コメントを更新または新規作成 | |
| if [ -n "$COMMENT_ID" ]; then | |
| # 既存のコメントを更新(jqでJSON形式に変換) | |
| echo "Updating existing comment..." | |
| printf '%b' "$COMMENT" | jq -Rs '{"body": .}' | gh api -X PATCH "repos/${{ github.repository }}/issues/comments/${COMMENT_ID}" --input - | |
| echo "Comment updated successfully" | |
| else | |
| # 新規コメントを作成 | |
| echo "Creating new comment..." | |
| echo -e "$COMMENT" | gh pr comment ${{ github.event.pull_request.number }} --body-file - | |
| echo "Comment created successfully" | |
| fi |