-
Notifications
You must be signed in to change notification settings - Fork 0
94 lines (82 loc) · 3.03 KB
/
cd.yml
File metadata and controls
94 lines (82 loc) · 3.03 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
name: CD - Deploy to Server
on:
workflow_run:
workflows: ["CI - Build and Test"]
types: [completed]
branches: [ develop, main ]
env:
DOCKER_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/tech-fork
jobs:
docker-build:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download JAR from CI
uses: actions/download-artifact@v4
with:
name: tech-fork-jar
path: build/libs/
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64
push: true
tags: |
${{ env.DOCKER_IMAGE }}:${{ github.event.workflow_run.head_branch }}
${{ env.DOCKER_IMAGE }}:${{ github.event.workflow_run.head_branch }}-${{ github.event.workflow_run.head_sha }}
deploy:
needs: [docker-build]
runs-on: ubuntu-latest
env:
BRANCH: ${{ github.event.workflow_run.head_branch }}
SPRING_PROFILES_ACTIVE: dev
DB_URL: ${{ secrets.DB_URL }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Copy deployment files to server
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
source: "docker-compose.yml, deploy.sh"
target: "~/deploy/"
- name: Deploy with docker-compose
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
envs: DOCKER_IMAGE,BRANCH,SPRING_PROFILES_ACTIVE,DB_URL,DB_USERNAME,DB_PASSWORD,REDIS_PASSWORD
script: |
cd ~/deploy
chmod +x deploy.sh
./deploy.sh
- name: Health check
run: |
echo "🚀 Starting health check..."
for i in {1..18}; do
response=$(curl -s -o /dev/null -w "%{http_code}" https://techfork.shop/actuator/health || echo "000")
if [ "$response" = "200" ]; then
echo "✅ (Attempt $i/18) Health check successful!"
exit 0
fi
echo "🟡 (Attempt $i/18) Health check failed with status $response. Retrying in 5 seconds..."
sleep 5
done
echo "❌ Health check failed after 90 seconds."
exit 1