Skip to content

Commit b9b7644

Browse files
committed
chore: CD 세팅
1 parent 061d31e commit b9b7644

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

.github/workflows/cd-dev.yml

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
name: TnT CD for dev with Gradle, Docker, AWS
2+
3+
on:
4+
push:
5+
branches:
6+
- develop
7+
8+
jobs:
9+
move-files:
10+
name: move-files
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout with Submodule
14+
uses: actions/checkout@v4
15+
with:
16+
submodules: true
17+
token: ${{ secrets.SUBMODULE_PAT }}
18+
fetch-depth: 0
19+
20+
- name: Public IP
21+
uses: haythem/[email protected]
22+
23+
- name: Configure AWS Credentials
24+
uses: aws-actions/configure-aws-credentials@v4
25+
with:
26+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
27+
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
28+
aws-region: ${{ secrets.AWS_REGION }}
29+
30+
- name: Github Actions IP 보안그룹 추가
31+
run: |
32+
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
33+
34+
- name: AWS EC2 Create Directory
35+
uses: appleboy/ssh-action@master
36+
with:
37+
host: ${{ secrets.EC2_DEV_HOST }}
38+
username: ${{ secrets.EC2_DEV_USERNAME }}
39+
password: ${{ secrets.EC2_DEV_PASSWORD }}
40+
port: ${{ secrets.EC2_SSH_PORT }}
41+
timeout: 60s
42+
script: |
43+
mkdir -p /home/ubuntu/tnt/
44+
45+
- name: Docker env File
46+
run:
47+
cp src/main/resources/config/dev.env ./infra/.env
48+
49+
- name: AWS EC2 Copy Files
50+
uses: appleboy/scp-action@master
51+
with:
52+
host: ${{ secrets.EC2_DEV_HOST }}
53+
username: ${{ secrets.EC2_DEV_USERNAME }}
54+
password: ${{ secrets.EC2_DEV_PASSWORD }}
55+
port: ${{ secrets.EC2_SSH_PORT }}
56+
timeout: 60s
57+
source: "infra"
58+
target: "/home/ubuntu/tnt"
59+
60+
- name: Github Actions IP 보안그룹 삭제
61+
run: |
62+
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
63+
64+
deploy:
65+
runs-on: ubuntu-latest
66+
steps:
67+
- name: Checkout with Submodule
68+
uses: actions/checkout@v4
69+
with:
70+
submodules: true
71+
token: ${{ secrets.SUBMODULE_PAT }}
72+
fetch-depth: 0
73+
74+
- name: Set up JDK 21
75+
uses: actions/setup-java@v4
76+
with:
77+
java-version: 21
78+
distribution: 'corretto'
79+
80+
- name: Gradle cache
81+
uses: actions/cache@v4
82+
with:
83+
path: |
84+
~/.gradle/caches
85+
~/.gradle/wrapper
86+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
87+
restore-keys: |
88+
${{ runner.os }}-gradle-
89+
90+
- name: Gradle 실행 권한 부여
91+
run: chmod +x gradlew
92+
93+
- name: Build without test
94+
run: ./gradlew build -x test
95+
96+
- name: Docker Buildx setting
97+
uses: docker/[email protected]
98+
99+
- name: Dockerhub Login
100+
uses: docker/[email protected]
101+
with:
102+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
103+
password: ${{ secrets.DOCKER_HUB_TOKEN }}
104+
105+
- name: Dockerhub build and push
106+
uses: docker/[email protected]
107+
with:
108+
context: .
109+
file: ./infra/Dockerfile
110+
push: true
111+
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO }}:latest
112+
build-args: |
113+
"SPRING_ACTIVE_PROFILE=dev"
114+
platforms: |
115+
linux/amd64
116+
linux/arm64
117+
118+
- name: Public IP
119+
uses: haythem/[email protected]
120+
121+
- name: Configure AWS Credentials
122+
uses: aws-actions/configure-aws-credentials@v4
123+
with:
124+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
125+
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
126+
aws-region: ${{ secrets.AWS_REGION }}
127+
128+
- name: Github Actions IP 보안그룹 추가
129+
run: |
130+
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
131+
132+
- name: AWS EC2 Server Deploy
133+
uses: appleboy/ssh-action@master
134+
env:
135+
COMPOSE: "home/ubuntu/infra/docker-compose-dev.yml"
136+
with:
137+
envs: COMPOSE
138+
host: ${{ secrets.EC2_DEV_HOST }}
139+
username: ${{ secrets.EC2_DEV_USERNAME }}
140+
password: ${{ secrets.EC2_DEV_PASSWORD }}
141+
port: ${{ secrets.EC2_SSH_PORT }}
142+
script: |
143+
docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_TOKEN }}
144+
docker-compose -f $COMPOSE down --rmi all
145+
docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO }}
146+
docker-compose -f $COMPOSE up -d
147+
echo "##### EC2 배포 완료 #####"
148+
149+
- name: Github Actions IP 보안그룹 삭제
150+
run: |
151+
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32

infra/Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM amazoncorretto:21
2+
3+
ARG SPRING_ACTIVE_PROFILE
4+
ENV SPRING_ACTIVE_PROFILE ${SPRING_ACTIVE_PROFILE}
5+
6+
COPY build/libs/TnT-0.0.1-SNAPSHOT.jar TnT.jar
7+
8+
ENTRYPOINT ["java", "-jar", "-Duser.timezone=Asia/Seoul", "-Dspring.profiles.active=${SPRING_ACTIVE_PROFILE}", "/TnT.jar"]

infra/docker-compose-dev.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
services:
2+
tnt:
3+
image: ${DOCKER_HUB_USERNAME}/${DOCKER_HUB_REPOSITORY}:latest
4+
container_name: tnt-spring-dev
5+
restart: unless-stopped
6+
expose:
7+
- ${SERVER_PORT}
8+
depends_on:
9+
- mysql
10+
environment:
11+
SPRING_ACTIVE_PROFILE: ${SPRING_ACTIVE_PROFILE}
12+
13+
mysql:
14+
image: mysql:8.0.40
15+
container_name: mysql
16+
platform: linux/arm64/v8
17+
restart: always
18+
ports:
19+
- "3306:3306"
20+
environment:
21+
TZ: Asia/Seoul
22+
MYSQL_DATABASE: ${DEV_MYSQL_DATABASE}
23+
MYSQL_ROOT_PASSWORD: ${DEV_MYSQL_PASSWORD}
24+
MYSQL_USERNAME: ${DEV_MYSQL_USERNAME}
25+
MYSQL_PASSWORD: ${DEV_MYSQL_PASSWORD}
26+
command:
27+
- --skip-character-set-client-handshake
28+
- --character-set-server=utf8mb4
29+
- --collation-server=utf8mb4_unicode_ci
30+
31+
redis:
32+
image: redis:alpine
33+
container_name: redis
34+
platform: linux/arm64
35+
restart: always
36+
command: redis-server
37+
ports:
38+
- "6379:6379"

src/main/resources/config

0 commit comments

Comments
 (0)