-
Notifications
You must be signed in to change notification settings - Fork 1
115 lines (93 loc) · 3.66 KB
/
deploy.yml
File metadata and controls
115 lines (93 loc) · 3.66 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
name: Backend Deploy
on:
push:
branches:
- main
tags:
- 'v*'
workflow_dispatch:
env:
IMAGE_NAME: git-ranker
jobs:
docker:
name: Build & Push Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=sha,prefix=
type=semver,pattern={{version}}
- name: Build and Push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
name: Deploy to Production
runs-on: ubuntu-latest
needs: docker
if: github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
echo "=========================================="
echo "Backend Deployment Started"
echo "Time: $(date)"
echo "=========================================="
cd ${{ secrets.DEPLOY_PATH }}
echo "[1/5] Pulling latest image..."
docker compose pull git-ranker-api
echo "[2/5] Restarting container..."
docker compose up -d --no-deps --force-recreate git-ranker-api
echo "[3/5] Waiting for Docker health status..."
for i in {1..12}; do
HEALTH_STATUS=$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}unknown{{end}}' git-ranker-api 2>/dev/null || echo "missing")
echo "Attempt $i/12 - container health: $HEALTH_STATUS"
if [ "$HEALTH_STATUS" = "healthy" ]; then
break
fi
if [ "$HEALTH_STATUS" = "unhealthy" ] || [ "$i" -eq 12 ]; then
echo "Container health check failed."
docker logs --tail 200 git-ranker-api || true
exit 1
fi
sleep 10
done
echo "[4/5] Verifying actuator health endpoint..."
HEALTH_URL="http://127.0.0.1:9090/actuator/health"
RESPONSE=$(curl -sS --connect-timeout 5 --max-time 10 -w '\n%{http_code}' "$HEALTH_URL" || true)
HTTP_STATUS=$(printf '%s' "$RESPONSE" | tail -n1)
RESPONSE_BODY=$(printf '%s' "$RESPONSE" | sed '$d')
if [ "$HTTP_STATUS" != "200" ] || ! echo "$RESPONSE_BODY" | grep -Eq '"status"[[:space:]]*:[[:space:]]*"UP"'; then
echo "Actuator health check failed. HTTP=$HTTP_STATUS, body=$RESPONSE_BODY"
docker logs --tail 200 git-ranker-api || true
exit 1
fi
echo "[5/5] Cleaning up old images..."
docker image prune -f
echo "=========================================="
echo "Backend Deployment Completed!"
echo "=========================================="