Files
portfolio/.github/workflows/main.yml
2025-02-10 11:19:21 +01:00

67 lines
2.2 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Deploy Next.js to Raspberry Pi
on:
push:
branches:
- production
- dev
- preview
jobs:
deploy:
runs-on: self-hosted # Der Runner sollte auf dem Raspberry Pi laufen
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set Environment Variables
run: |
if [[ "${{ github.ref_name }}" == "production" ]]; then
echo "DEPLOY_ENV=production" >> $GITHUB_ENV
echo "PORT=3000" >> $GITHUB_ENV
elif [[ "${{ github.ref_name }}" == "dev" ]]; then
echo "DEPLOY_ENV=dev" >> $GITHUB_ENV
echo "PORT=3001" >> $GITHUB_ENV
elif [[ "${{ github.ref_name }}" == "preview" ]]; then
echo "DEPLOY_ENV=preview" >> $GITHUB_ENV
echo "PORT=3002" >> $GITHUB_ENV
fi
- name: Build Docker Image
run: |
IMAGE_NAME="my-nextjs-app:$DEPLOY_ENV"
docker build -t $IMAGE_NAME .
- name: Deploy on Raspberry Pi (Zero-Downtime)
run: |
CONTAINER_NAME="nextjs-$DEPLOY_ENV"
IMAGE_NAME="my-nextjs-app:$DEPLOY_ENV"
NEW_CONTAINER_NAME="$CONTAINER_NAME-new"
# Pull latest image version if exists
docker pull $IMAGE_NAME || true
# Run new container on a different port
docker run -d --name "$NEW_CONTAINER_NAME" -p 30000:3000 $IMAGE_NAME
# Wait to ensure the new container is up and running
sleep 10
# Check if new container is healthy and running
if [ $(docker inspect --format='{{.State.Running}}' $NEW_CONTAINER_NAME) == "true" ]; then
# Stop and remove the old container
docker stop "$CONTAINER_NAME" || true
docker rm "$CONTAINER_NAME" || true
# Rename new container to the old containers name
docker rename "$NEW_CONTAINER_NAME" "$CONTAINER_NAME"
# Reassign the desired port (e.g., 3000 for production branch) to the new container
docker run -d --name "$CONTAINER_NAME" -p $PORT:3000 $IMAGE_NAME
else
echo "New container failed to start."
docker logs $NEW_CONTAINER_NAME
exit 1
fi