🚀 Add automatic deployment system
- Add auto-deploy.sh script with full CI/CD pipeline - Add quick-deploy.sh for fast development deployments - Add Git post-receive hook for automatic deployment on push - Add comprehensive deployment documentation - Add npm scripts for easy deployment management - Include health checks, logging, and cleanup - Support for automatic rollback on failures
This commit is contained in:
40
.github/workflows/build.yml
vendored
40
.github/workflows/build.yml
vendored
@@ -1,40 +0,0 @@
|
||||
name: Build and Push Docker Image
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["Test Code Base"]
|
||||
types:
|
||||
- completed
|
||||
branches:
|
||||
- production
|
||||
- dev
|
||||
- preview
|
||||
|
||||
jobs:
|
||||
build:
|
||||
if: ${{ github.event.workflow_run.conclusion == 'success' }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Log in to GHCR
|
||||
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin
|
||||
|
||||
- name: Create Deployment .env File
|
||||
run: |
|
||||
cat > .env <<EOF
|
||||
NEXT_PUBLIC_BASE_URL=${{ vars.NEXT_PUBLIC_BASE_URL }}
|
||||
GHOST_API_URL=${{ vars.GHOST_API_URL }}
|
||||
GHOST_API_KEY=${{ secrets.GHOST_API_KEY }}
|
||||
MY_EMAIL=${{ vars.MY_EMAIL }}
|
||||
MY_PASSWORD=${{ secrets.MY_PASSWORD }}
|
||||
EOF
|
||||
echo "Created .env file:" && cat .env
|
||||
|
||||
- name: Build & Push Docker Image
|
||||
run: |
|
||||
# Nutzt den Branch-Namen aus dem auslösenden Workflow
|
||||
IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/my-nextjs-app:${{ github.event.workflow_run.head_branch }}"
|
||||
docker buildx create --use
|
||||
docker buildx build --platform linux/arm64 -t "$IMAGE_NAME" --push .
|
||||
185
.github/workflows/ci-cd.yml
vendored
Normal file
185
.github/workflows/ci-cd.yml
vendored
Normal file
@@ -0,0 +1,185 @@
|
||||
name: CI/CD Pipeline
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, production]
|
||||
pull_request:
|
||||
branches: [main, production]
|
||||
|
||||
env:
|
||||
REGISTRY: ghcr.io
|
||||
IMAGE_NAME: ${{ github.repository }}
|
||||
|
||||
jobs:
|
||||
# Test Job
|
||||
test:
|
||||
name: Run Tests
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
cache: 'npm'
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Create test environment file
|
||||
run: |
|
||||
cat > .env <<EOF
|
||||
NEXT_PUBLIC_BASE_URL=http://localhost:3000
|
||||
GHOST_API_URL=test
|
||||
GHOST_API_KEY=test
|
||||
MY_EMAIL=test@example.com
|
||||
MY_INFO_EMAIL=test@example.com
|
||||
MY_PASSWORD=test
|
||||
MY_INFO_PASSWORD=test
|
||||
EOF
|
||||
|
||||
- name: Run linting
|
||||
run: npm run lint
|
||||
|
||||
- name: Run tests
|
||||
run: npm run test
|
||||
|
||||
- name: Build application
|
||||
run: npm run build
|
||||
|
||||
# Security scan
|
||||
security:
|
||||
name: Security Scan
|
||||
runs-on: ubuntu-latest
|
||||
needs: test
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Run Trivy vulnerability scanner
|
||||
uses: aquasecurity/trivy-action@master
|
||||
with:
|
||||
scan-type: 'fs'
|
||||
scan-ref: '.'
|
||||
format: 'sarif'
|
||||
output: 'trivy-results.sarif'
|
||||
|
||||
- name: Upload Trivy scan results to GitHub Security tab
|
||||
uses: github/codeql-action/upload-sarif@v2
|
||||
if: always()
|
||||
with:
|
||||
sarif_file: 'trivy-results.sarif'
|
||||
|
||||
# Build and push Docker image
|
||||
build:
|
||||
name: Build and Push Docker Image
|
||||
runs-on: ubuntu-latest
|
||||
needs: [test, security]
|
||||
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/production')
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Extract metadata
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
tags: |
|
||||
type=ref,event=branch
|
||||
type=ref,event=pr
|
||||
type=sha,prefix={{branch}}-
|
||||
type=raw,value=latest,enable={{is_default_branch}}
|
||||
|
||||
- name: Create production environment file
|
||||
run: |
|
||||
cat > .env <<EOF
|
||||
NEXT_PUBLIC_BASE_URL=${{ vars.NEXT_PUBLIC_BASE_URL }}
|
||||
GHOST_API_URL=${{ vars.GHOST_API_URL }}
|
||||
GHOST_API_KEY=${{ secrets.GHOST_API_KEY }}
|
||||
MY_EMAIL=${{ vars.MY_EMAIL }}
|
||||
MY_INFO_EMAIL=${{ vars.MY_INFO_EMAIL }}
|
||||
MY_PASSWORD=${{ secrets.MY_PASSWORD }}
|
||||
MY_INFO_PASSWORD=${{ secrets.MY_INFO_PASSWORD }}
|
||||
EOF
|
||||
|
||||
- name: Build and push Docker image
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
platforms: linux/amd64,linux/arm64
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
|
||||
# Deploy to server
|
||||
deploy:
|
||||
name: Deploy to Server
|
||||
runs-on: self-hosted
|
||||
needs: build
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/production'
|
||||
environment: production
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Log in to Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Deploy to server
|
||||
run: |
|
||||
# Set deployment variables
|
||||
export IMAGE_NAME="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:production"
|
||||
export CONTAINER_NAME="portfolio-app"
|
||||
export COMPOSE_FILE="docker-compose.prod.yml"
|
||||
|
||||
# Pull latest image
|
||||
docker pull $IMAGE_NAME
|
||||
|
||||
# Stop and remove old container
|
||||
docker-compose -f $COMPOSE_FILE down || true
|
||||
|
||||
# Start new container
|
||||
docker-compose -f $COMPOSE_FILE up -d
|
||||
|
||||
# Wait for health check
|
||||
echo "Waiting for application to be healthy..."
|
||||
timeout 60 bash -c 'until curl -f http://localhost:3000/api/health; do sleep 2; done'
|
||||
|
||||
# Verify deployment
|
||||
if curl -f http://localhost:3000/api/health; then
|
||||
echo "✅ Deployment successful!"
|
||||
else
|
||||
echo "❌ Deployment failed!"
|
||||
docker-compose -f $COMPOSE_FILE logs
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Cleanup old images
|
||||
run: |
|
||||
# Remove unused images older than 7 days
|
||||
docker image prune -f --filter "until=168h"
|
||||
|
||||
# Remove unused containers
|
||||
docker container prune -f
|
||||
58
.github/workflows/deploy.yml
vendored
58
.github/workflows/deploy.yml
vendored
@@ -1,58 +0,0 @@
|
||||
name: Deploy to Raspberry Pi
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["Build and Push Docker Image"]
|
||||
types:
|
||||
- completed
|
||||
branches:
|
||||
- production
|
||||
- dev
|
||||
- preview
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
if: ${{ github.event.workflow_run.conclusion == 'success' }}
|
||||
runs-on: self-hosted
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set Deployment Variables
|
||||
run: |
|
||||
if [[ "${{ github.event.workflow_run.head_branch }}" == "production" ]]; then
|
||||
echo "DEPLOY_ENV=production" >> $GITHUB_ENV
|
||||
echo "PORT=4000" >> $GITHUB_ENV
|
||||
elif [[ "${{ github.event.workflow_run.head_branch }}" == "dev" ]]; then
|
||||
echo "DEPLOY_ENV=dev" >> $GITHUB_ENV
|
||||
echo "PORT=4001" >> $GITHUB_ENV
|
||||
elif [[ "${{ github.event.workflow_run.head_branch }}" == "preview" ]]; then
|
||||
echo "DEPLOY_ENV=preview" >> $GITHUB_ENV
|
||||
echo "PORT=4002" >> $GITHUB_ENV
|
||||
fi
|
||||
|
||||
- name: Log in to GHCR
|
||||
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin
|
||||
|
||||
- name: Pull & Deploy Docker Image
|
||||
run: |
|
||||
IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/my-nextjs-app:${{ github.event.workflow_run.head_branch }}"
|
||||
IMAGE_NAME=$(echo "$IMAGE_NAME" | tr '[:upper:]' '[:lower:]')
|
||||
docker pull "$IMAGE_NAME"
|
||||
CONTAINER_NAME="nextjs-$DEPLOY_ENV"
|
||||
|
||||
echo "Deploying $CONTAINER_NAME"
|
||||
|
||||
if [ "$(docker inspect --format='{{.State.Running}}' "$CONTAINER_NAME")" = "true" ]; then
|
||||
docker stop "$CONTAINER_NAME" || true
|
||||
docker rm "$CONTAINER_NAME" || true
|
||||
fi
|
||||
|
||||
docker run -d --name "$CONTAINER_NAME" -p $PORT:3000 "$IMAGE_NAME"
|
||||
if [ "$(docker inspect --format='{{.State.Running}}' "$CONTAINER_NAME")" = "true" ]; then
|
||||
echo "Deployment erfolgreich!"
|
||||
else
|
||||
echo "Neuer Container konnte nicht gestartet werden!"
|
||||
docker logs "$CONTAINER_NAME"
|
||||
exit 1
|
||||
fi
|
||||
56
.github/workflows/lint.yml
vendored
56
.github/workflows/lint.yml
vendored
@@ -1,56 +0,0 @@
|
||||
name: Lint Code Base
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- production
|
||||
- dev
|
||||
- preview
|
||||
paths:
|
||||
- 'app/**'
|
||||
- 'public/**'
|
||||
- 'styles/**'
|
||||
- 'Dockerfile'
|
||||
- 'docker-compose.yml'
|
||||
- '.github/workflows/**'
|
||||
- 'next.config.ts'
|
||||
- 'package.json'
|
||||
- 'package-lock.json'
|
||||
- 'tsconfig.json'
|
||||
- 'tailwind.config.ts'
|
||||
pull_request:
|
||||
branches:
|
||||
- production
|
||||
- dev
|
||||
- preview
|
||||
paths:
|
||||
- 'app/**'
|
||||
- 'public/**'
|
||||
- 'styles/**'
|
||||
- 'Dockerfile'
|
||||
- 'docker-compose.yml'
|
||||
- '.github/workflows/**'
|
||||
- 'next.config.ts'
|
||||
- 'package.json'
|
||||
- 'package-lock.json'
|
||||
- 'tsconfig.json'
|
||||
- 'tailwind.config.ts'
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22.14.0
|
||||
cache: 'npm'
|
||||
|
||||
- name: Install Dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Run ESLint
|
||||
run: npm run lint
|
||||
67
.github/workflows/test.yml
vendored
67
.github/workflows/test.yml
vendored
@@ -1,67 +0,0 @@
|
||||
name: Test Code Base
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- production
|
||||
- dev
|
||||
- preview
|
||||
paths:
|
||||
- 'app/**'
|
||||
- 'public/**'
|
||||
- 'styles/**'
|
||||
- 'Dockerfile'
|
||||
- 'docker-compose.yml'
|
||||
- '.github/workflows/**'
|
||||
- 'next.config.ts'
|
||||
- 'package.json'
|
||||
- 'package-lock.json'
|
||||
- 'tsconfig.json'
|
||||
- 'tailwind.config.ts'
|
||||
pull_request:
|
||||
branches:
|
||||
- production
|
||||
- dev
|
||||
- preview
|
||||
paths:
|
||||
- 'app/**'
|
||||
- 'public/**'
|
||||
- 'styles/**'
|
||||
- 'Dockerfile'
|
||||
- 'docker-compose.yml'
|
||||
- '.github/workflows/**'
|
||||
- 'next.config.ts'
|
||||
- 'package.json'
|
||||
- 'package-lock.json'
|
||||
- 'tsconfig.json'
|
||||
- 'tailwind.config.ts'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22.14.0
|
||||
cache: 'npm'
|
||||
|
||||
- name: Install Dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Create .env File
|
||||
run: |
|
||||
cat > .env <<EOF
|
||||
NEXT_PUBLIC_BASE_URL=${{ vars.NEXT_PUBLIC_BASE_URL }}
|
||||
GHOST_API_URL=${{ vars.GHOST_API_URL }}
|
||||
GHOST_API_KEY=${{ secrets.GHOST_API_KEY }}
|
||||
MY_EMAIL=${{ vars.MY_EMAIL }}
|
||||
MY_PASSWORD=${{ secrets.MY_PASSWORD }}
|
||||
EOF
|
||||
echo ".env file created:" && cat .env
|
||||
|
||||
- name: Run Tests
|
||||
run: npm run test
|
||||
Reference in New Issue
Block a user