59 lines
1.4 KiB
YAML
59 lines
1.4 KiB
YAML
name: FastAPI CI/CD
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
pull_request:
|
|
|
|
jobs:
|
|
test-and-deploy:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup Python
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: "3.12"
|
|
|
|
- name: Install dependencies
|
|
run: |
|
|
python -m pip install --upgrade pip
|
|
pip install -r requirements.txt
|
|
|
|
- name: Run tests
|
|
run: pytest -v
|
|
|
|
- name: Build Docker image
|
|
run: docker build -t fastapi-demo:latest .
|
|
|
|
- name: Deploy to home server
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
run: |
|
|
docker rm -f fastapi-demo || true
|
|
|
|
docker run -d \
|
|
--name fastapi-demo \
|
|
--network web \
|
|
--restart unless-stopped \
|
|
-p 127.0.0.1:8000:8000 \
|
|
fastapi-demo:latest
|
|
|
|
- name: Health check
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
run: |
|
|
for i in $(seq 1 15); do
|
|
if docker exec fastapi-demo python -c \
|
|
"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2)"; then
|
|
echo "Deployment successful!"
|
|
exit 0
|
|
fi
|
|
sleep 2
|
|
done
|
|
|
|
docker logs fastapi-demo
|
|
exit 1
|