-
Notifications
You must be signed in to change notification settings - Fork 1
89 lines (76 loc) · 3.06 KB
/
Copy pathcd.yml
File metadata and controls
89 lines (76 loc) · 3.06 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
name: News Intelligent CD - Deploy to AWS EC2
on:
workflow_run:
# CI 성공 시 수행
workflows:
- "News Intelligent CI"
types:
- completed
jobs:
deploy:
if: ${{github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-22.04
steps:
- name: Checkout repository at CI commit
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 1
- name: List workspace & check compose file
run: |
echo "HEAD SHA: ${{ github.event.workflow_run.head_sha }}"
pwd
ls -al
test -f docker-compose-prod.yml || (echo "compose file not found" && exit 1)
- name: Prepare remote dir
uses: appleboy/ssh-action@v1.2.0
with:
host: ${{ secrets.EC2_SERVER_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
mkdir -p ~/${{ secrets.REPOSITORY_PATH }}
- name: Upload compose file
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.EC2_SERVER_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
source: "docker-compose-prod.yml"
target: "~/${{ secrets.REPOSITORY_PATH }}/"
- name: Deploy to EC2
uses: appleboy/ssh-action@v1.2.0
env:
IMAGE_TAG: ${{ github.event.workflow_run.head_sha }}
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}
with:
host: ${{ secrets.EC2_SERVER_IP }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
envs: IMAGE_TAG,DOCKERHUB_USERNAME,DOCKERHUB_REPOSITORY
command_timeout: 30m
script: |
set -eo pipefail
cd ~/${{ secrets.REPOSITORY_PATH }}
export IMAGE_TAG="$IMAGE_TAG"
export DOCKERHUB_USERNAME="$DOCKERHUB_USERNAME"
export DOCKERHUB_REPOSITORY="$DOCKERHUB_REPOSITORY"
docker-compose -f docker-compose-prod.yml down --remove-orphans || true
docker-compose -f docker-compose-prod.yml pull
docker-compose -f docker-compose-prod.yml up -d --force-recreate --remove-orphans
echo "Health-check: http://127.0.0.1:8081/actuator/health/readiness"
for i in $(seq 1 30); do
if curl -fsS "http://127.0.0.1:8081/actuator/health/readiness" | grep -q '"status":"UP"'; then
echo "App is healthy "; break
fi
echo "waiting health... ($i)"
sleep 2
done
if ! curl -fsS "http://127.0.0.1:8081/actuator/health/readiness" | grep -q '"status":"UP"'; then
echo "App failed to become healthy "
docker-compose -f docker-compose-prod.yml ps || true
docker-compose -f docker-compose-prod.yml logs --tail=200 || true
exit 1
fi
docker image prune -f