Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Deploy to EC2

on:
push:
branches:
- production

env:
AWS_REGION: ap-northeast-2
AWS_S3_BUCKET: herewe-deploy-bucket
AWS_CODE_DEPLOY_APPLICATION: HEREWE-Application
AWS_CODE_DEPLOY_GROUP: HEREWE-Deployment-Group
DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }}
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_TOKEN }}
IMAGE_NAME: herewe/herewe-server

jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

services:
redis:
image: redis:latest
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.ACTIONS_TOKEN }}
submodules: true

- name: Create directories and copy yml files
run: |
cp HERE-WE-SECRET/.env .

mkdir -p src/main/resources
cp HERE-WE-SECRET/main/resources/application.yml src/main/resources/
cp HERE-WE-SECRET/main/resources/application-prod.yml src/main/resources/
cp HERE-WE-SECRET/main/resources/application-oauth.yml src/main/resources/
cp HERE-WE-SECRET/main/resources/application-common.yml src/main/resources/

mkdir -p src/test/resources
cp HERE-WE-SECRET/test/resources/application.yml src/test/resources/
cp HERE-WE-SECRET/test/resources/application-test.yml src/test/resources/

echo "Main resources contents:"
ls -la src/main/resources/
echo "Test resources contents:"
ls -la src/test/resources/

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Test Redis Connection
run: |
sudo apt-get install -y redis-tools
redis-cli ping

- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

- name: Build with Gradle and Test
run: ./gradlew clean build

# Docker Hub 로그인
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ env.DOCKER_HUB_USERNAME }}
password: ${{ env.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build and Push Docker Image
run: |
docker build --platform=linux/amd64 -t $IMAGE_NAME .
docker tag $IMAGE_NAME $IMAGE_NAME:latest
docker push $IMAGE_NAME:latest

- name: AWS credential 설정
uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.CICD_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.CICD_SECRET_KEY }}

- name: Copy .env file
run: cp HERE-WE-SECRET/.env .

- name: Package and Upload to S3
run: |
mkdir -p deploy/scripts
cp appspec.yml deploy/
cp docker-compose.yml deploy/
cp HERE-WE-SECRET/.env deploy/
cp HERE-WE-SECRET/main/resources/*.yml deploy/
cp scripts/deploy.sh deploy/scripts/
cd deploy
zip -r deploy.zip .
aws s3 cp deploy.zip s3://$AWS_S3_BUCKET/deploy.zip --region $AWS_REGION

- name: Deploy with CodeDeploy
run: |
aws deploy create-deployment \
--application-name $AWS_CODE_DEPLOY_APPLICATION \
--deployment-group-name $AWS_CODE_DEPLOY_GROUP \
--s3-location bucket=$AWS_S3_BUCKET,bundleType=zip,key=deploy.zip \
--region $AWS_REGION
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build/
!**/src/test/**/build/

*.yml
.env

