Files
stiftung-management-system/.github/workflows/ci-cd.yml
Stiftung Development 175954ae67 Fix deployment script issues
- Use HTTPS git pull instead of SSH to avoid authentication issues
- Fix docker-compose file path (copy from deploy-production/)
- Use docker-compose with hyphen instead of 'docker compose'
- Add -T flag to exec commands for non-interactive mode
- Add container status check at the end
- Improve error handling and logging
2025-09-09 21:07:37 +02:00

217 lines
5.7 KiB
YAML

name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_stiftung
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
redis:
image: redis:7-alpine
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Cache pip dependencies
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('app/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y postgresql-client
python -m pip install --upgrade pip
pip install -r app/requirements.txt
- name: Set up environment
run: |
cp env-template.txt app/.env
cd app
echo "DJANGO_DEBUG=1" >> .env
echo "DJANGO_SECRET_KEY=test-secret-key-for-ci" >> .env
echo "POSTGRES_DB=test_stiftung" >> .env
echo "POSTGRES_USER=postgres" >> .env
echo "POSTGRES_PASSWORD=postgres" >> .env
echo "DB_HOST=localhost" >> .env
echo "DB_PORT=5432" >> .env
echo "REDIS_URL=redis://localhost:6379/0" >> .env
- name: Wait for PostgreSQL
run: |
while ! pg_isready -h localhost -p 5432 -U postgres; do
echo "Waiting for PostgreSQL..."
sleep 2
done
echo "PostgreSQL is ready!"
- name: Run migrations
working-directory: ./app
env:
DJANGO_DEBUG: "1"
DJANGO_SECRET_KEY: "test-secret-key-for-ci"
POSTGRES_DB: "test_stiftung"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
DB_HOST: "localhost"
DB_PORT: "5432"
run: |
python manage.py migrate
- name: Run tests
working-directory: ./app
env:
DJANGO_DEBUG: "1"
DJANGO_SECRET_KEY: "test-secret-key-for-ci"
POSTGRES_DB: "test_stiftung"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
DB_HOST: "localhost"
DB_PORT: "5432"
run: |
python manage.py test
- name: Check Django configuration
working-directory: ./app
env:
DJANGO_DEBUG: "1"
DJANGO_SECRET_KEY: "test-secret-key-for-ci"
POSTGRES_DB: "test_stiftung"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
DB_HOST: "localhost"
DB_PORT: "5432"
run: |
python manage.py check --deploy
- name: Collect static files
working-directory: ./app
env:
DJANGO_DEBUG: "1"
DJANGO_SECRET_KEY: "test-secret-key-for-ci"
POSTGRES_DB: "test_stiftung"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
DB_HOST: "localhost"
DB_PORT: "5432"
run: |
python manage.py collectstatic --noinput
build:
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'push'
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
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: 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}}-
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ./app
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' # Auto-deploy when pushing to main branch
environment: production
steps:
- name: Deploy to production
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.PROD_HOST }}
username: ${{ secrets.PROD_USERNAME }}
key: ${{ secrets.PROD_SSH_KEY }}
script: |
cd /opt/stiftung
# Pull latest changes (using HTTPS)
git pull https://github.com/remmerinio/stiftung-management-system.git main
# Copy production docker compose file
cp deploy-production/docker-compose.prod.yml docker-compose.yml
# Stop containers
docker-compose down
# Start containers with rebuild
docker-compose up -d --build
# Wait for containers to be ready
echo "Waiting for containers to start..."
sleep 30
# Run migrations
docker-compose exec -T web python manage.py migrate
# Collect static files
docker-compose exec -T web python manage.py collectstatic --noinput
# Show container status
docker-compose ps