VERSION 0.8
FROM --platform=linux/amd64 node:latest
WORKDIR /app

deps:
    # Install the Netlify CLI (global)
    RUN npm install netlify-cli -g
    # Copy package.json for installing required packages
    COPY package.json ./
    # Install the Netlify CLI (local) and Netlify Next.js plugin and required packages
    RUN npm install netlify-cli --save-dev
    RUN npm install @netlify/plugin-nextjs --save-dev
    RUN npm install
    # Copy netlify.toml - required for building and deploying to Netlify
    COPY netlify.toml ./

build:
    FROM +deps
    # Copy files and directories required for building
    COPY next-env.d.ts styles.module.css tsconfig.json ./
    COPY --dir pages ./
    # Build site using NETLIFY_AUTH_TOKEN and NETLIFY_SITE_ID secrets
    RUN --secret NETLIFY_AUTH_TOKEN --secret NETLIFY_SITE_ID netlify build --context production
    SAVE ARTIFACT ./node_modules node_modules/ AS LOCAL ./
    SAVE ARTIFACT ./.next .next/ AS LOCAL ./
    SAVE ARTIFACT ./.netlify .netlify/ AS LOCAL ./

deploy:
    FROM +deps
    # Copy artifacts required for deploying to Netlify
    COPY +build/node_modules/ +build/.next/ +build/.netlify/ ./
    # Deploy site
    RUN --push --secret NETLIFY_AUTH_TOKEN --secret NETLIFY_SITE_ID netlify deploy --prod
