From 82953ea1b32fd5768df1ea02ea2e21d1cde379c4 Mon Sep 17 00:00:00 2001 From: Denshooter Date: Mon, 10 Feb 2025 15:27:39 +0100 Subject: [PATCH] update bug fix --- .github/workflows/main.yml | 27 +++++++++++++++------------ Dockerfile | 15 ++++++++++----- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8f60a98..65387cd 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,7 @@ on: jobs: deploy: - runs-on: self-hosted # Der Runner sollte auf dem Raspberry Pi laufen + runs-on: self-hosted # Der Runner sollte auf dem Raspberry Pi laufen steps: - name: Checkout Code @@ -39,25 +39,28 @@ jobs: IMAGE_NAME="my-nextjs-app:$DEPLOY_ENV" NEW_CONTAINER_NAME="$CONTAINER_NAME-new" - # Pull latest image version if exists - docker pull $IMAGE_NAME || true + # Prüft, ob der alte Container existiert und entfernt ihn + if [ $(docker ps -aq -f name=$NEW_CONTAINER_NAME) ]; then + echo "Removing existing new container ($NEW_CONTAINER_NAME)..." + docker rm -f "$NEW_CONTAINER_NAME" || true + fi - # Run new container on a different port + # Führt den neuen Container auf einem temporären internen Port aus docker run -d --name "$NEW_CONTAINER_NAME" -p 30000:3000 $IMAGE_NAME - # Wait to ensure the new container is up and running + # Warten, um sicherzustellen, dass der neue Container läuft sleep 10 - # Check if new container is healthy and running + # Prüfen, ob der neue Container erfolgreich läuft 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 + # Stoppt und entfernt den alten Container, falls vorhanden + if [ $(docker ps -aq -f name=$CONTAINER_NAME) ]; then + docker stop "$CONTAINER_NAME" || true + docker rm "$CONTAINER_NAME" || true + fi - # Rename new container to the old container’s name + # Benennt den neuen Container zum alten Namen um und weist den gewünschten Port zu 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." diff --git a/Dockerfile b/Dockerfile index 23ba3cb..9fd3e6e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,21 +1,26 @@ +# Use Node.js LTS image as the base FROM node:current-alpine +# Set working directory WORKDIR /app -# npm Cache reinigen, npm aktualisieren und danach die Abhängigkeiten installieren -RUN npm cache clean --force -RUN npm install -g npm@latest - +# Copy package.json and package-lock.json COPY package*.json ./ -RUN npm install --loglevel verbose || npm install --force +# Install dependencies +RUN npm install +# Copy the application code COPY . . +# Build the Next.js application RUN npm run build +# Set environmental variable for production mode ENV NODE_ENV=production +# Expose the port the app runs on EXPOSE 3000 +# Run the app with the start script CMD ["npm", "start"]