Files
portfolio/.github/workflows/deploy.yml
denshooter 4aa0ba662b dev (#42)
* 🚀 refactor: simplify deployment process in workflow file

* 🚀 chore: add IMAGE_NAME to GITHUB_ENV for deployment workflow

*  chore: simplify deployment logging in workflow file

* 🚀 fix: correct container name in deployment script logic
2025-02-21 16:31:14 +01:00

62 lines
2.1 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}}' "$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