-
Notifications
You must be signed in to change notification settings - Fork 1
142 lines (120 loc) · 4.65 KB
/
backend-cd-dev.yml
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: backend-cd-dev
on:
push:
branches:
- develop
paths:
- backend/**
env:
DOCKERHUB_REPOSITORY: ody-official
jobs:
build-and-push:
runs-on: ubuntu-latest
env:
TZ: 'Asia/Seoul'
defaults:
run:
working-directory: backend
steps:
- uses: actions/checkout@v4
with:
ref: feature/927
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Check system timezone
run: |
echo "Current date and time: $(date)"
echo "TZ environment variable: $TZ"
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
- name: Clean Build With Gradle Wrapper
run: ./gradlew clean build
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: ${{ github.workspace }}/backend/build/test-results/**/*.xml
- name: JUnit Report Action
uses: mikepenz/action-junit-report@v4
if: always()
with:
report_paths: ${{ github.workspace }}/backend/build/test-results/**/*.xml
- name: Setup 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_PASSWORD }}
- name: Back Up Image For Rollback
run: |
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest || true
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous || true
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous || true
- name: Docker Image Build And Push
run: docker build --platform linux/arm64 -t ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-dev -f Dockerfile . --push
pull-and-deploy:
needs: build-and-push
runs-on: dev
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Clean Up Legacy Image
run: |
export DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}
export DOCKERHUB_REPOSITORY=$DOCKERHUB_REPOSITORY
export DEV_MYSQL_PASSWORD=${{ secrets.DEV_MYSQL_PASSWORD }}
export JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }}
export TAG=${{ github.sha }}-dev
docker compose down || true
docker rmi $(docker images -q) -f || true
- name: Docker Compose up
run: |
export DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}
export DOCKERHUB_REPOSITORY=$DOCKERHUB_REPOSITORY
export DEV_MYSQL_PASSWORD=${{ secrets.DEV_MYSQL_PASSWORD }}
export JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }}
export TAG=${{ github.sha }}-dev
docker compose up -d
- name: Health Check with Retry
uses: nick-invision/retry@v2
with:
timeout_minutes: 1
max_attempts: 5
retry_wait_seconds: 6
command: |
response=$(curl -s https://dev.oody.site/actuator/health)
status=$(echo $response | jq -r '.status')
if [ "$status" = "UP" ]; then
echo "Status is UP. Continuing..."
exit 0
else
echo "Status is NOT UP."
exit 1
fi
- name: Rollback if Health Check fails
if: failure()
run: |
docker compose down || true
docker rmi $(docker images -q) -f || true
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:previous
export DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}
export DOCKERHUB_REPOSITORY=$DOCKERHUB_REPOSITORY
export DEV_MYSQL_PASSWORD=${{ secrets.DEV_MYSQL_PASSWORD }}
export JASYPT_ENCRYPTOR_PASSWORD=${{ secrets.JASYPT_PASSWORD }}
export TAG=previous
docker compose up -d
- name: Tag successful deployment as latest
if: success()
run: |
docker tag ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:${{ github.sha }}-dev ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest
docker push ${{ secrets.DOCKERHUB_USERNAME }}/$DOCKERHUB_REPOSITORY:latest
- name: Check Docker Process
if: always()
run: docker ps