-
Notifications
You must be signed in to change notification settings - Fork 1
87 lines (77 loc) · 2.88 KB
/
cd.yml
File metadata and controls
87 lines (77 loc) · 2.88 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
name: CD
on:
push:
branches: [main, develop]
workflow_dispatch:
permissions:
contents: read
concurrency:
group: deploy-${{ github.ref_name }}
cancel-in-progress: false
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v6
with:
python-version: "3.12"
cache: pip
- name: Install dependencies
run: pip install -e ".[dev]"
- name: Run tests
run: python -m pytest tests/ --cov=paperscout --cov-fail-under=90 -v
deploy:
name: Deploy
needs: test
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
runs-on: ubuntu-latest
timeout-minutes: 15
environment: >-
${{ github.ref == 'refs/heads/main' && 'production'
|| github.ref == 'refs/heads/develop' && 'staging'
|| '' }}
steps:
- name: Validate GitHub Environment variables
run: |
set -euo pipefail
err() { echo "::error title=Missing environment variable::$1" >&2; exit 1; }
[ -n "${{ vars.DEPLOY_PATH }}" ] || err "Set Variable DEPLOY_PATH on this job's GitHub Environment (e.g. /opt/paperscout). Settings → Environments → ${{ github.environment }} → Environment variables."
[ -n "${{ vars.DEPLOY_BRANCH }}" ] || err "Set Variable DEPLOY_BRANCH (e.g. main or develop)."
[ -n "${{ vars.HEALTH_PORT }}" ] || err "Set Variable HEALTH_PORT to the host port published for GET /health (see docker-compose / README; e.g. 9101). Without it the workflow builds http://localhost:/health and curl fails."
- name: Deploy via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
port: ${{ secrets.SERVER_PORT || 22 }}
command_timeout: 15m
script: |
set -euo pipefail
cd "${{ vars.DEPLOY_PATH }}"
git fetch origin "${{ vars.DEPLOY_BRANCH }}"
git checkout "${{ vars.DEPLOY_BRANCH }}"
git pull --ff-only origin "${{ vars.DEPLOY_BRANCH }}"
docker compose up -d --build paperscout
- name: Health check
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
port: ${{ secrets.SERVER_PORT || 22 }}
command_timeout: 5m
script: |
set -euo pipefail
url="http://localhost:${{ vars.HEALTH_PORT }}/health"
for i in $(seq 1 12); do
if curl -sf "$url"; then
echo "Health check OK after attempt $i"
exit 0
fi
sleep 5
done
echo "Health check failed after 12 attempts"
exit 1