Fix NEXT_PUBLIC environment variables for production builds
Some checks failed
Build and Push Docker Images / docker (push) Failing after 1m56s

Problem:
- NEXT_PUBLIC_* variables must be baked into Next.js build at BUILD TIME
- Setting them in docker-compose is too late (bundle already built)
- This caused "NEXT_PUBLIC_WREN_API_TOKEN is undefined" errors in production

Solution:
1. Updated Dockerfile to accept ARG values for all NEXT_PUBLIC_* variables
2. Set ARGs as ENV variables before npm run build (lines 15-26)
3. Updated CI/CD workflow to pass build-args from Gitea secrets/vars
4. Variables are now baked into the image during build

Next Steps:
1. Add these secrets to Gitea repository settings:
   - NEXT_PUBLIC_WREN_API_TOKEN
   - NEXT_PUBLIC_FORMSPREE_CONTACT_ID
   - NEXT_PUBLIC_FORMSPREE_OFFSET_ID
   - NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY

2. Add this variable to Gitea repository settings:
   - NEXT_PUBLIC_API_BASE_URL

3. Next push will build image with variables baked in
4. Can simplify docker-compose (remove NEXT_PUBLIC_* from web service)

Files Changed:
- Dockerfile: Added ARG and ENV declarations before build step
- .gitea/workflows/build-deploy.yml: Added build-args to frontend image build

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Matt 2025-11-03 10:47:27 +01:00
parent a6484de35e
commit bfe5897232
2 changed files with 22 additions and 1 deletions

View File

@ -30,6 +30,12 @@ jobs:
file: ./Dockerfile file: ./Dockerfile
platforms: linux/amd64 platforms: linux/amd64
push: true push: true
build-args: |
NEXT_PUBLIC_API_BASE_URL=${{ vars.NEXT_PUBLIC_API_BASE_URL }}
NEXT_PUBLIC_WREN_API_TOKEN=${{ secrets.NEXT_PUBLIC_WREN_API_TOKEN }}
NEXT_PUBLIC_FORMSPREE_CONTACT_ID=${{ secrets.NEXT_PUBLIC_FORMSPREE_CONTACT_ID }}
NEXT_PUBLIC_FORMSPREE_OFFSET_ID=${{ secrets.NEXT_PUBLIC_FORMSPREE_OFFSET_ID }}
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=${{ secrets.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY }}
tags: | tags: |
${{ vars.REGISTRY_HOST }}/${{ vars.REGISTRY_USERNAME }}/${{ vars.IMAGE_NAME }}:frontend-latest ${{ vars.REGISTRY_HOST }}/${{ vars.REGISTRY_USERNAME }}/${{ vars.IMAGE_NAME }}:frontend-latest
${{ vars.REGISTRY_HOST }}/${{ vars.REGISTRY_USERNAME }}/${{ vars.IMAGE_NAME }}:frontend-main-${{ github.sha }} ${{ vars.REGISTRY_HOST }}/${{ vars.REGISTRY_USERNAME }}/${{ vars.IMAGE_NAME }}:frontend-main-${{ github.sha }}

View File

@ -10,8 +10,23 @@ RUN npm ci
# Copy the rest of the app # Copy the rest of the app
COPY . . COPY . .
# Accept build arguments for NEXT_PUBLIC_ variables
# These MUST be provided at build time
ARG NEXT_PUBLIC_API_BASE_URL
ARG NEXT_PUBLIC_WREN_API_TOKEN
ARG NEXT_PUBLIC_FORMSPREE_CONTACT_ID
ARG NEXT_PUBLIC_FORMSPREE_OFFSET_ID
ARG NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
# Set as environment variables so Next.js can bake them into the build
ENV NEXT_PUBLIC_API_BASE_URL=$NEXT_PUBLIC_API_BASE_URL
ENV NEXT_PUBLIC_WREN_API_TOKEN=$NEXT_PUBLIC_WREN_API_TOKEN
ENV NEXT_PUBLIC_FORMSPREE_CONTACT_ID=$NEXT_PUBLIC_FORMSPREE_CONTACT_ID
ENV NEXT_PUBLIC_FORMSPREE_OFFSET_ID=$NEXT_PUBLIC_FORMSPREE_OFFSET_ID
ENV NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=$NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
# Build Next.js app (standalone mode) # Build Next.js app (standalone mode)
# Environment variables with NEXT_PUBLIC_ prefix are baked in at build time # NEXT_PUBLIC_ variables are now baked in at build time
RUN npm run build RUN npm run build
# Production Stage - Next.js standalone server # Production Stage - Next.js standalone server