-
Notifications
You must be signed in to change notification settings - Fork 0
117 lines (95 loc) · 3.17 KB
/
Copy pathdevelop.yaml
File metadata and controls
117 lines (95 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
name: Deploy to Production
on:
push:
branches:
- main
pull_request:
branches:
- main
env:
APP_NAME: fastapi-app # Change this to your app name
DEPLOY_PATH: /opt/apps # Change this to your deployment path
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.13'
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run linters
run: |
pip install ruff
ruff check .
- name: Run tests
run: |
pip install pytest pytest-cov
pytest --cov=. --cov-report=xml
build:
runs-on: ubuntu-latest
needs: test
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub (optional)
if: env.DOCKER_USERNAME != ''
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./docker/Dockerfile
push: false
tags: ${{ env.APP_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
runs-on: ubuntu-latest
needs: build
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts
- name: Create config.ini file
run: |
echo "${{ secrets.CONFIG_INI }}" > config.ini
- name: Create alembic.ini file
run: |
echo "${{ secrets.ALEMBIC_INI }}" > alembic.ini
- name: Copy files to remote server
run: |
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "mkdir -p ${{ env.DEPLOY_PATH }}/${{ env.APP_NAME }}"
rsync -avz --exclude='.git' --exclude='__pycache__' --exclude='*.pyc' \
./ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:${{ env.DEPLOY_PATH }}/${{ env.APP_NAME }}/
- name: Deploy with Docker Compose
run: |
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'EOF'
cd ${{ env.DEPLOY_PATH }}/${{ env.APP_NAME }}
docker compose -f docker/docker-compose.yml down
docker compose -f docker/docker-compose.yml up -d --build
docker compose -f docker/docker-compose.yml ps
EOF
- name: Health check
run: |
sleep 10
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} \
"curl -f http://localhost:8000/health || exit 1"