📝 priettier check
All checks were successful
PR Checks / lint-and-build (pull_request) Successful in 18s

This commit is contained in:
RJ
2025-12-04 15:57:39 +02:00
parent b68325123b
commit 101624c4d5
32 changed files with 185 additions and 172 deletions

View File

@@ -5,6 +5,7 @@
This guide documents the configuration for build-time environment variables in the Next.js CI/CD pipeline.
**Problem Solved:** Next.js 16 requires `NEXT_PUBLIC_*` variables available during `npm run build` for:
- SEO metadata (`metadataBase`)
- Sitemap generation
- OpenGraph URLs
@@ -19,6 +20,7 @@ This guide documents the configuration for build-time environment variables in t
### 1. `.gitea/workflows/main.yml`
**Changes:**
- Added step to create `.env` from Gitea secrets (after checkout)
- Added cleanup step to remove `.env` after Docker push
@@ -34,7 +36,7 @@ This guide documents the configuration for build-time environment variables in t
NODE_ENV=production
NEXT_TELEMETRY_DISABLED=1
EOF
echo "✅ .env file created successfully"
echo "Preview (secrets masked):"
cat .env | sed 's/=.*/=***MASKED***/g'
@@ -44,7 +46,7 @@ This guide documents the configuration for build-time environment variables in t
- name: 🚀 Push Docker image to registry
run: |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
# Clean up sensitive files
rm -f .env
echo "✅ Cleaned up .env file"
@@ -55,6 +57,7 @@ This guide documents the configuration for build-time environment variables in t
### 2. `Dockerfile.nextjs`
**Changes:**
- Added `COPY .env* ./` in builder stage (after copying node_modules, before copying source code)
**Added Section:**
@@ -73,6 +76,7 @@ COPY .env* ./
### 3. `.dockerignore`
**Changes:**
- Modified to allow `.env` file (created by CI/CD) while excluding other `.env*` files
**Updated Section:**
@@ -85,6 +89,7 @@ COPY .env* ./
```
**Explanation:**
- `.env*` excludes all environment files
- `!.env` creates exception for main `.env` (from CI/CD)
- `.env.local`, `.env.development`, `.env.production.local` remain excluded
@@ -99,11 +104,12 @@ Navigate to: **Repository Settings → Secrets**
Add the following secret:
| Secret Name | Value | Type | Description |
|------------|-------|------|-------------|
| Secret Name | Value | Type | Description |
| ---------------------- | ------------------------ | ------------------ | ------------------- |
| `NEXT_PUBLIC_SITE_URL` | `https://yourdomain.com` | Secret or Variable | Production site URL |
**Notes:**
- Can be configured as **Secret** (masked in logs) or **Variable** (visible in logs)
- Recommended: Use **Variable** since it's a public URL
- For sensitive values (API keys), always use **Secret**
@@ -113,12 +119,14 @@ Add the following secret:
To add more build-time variables:
1. **Add to Gitea Secrets/Variables:**
```
NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX
NEXT_PUBLIC_API_URL=https://api.example.com
```
2. **Update workflow `.env` creation step:**
```yaml
cat > .env << EOF
NEXT_PUBLIC_SITE_URL=${{ secrets.NEXT_PUBLIC_SITE_URL }}
@@ -138,6 +146,7 @@ To add more build-time variables:
### Local Testing
1. **Create test `.env` file:**
```bash
cat > .env << EOF
NEXT_PUBLIC_SITE_URL=http://localhost:3030
@@ -147,24 +156,27 @@ To add more build-time variables:
```
2. **Build Docker image:**
```bash
docker build -t mypage:test -f Dockerfile.nextjs .
```
3. **Verify variable is embedded (should show "NOT FOUND" - correct behavior):**
```bash
docker run --rm mypage:test node -e "console.log(process.env.NEXT_PUBLIC_SITE_URL || 'NOT FOUND')"
```
**Expected Output:** `NOT FOUND`
**Why?** `NEXT_PUBLIC_*` variables are embedded in JavaScript bundle during build, NOT available as runtime environment variables.
4. **Test application starts:**
```bash
docker run --rm -p 3030:3030 mypage:test
```
Visit `http://localhost:3030` to verify.
5. **Cleanup:**
@@ -214,10 +226,10 @@ To add more build-time variables:
### 🔒 Sensitive Data Guidelines
| Type | Use For | Access |
|------|---------|--------|
| `NEXT_PUBLIC_*` | Client-side config (URLs, feature flags) | Public (embedded in JS bundle) |
| `SECRET_*` | Server-side secrets (API keys, DB passwords) | Private (runtime only) |
| Type | Use For | Access |
| --------------- | -------------------------------------------- | ------------------------------ |
| `NEXT_PUBLIC_*` | Client-side config (URLs, feature flags) | Public (embedded in JS bundle) |
| `SECRET_*` | Server-side secrets (API keys, DB passwords) | Private (runtime only) |
---
@@ -226,10 +238,12 @@ To add more build-time variables:
### Issue: Variables not available during build
**Symptoms:**
- Next.js build errors about missing `NEXT_PUBLIC_SITE_URL`
- Metadata/sitemap generation fails
**Solution:**
- Verify `NEXT_PUBLIC_SITE_URL` secret exists in Gitea
- Check workflow logs for `.env` creation step
- Ensure `.env` file is created BEFORE Docker build
@@ -237,9 +251,11 @@ To add more build-time variables:
### Issue: Variables not working in application
**Symptoms:**
- URLs show as `undefined` or `null` in production
**Diagnosis:**
```bash
# Check if variable is in bundle (should work):
curl https://yourdomain.com | grep -o 'NEXT_PUBLIC_SITE_URL'
@@ -249,6 +265,7 @@ docker exec mypage-prod node -e "console.log(process.env.NEXT_PUBLIC_SITE_URL)"
```
**Solution:**
- Verify `.env` was copied during Docker build
- Check Dockerfile logs for `COPY .env* ./` step
- Rebuild with `--no-cache` if needed
@@ -256,9 +273,11 @@ docker exec mypage-prod node -e "console.log(process.env.NEXT_PUBLIC_SITE_URL)"
### Issue: `.env` file not found during Docker build
**Symptoms:**
- Docker build warning: `COPY .env* ./` - no files matched
**Solution:**
- Check `.dockerignore` allows `.env` file
- Verify workflow creates `.env` BEFORE Docker build
- Check file exists: `ls -la .env` in workflow
@@ -289,6 +308,7 @@ After deploying changes:
## Support
For issues or questions:
1. Check workflow logs in Gitea Actions
2. Review Docker build logs
3. Verify Gitea secrets configuration