Files
portfolio/.github/workflows/deploy.yml

60 lines
2.0 KiB
YAML

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 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"
- name: Zero-Downtime Deployment
run: |
CONTAINER_NAME="nextjs-$DEPLOY_ENV"
echo "Deploying $CONTAINER_NAME"
if [ "$(docker inspect --format='{{.State.Running}}' "$NEW_CONTAINER_NAME")" = "true" ]; then
docker stop "$CONTAINER_NAME" || true
docker rm "$CONTAINER_NAME" || true
docker run -d --name "$CONTAINER_NAME" -p $PORT:3000 "$IMAGE_NAME"
echo "Deployment erfolgreich!"
else
echo "Neuer Container konnte nicht gestartet werden!"
docker logs "$CONTAINER_NAME"
exit 1
fi