Next.js Staging Deployment v1.0
This solution provides automated staging deployment for Next.js applications using GitLab CI/CD and Docker Swarm with Traefik.
đ Quick Install
Online Install (Recommended):
curl -sSL https://stage-tools.tyche.q2.cz/nextjs/v1.0/install.sh | bashDownload & Run:
curl -O https://stage-tools.tyche.q2.cz/nextjs/v1.0/install.sh
chmod +x install.sh
./install.shWeb Interface:
Visit https://stage-tools.tyche.q2.cz for a full solution browser.
Features
- đ Optimized Docker builds with multi-stage building and caching
- đ Automated CI/CD with GitLab pipelines
- đ Traefik integration for automatic SSL and routing
- đĻ Redis support for caching and sessions
- đī¸ Configurable via YAML files
- đĄī¸ Production-ready with security best practices
- Next.js project with
package.json - GitLab repository with CI/CD enabled
- Docker Swarm cluster with Traefik
- GitLab runners with Docker support
- đ Interactive documentation with syntax highlighting
- đž Copy-paste installation commands for multiple methods
- đ Solution browser with status and features
- đ Template previews and configuration examples
Prerequisites
Installation
đ Web Interface (Recommended)
Visit the Stage Tools Web Interface to browse solutions and get installation commands:
https://stage-tools.tyche.q2.cz
The web interface provides:
Manual Installation
If you prefer command-line installation:
Clone stage-tools repository
git clone git@gitlab.com:your-org/stage-tools.git /tmp/stage-tools
Run installer from your Next.js project root
cd /path/to/your/nextjs/project
/tmp/stage-tools/nextjs/default/v1.0/install.sh
Clean up
rm -rf /tmp/stage-tools1. Download the install script:
wget https://gitlab.com/your-org/stage-tools/-/raw/main/nextjs/default/v1.0/install.sh
chmod +x install.sh2. Run the interactive setup:
./install.shConfiguration
The script will ask for:
yourdomain.com)Generated Files
After installation, you'll have:
your-project/
âââ .gitlab-ci.yml # GitLab CI/CD pipeline
âââ .dockerignore # Docker build optimization
âââ .q2/
âââ config/
â âââ config.yaml # Service configuration
âââ staging/
âââ Dockerfile.stage # Optimized Next.js Dockerfile
âââ generate-nextjs-compose.sh # Dynamic compose generatorUsage
1. Configure Next.js for standalone output in
next.config.js:/* @type {import('next').NextConfig} /
const nextConfig = {
output: 'standalone',
// ... other config
}
module.exports = nextConfig2. Push to staging branch (default:
stage):git push origin stage3. Monitor deployment in GitLab CI/CD pipelines
4. Access your application at:
https://[service-name].[domain-suffix]Customization
Environment Variables
Add environment variables in
.q2/config/config.yaml:domain_suffix: yourdomain.com
nextjs:
name: myapp
env:
CUSTOM_VAR: value
backend:
name: myapp-api
redis:
name: redis
port: 6379Build Scripts
The installer detects common build script names:
build:testbuild:prod build:stagebuild (default)Ensure your
package.json has the correct script.Docker Optimization
The generated Dockerfile includes:
Troubleshooting
Common Issues
1. "No package.json found"
- Run the script from your Next.js project root
2. "Build script not found"
- Ensure your package.json has the specified build script
- Update the script name in the installer
3. Deployment fails
- Check GitLab runner has Docker access
- Verify runner tags match configuration
- Check Traefik network exists
4. SSL certificate issues
- Verify your domain DNS points to the server
- Check Traefik certificate resolver configuration
Logs and Debugging
docker service logs [service-name]Advanced Configuration
Custom Dockerfile
To modify the Dockerfile generation, edit:
.q2/staging/generate-nextjs-compose.shMultiple Environments
Copy and modify the solution for different environments:
Create production variant
cp -r .q2/staging .q2/production
Modify configurations as needed
Migration from Existing Setup
If migrating from manual Docker setup:
1. Backup existing files
2. Run the installer
3. Compare and merge custom configurations
4. Update GitLab CI/CD variables
5. Test deployment
Security Considerations
Support
docker build