### STS ###
.apt_generated
Expand Down Expand Up @@ -42,4 +43,5 @@ out/
### MAC ###
*.DS_Store
src/main/generated/**
/.idea
/.idea
/mysql_data
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM openjdk:17-jdk-slim

# curl 설치 추가
RUN apt-get update && apt-get install -y curl && apt-get clean

WORKDIR /app

COPY build/libs/HereWeApplication.jar HereWeApplication.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "HereWeApplication.jar"]
2 changes: 1 addition & 1 deletion HERE-WE-SECRET
30 changes: 30 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: 0.0
os: linux

files:
- source: appspec.yml
destination: /home/ubuntu/app
- source: docker-compose.yml
destination: /home/ubuntu/app
- source: scripts/
destination: /home/ubuntu/app/scripts
- source: .env
destination: /home/ubuntu/app

# ✅ 여기에 설정 파일 추가
- source: application-prod.yml
destination: /home/ubuntu/app
- source: application-oauth.yml
destination: /home/ubuntu/app
- source: application-common.yml
destination: /home/ubuntu/app

permissions:
- object: /home/ubuntu/app
owner: ubuntu
group: ubuntu

hooks:
ApplicationStart:
- location: scripts/deploy.sh
timeout: 60
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies {

//DB
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'mysql:mysql-connector-java:8.0.33'
implementation 'com.mysql:mysql-connector-j:8.0.33'
testRuntimeOnly 'com.h2database:h2:2.2.222'

// Redis
Expand Down
60 changes: 60 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
services:
server:
image: herewe/herewe-server:latest
ports:
- 8080:8080
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/${MYSQL_DATABASE}?serverTimezone=UTC&characterEncoding=UTF-8
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=${MYSQL_ROOT_PASSWORD}
- SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.cj.jdbc.Driver
- SPRING_PROFILES_ACTIVE=prod,oauth,common
- SPRING_CONFIG_ADDITIONAL_LOCATION=optional:file:/config/
volumes:
- ./configs:/config # <== 이 폴더 안에 yml 파일들 넣어두기
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:8080/api/auth/health-check" ]
interval: 10s
timeout: 3s
retries: 5
start_period: 15s

db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
volumes:
- ./mysql_data:/var/lib/mysql
ports:
- 3306:3306
command:
- "mysqld"
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
healthcheck:
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ]
interval: 5s
timeout: 3s
retries: 10
start_period: 10s

redis:
image: redis
ports:
- 6379:6379
healthcheck:
test: [ "CMD", "redis-cli", "ping" ]
interval: 5s
timeout: 3s
retries: 10
start_period: 5s

networks:
default:
driver: bridge
61 changes: 61 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

LOG_FILE=/home/ubuntu/deploy.log
DEPLOY_PATH=/home/ubuntu/app/
HEALTH_URL="http://localhost:8080/api/auth/health-check"

echo ">>> 배포 시작: $(date)" >> $LOG_FILE

cd $DEPLOY_PATH || exit

# .env 로부터 환경변수 불러오기
export $(grep -v '^#' ${DEPLOY_PATH}.env | xargs)

# Docker Hub 로그인
echo ">>> Docker Hub 로그인" >> $LOG_FILE
docker login -u ${DOCKER_HUB_USERNAME} -p ${DOCKER_HUB_ACCESS_TOKEN}
if [ $? -ne 0 ]; then
echo ">>> [ERROR] Docker Hub 로그인 실패" >> $LOG_FILE
exit 1
fi

# configs 디렉토리 생성 및 설정파일 복사
echo ">>> 설정 파일 복사" >> $LOG_FILE
mkdir -p ${DEPLOY_PATH}configs

cp ${DEPLOY_PATH}application-prod.yml ${DEPLOY_PATH}configs/
cp ${DEPLOY_PATH}application-oauth.yml ${DEPLOY_PATH}configs/
cp ${DEPLOY_PATH}application-common.yml ${DEPLOY_PATH}configs/

# 기존 컨테이너 종료 및 삭제
echo ">>> 기존 컨테이너 종료 및 제거" >> $LOG_FILE
docker-compose down

# 최신 이미지 Pull
echo ">>> Docker 이미지 pull" >> $LOG_FILE
docker-compose pull

# 컨테이너 실행
echo ">>> Docker Compose로 애플리케이션 실행" >> $LOG_FILE
docker-compose up -d

# Health check
echo ">>> 애플리케이션 Health Check 시작" >> $LOG_FILE

for i in {1..20}
do
STATUS=$(curl -s $HEALTH_URL)
if [[ "$STATUS" == *"health check OK"* || "$STATUS" == *"OK"* ]]; then
echo ">>> 서버가 정상적으로 기동되었습니다." >> $LOG_FILE
break
fi
echo ">>> 서버가 아직 기동되지 않았습니다. 재시도: $i" >> $LOG_FILE
sleep 5
done

if [[ ! "$STATUS" == *"health check OK"* && ! "$STATUS" == *"OK"* ]]; then
echo ">>> [ERROR] 서버가 정상적으로 기동되지 않았습니다." >> $LOG_FILE
exit 1
fi

echo ">>> 배포 완료: $(date)" >> $LOG_FILE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.genius.herewe.core.security.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -25,7 +26,7 @@ public class AuthController implements AuthApi {

@PostMapping("/auth")
public SingleResponse<AuthResponse> authorize(HttpServletResponse response,
@RequestBody AuthRequest authRequest) {
@RequestBody AuthRequest authRequest) {

User user = userFacade.findUser(authRequest.userId());

Expand All @@ -36,4 +37,9 @@ public SingleResponse<AuthResponse> authorize(HttpServletResponse response,
AuthResponse authResponse = userFacade.getAuthInfo(user.getId());
return new SingleResponse<>(HttpStatus.OK, authResponse);
}

@GetMapping("/auth/health-check")
public String healthCheck() {
return "health check OK";
}
}