Deploy Next.js site to Pages #23
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
| # 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: | |
| # 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 | |
| with: | |
| fetch-depth: 0 # Fetch all history to ensure we have all files | |
| - name: Debug - List workspace contents | |
| run: | | |
| echo "=== Workspace Contents ===" | |
| ls -la ${{ github.workspace }} | |
| echo "=== Package Files Check ===" | |
| if [ -f "${{ github.workspace }}/yarn.lock" ]; then | |
| echo "✅ yarn.lock file found" | |
| else | |
| echo "❌ yarn.lock file NOT found" | |
| fi | |
| if [ -f "${{ github.workspace }}/package.json" ]; then | |
| echo "✅ package.json file found" | |
| echo "=== Package.json Contents ===" | |
| cat package.json | |
| else | |
| echo "❌ package.json file NOT found" | |
| fi | |
| - name: Debug - Node and Yarn versions | |
| run: | | |
| echo "=== Node Version ===" | |
| node --version | |
| echo "=== Yarn Version ===" | |
| yarn --version | |
| echo "=== NPM Version ===" | |
| npm --version | |
| - name: Detect package manager | |
| id: detect-package-manager | |
| run: | | |
| echo "=== Package Manager Detection ===" | |
| if [ -f "${{ github.workspace }}/yarn.lock" ]; then | |
| echo "📦 Using Yarn as package manager" | |
| 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 "📦 Using NPM as package manager" | |
| echo "manager=npm" >> $GITHUB_OUTPUT | |
| echo "command=ci" >> $GITHUB_OUTPUT | |
| echo "runner=npx --no-install" >> $GITHUB_OUTPUT | |
| exit 0 | |
| else | |
| echo "❌ Unable to determine package manager" | |
| exit 1 | |
| fi | |
| # Enable Corepack and install the correct Yarn version (3.5.0) BEFORE Node setup | |
| - name: Enable Corepack and install Yarn | |
| run: | | |
| echo "=== Setting up Yarn ===" | |
| corepack enable | |
| corepack prepare [email protected] --activate | |
| echo "✅ Yarn setup complete" | |
| - name: Setup Node | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "20" | |
| cache: yarn | |
| - name: Debug - Environment after Node setup | |
| run: | | |
| echo "=== Environment Variables ===" | |
| env | sort | |
| echo "=== Current Directory ===" | |
| pwd | |
| echo "=== Directory Contents ===" | |
| ls -la | |
| - name: Setup Pages | |
| if: github.actor != 'nektos/act' | |
| 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: | | |
| echo "=== Installing Dependencies ===" | |
| if [ "${{ github.actor }}" = "nektos/act" ]; then | |
| echo "Using --no-immutable flag for local testing" | |
| yarn install --no-immutable | |
| else | |
| echo "Using --immutable flag for CI" | |
| # First try with --immutable | |
| if ! yarn install --immutable; then | |
| echo "⚠️ Lockfile needs updates, updating dependencies..." | |
| # If immutable fails, update dependencies and regenerate lockfile | |
| yarn install --mode=update-lockfile | |
| # Verify the installation | |
| yarn install --immutable | |
| fi | |
| fi | |
| echo "=== Dependencies Installation Complete ===" | |
| echo "=== Node Modules Contents ===" | |
| ls -la node_modules | |
| - name: Build with Next.js | |
| run: | | |
| echo "=== Starting Next.js Build ===" | |
| ${{ steps.detect-package-manager.outputs.runner }} next build | |
| echo "=== Build Complete ===" | |
| echo "=== Build Output Contents ===" | |
| ls -la out | |
| - name: Upload artifact | |
| if: github.actor != 'nektos/act' | |
| uses: actions/upload-pages-artifact@v3 | |
| with: | |
| path: ./out | |
| # Deployment job | |
| deploy: | |
| if: github.actor != 'nektos/act' | |
| 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 |