0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MicroCMS + Nextjs + GitHub pages + GitHub Actionsでほぼ無料ブログ作成

Posted at

こんにちは、学生でWebアプリ開発やWeb3について勉強しているNknight AMAMIYAです。
今回は私が普段運用しているブログについて紹介しようと思います。

ブログはこちら

警告
私はQiita初心者のため多分いろんなところで間違えてると思います。間違い等みつけましたらやんわりとコメントお願い致します。

技術スタック

フレームワーク

  • Next.js
  • MicroCMS SDK(JavaScript)
  • PandaCSS

言語

  • TypeScript

ホスティング

  • GitHub Pages
  • GitHub Actions

環境について

Webサイト全体でNextjsを使っていて、ブログ開発プロジェクト紹介ページにMicroCMSを使っています。

ヘッドレスCMSのため柔軟な運用ができて重宝しています。あと趣味程度であれば無料版の範囲に収まるので金欠な学生みはありがたいw

サイトの構造はこんな感じです。

- src
    - src/pages <= ホームページ
        - src/pages/blog 一覧
            - src/pages/blog/[id] 記事ページ
        - src/dev/ <= 開発PJページ
            - src/dev/[id]詳細ぺーージ

Pagesにデプロイする際にはリポジトリにプッシュしたあとnextjs.yamlの内容をもとに自動でActionsでnext buildコマンドを実行されデプロイされます。

# Sample workflow for building and deploying a Next.js site to GitHub Pages
#
# To get started with Next.js see: https://nextjs.org/docs/getting-started
#
name: Deploy Next.js site to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["master"]
  repository_dispatch:
    types: [update-posts]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Detect package manager
        id: detect-package-manager
        run: |
          if [ -f "${{ github.workspace }}/yarn.lock" ]; then
            echo "manager=yarn" >> $GITHUB_OUTPUT
            echo "command=install" >> $GITHUB_OUTPUT
            echo "runner=yarn" >> $GITHUB_OUTPUT
            exit 0
          elif [ -f "${{ github.workspace }}/package.json" ]; then
            echo "manager=npm" >> $GITHUB_OUTPUT
            echo "command=ci" >> $GITHUB_OUTPUT
              # Use `npm run` so later steps can call `npm run build`
              echo "runner=npm run" >> $GITHUB_OUTPUT
            exit 0
          else
            echo "Unable to determine package manager"
            exit 1
          fi
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: ${{ steps.detect-package-manager.outputs.manager }}
      - name: Setup Pages
        uses: actions/configure-pages@v5
        with:
          # Automatically inject basePath in your Next.js configuration file and disable
          # server side image optimization (https://nextjs.org/docs/api-reference/next/image#unoptimized).
          #
          # You may remove this line if you want to manage the configuration yourself.
          static_site_generator: next
      - name: Restore cache
        uses: actions/cache@v4
        with:
          path: |
            .next/cache
          # Generate a new cache whenever packages or source files change.
          key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
          # If source files changed but packages didn't, rebuild from a prior cache.
          restore-keys: |
            ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
      - name: Install dependencies
        run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
      - name: Build with Next.js
        run: ${{ steps.detect-package-manager.outputs.runner }} build
        env:
          CMS_API_KEY: ${{ secrets.CMS_API_KEY }}
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./out

  # Deployment job
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

またMicroCMS側で設定すれば新規投稿があると自動的にActionsが実行されてリビルド=>デプロイを勝手にやってくれます。便利。

設定の方法は以下から

ただしNextjsのサイトをGitHub pagesで公開する場合ミドルウェア関係の機能は使えず、ブログ部分も本来は動的にHTMLを生成されるのですがPagesの場合は公開済のMicroCMSの投稿分静的ページを生成しそのままPagesにデプロイする感じです。Nextjsのメリットを台無しにしてしまってるため、今後は別のフレームワークも検討中です。

おためしでAstroJSで作ったもがこちら。

もちろんAstroでもMicroCMSが使えます。Nextjsに比べて表示も速く、公式でActionsのサンプルもあるのでブログだけだったらこっちの方が便利かもしれないです。

ホームページのリポジトリ

そのほか趣味でいろんなWebアプリを作ってます

参考リンクなど

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?