Skip to content

[김태희] sprint8 #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ad3e7ce
docs: 불필요한 파일 삭제
WaiCat May 20, 2025
1a03ab0
docs: db 환경변수 설정
WaiCat May 20, 2025
16bedf9
fix: baseurl 오타 수정
WaiCat May 20, 2025
f12dc91
refactor: version변경 및 의존성 추가
WaiCat May 20, 2025
44a7eee
feat: 엔드포인트 설정 및 db파일 관련 설정
WaiCat May 20, 2025
c36a596
test: test yaml파일 생성
WaiCat May 20, 2025
1131f9b
feat: sql파일 추가
WaiCat May 20, 2025
1fc6237
feat: docker 파일
WaiCat May 20, 2025
0a1516f
test: test 오류 수정
WaiCat May 20, 2025
c0bf246
refactor: 이미지 최적화
WaiCat May 21, 2025
4d734ee
feat: S3 저장 로직 구현
WaiCat May 21, 2025
18e09d0
docs: readme codecov 추가
WaiCat May 21, 2025
61083f9
feat: codecov 의존성 추가 및 CI위한 yml 구현
WaiCat May 21, 2025
ba07554
feat: 각 환경변수 설정
WaiCat May 21, 2025
582292e
refactor: push 브런치명 변경
WaiCat May 21, 2025
20ddd8f
feat: cd yml 구현
WaiCat May 21, 2025
86785b1
test: cd TaskDefinition not found 를 위한 로그 추가
WaiCat May 21, 2025
9da7f76
refactor: amazon-ecs-deploy-task-definition@v2 사용
WaiCat May 21, 2025
f074f48
fix: runningCount -> status로 변경
WaiCat May 21, 2025
f49dffd
chore: codecov branch변경
WaiCat May 23, 2025
8a7d626
test: S3BinaryContentStorageTest 구현
WaiCat May 23, 2025
56c570c
fix: 태그 명 설정 코드 추가
WaiCat May 23, 2025
a8634b9
refactor: generatePresignedUrl, getS3Client protected로 변경
WaiCat May 23, 2025
7bc43da
refactor: desired-count 1개로 재설정
WaiCat May 23, 2025
8b83239
chore: prod환경으로 변경
WaiCat May 23, 2025
49e54af
refactor: server port 설정 변경
WaiCat May 23, 2025
08deed9
chore: echo "JVM_OPTS=$JVM_OPTS" 추가
WaiCat May 26, 2025
cdbb4d5
chore: 환경변수 재설정
WaiCat May 26, 2025
415cb73
chore: prod-yml 환경변수 설정
WaiCat May 26, 2025
3b19baf
chore: db설정 환경별로 분리
WaiCat May 26, 2025
272ba28
chore: 프론트 파일 변경
WaiCat May 26, 2025
3869b81
fix: 잘못된 error메세지 값설정 변경
WaiCat May 26, 2025
d25a7c7
fix: 채널 중복 선택시 ReadStatus 무한 생성으로 인한 오류 수정
WaiCat May 27, 2025
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
19 changes: 19 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# IDE & Git
.idea
*.iml
.git
.gitattributes
.github

# Gradle build cache
.gradle
build

# 환경 템플릿
.env.template

# 문서
*.md

# 로그
*.log
21 changes: 0 additions & 21 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

114 changes: 114 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
name: Build and Deploy to Public ECR and ECS
on:
push:
branches:
- part2-김태희-sprint8

env:
AWS_REGION: ${{ vars.AWS_REGION }}
ECR_REPOSITORY_URI: ${{ vars.ECR_REPOSITORY_URI }}
ECS_CLUSTER: ${{ vars.ECS_CLUSTER }}
ECS_SERVICE: ${{ vars.ECS_SERVICE }}
ECS_TASK_DEFINITION: ${{ vars.ECS_TASK_DEFINITION }}

jobs:
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
outputs:
image-tag: ${{ steps.set-image-tag.outputs.image-tag }}

steps:
- name: Checkout repository
uses: actions/checkout@v3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요런건 중요한건 아닙니다만 최신 버전을 쓰는게 좋아요. checkout@v4

좋은 linter 를 쓰면 사전에 업그레이드 하라고 조언해주는 경우도 있습니다.


- name: Set image tags
id: set-image-tag
run: |
IMAGE_TAG=$(date +'%Y%m%d%H%M%S')-${GITHUB_SHA::8}
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_ENV
echo "image-tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
Comment on lines +29 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요즘 github actions 로 ci/cd 하는 케이스가 많아져서, 요 부분은 기억하고 계시면 좋습니다. github actions 이 어떻게 변수를 저장하는가, 에 대해서요


- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR Public
run: |
aws ecr-public get-login-password --region us-east-1 \
| docker login --username AWS --password-stdin public.ecr.aws

- name: Build Docker image
run: |
docker build \
-t ${{ env.ECR_REPOSITORY_URI }}:latest \
-t ${{ env.ECR_REPOSITORY_URI }}:${{ env.IMAGE_TAG }} .

- name: Push Docker image
run: |
docker push ${{ env.ECR_REPOSITORY_URI }}:latest
docker push ${{ env.ECR_REPOSITORY_URI }}:${{ env.IMAGE_TAG }}

deploy-to-ecs:
name: Deploy to ECS
runs-on: ubuntu-latest
needs: build-and-push

env:
IMAGE_TAG: ${{ needs.build-and-push.outputs.image-tag }}

steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Download current ECS task definition
run: |
aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} --query taskDefinition > task-def.json

- name: Update task definition with new image
id: render-task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-def.json
container-name: discodeit-app
image: ${{ env.ECR_REPOSITORY_URI }}:${{ env.IMAGE_TAG }}

- name: Scale down ECS service to 0 (stop current tasks)
run: |
aws ecs update-service \
--cluster ${{ env.ECS_CLUSTER }} \
--service ${{ env.ECS_SERVICE }} \
--desired-count 0

- name: Deploy to Amazon ECS
uses: aws-actions/amazon-ecs-deploy-task-definition@v2
with:
task-definition: ${{ steps.render-task-def.outputs.task-definition }}
cluster: ${{ env.ECS_CLUSTER }}
service: ${{ env.ECS_SERVICE }}
wait-for-service-stability: true

- name: Scale up ECS service to 1 (start tasks)
run: |
aws ecs update-service \
--cluster ${{ env.ECS_CLUSTER }} \
--service ${{ env.ECS_SERVICE }} \
--desired-count 1
Comment on lines +83 to +103
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 세팅에서는 기존 서비스(서버)가 0개로 줄고 새 서비스(서버)가 1개로 태어나는 사이동안 서버가 꺼지는, 즉 무중단 배포에 실패하는 순간이 발생할 것으로 보입니다. ECS 를 활용한 무중단 배포는 한번 따로 공부를 해두시길 추천드립니다


- name: Verify deployment
run: |
echo "Verifying deployment..."
SERVICE_STATUS=$(aws ecs describe-services --cluster ${{ env.ECS_CLUSTER }} --services ${{ env.ECS_SERVICE }} --query "services[0].status" --output text)
if [ "$SERVICE_STATUS" = "ACTIVE" ]; then
echo "Deployment successful! Service is running."
else
echo "Deployment may have issues. Running count: $SERVICE_STATUS"
exit 1
fi
36 changes: 36 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI Test

on:
push:
branches:
- part2-김태희-sprint8

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3


- name: Set up JDK 17
uses: actions/setup-java@v3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요것도 아마 v4 가 있을겁니다

with:
java-version: '17'
distribution: 'temurin'

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

- name: Build and test with Gradle
run: ./gradlew test

- name: Run tests with coverage
run: ./gradlew test jacocoTestReport
Comment on lines +27 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 불필요하게 test task 를 두 번 돌리게 됩니다. 아마 캐시가 묻어있어서 시간은 거의 들지 않겠습니다만

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그리고 build.gradle 에 finalizedBy jacocoTestReport 가 있어서 그냥 ./gradlew test 만 있어도 될 것 같습니다


- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM amazoncorretto:17 as builder
WORKDIR /app

# Gradle 관련 파일 복사 (Wrapper 포함)
COPY gradlew .
COPY gradle gradle
COPY build.gradle .
COPY settings.gradle .

# Gradle 의존성 사전 다운로드 (캐시 활용)
RUN chmod +x gradlew && ./gradlew dependencies

# 애플리케이션 소스 코드 복사 및 빌드 (테스트 생략)
COPY src src
RUN ./gradlew build -x test

FROM amazoncorretto:17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test 를 돌리는 github action 에선 jdk 17 temurin 을 사용했는데 여기선 amazon corretto 를 사용하고 있네요. 99.999% 확률로 아무 문제 없겠지만 굳이 서로 다른 jdk 를 쓸 필요는 없을 듯 합니다.


WORKDIR /app

# 환경 변수 설정 (기본값 포함)
ENV PROJECT_NAME=discodeit
ENV PROJECT_VERSION=1.2-M8
ENV JVM_OPTS=""
ENV SERVER_PORT=80

# 임시 파일 저장소 볼륨 선언 (Spring Boot에서 종종 필요)
VOLUME /tmp

# 빌드 결과물 복사
COPY --from=builder /app/build/libs/${PROJECT_NAME}-${PROJECT_VERSION}.jar app.jar

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# 80 포트 노출
EXPOSE 80

# 실행 명령 설정 (환경 변수 활용)
ENTRYPOINT ["/entrypoint.sh"]
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# 2-sprint-mission
2기 스프린트 미션 제출 리포지토리입니다.

[![codecov](https://codecov.io/gh/WaiCat/2-sprint-mission/branch/part2-김태희-sprint8/graph/badge.svg)](https://codecov.io/gh/WaiCat/2-sprint-mission)

2기 스프린트 미션 제출 리포지토리입니다.
26 changes: 25 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.4.3'
id 'io.spring.dependency-management' version '1.1.7'
id 'jacoco'
}

group = 'com.sprint.mission'
version = '0.0.1-SNAPSHOT'
version = '1.2-M8'


java {
toolchain {
Expand All @@ -23,6 +25,10 @@ repositories {
mavenCentral()
}

jacoco {
toolVersion = "0.8.8" //추가: 버전 명시
}

dependencies {
// Spring Boot Core
implementation 'org.springframework.boot:spring-boot-starter-web'
Expand Down Expand Up @@ -59,6 +65,9 @@ dependencies {
// 비밀번호 암호화용
implementation 'org.mindrot:jbcrypt:0.4'


implementation 'software.amazon.awssdk:s3:2.31.7'

// 테스트 관련
testImplementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand All @@ -72,8 +81,23 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
finalizedBy jacocoTestReport
}

tasks.withType(JavaCompile) {
options.compilerArgs << "-parameters"
}

jacocoTestReport {
dependsOn test // tast 실행 전에 수행될 task
reports { // 어떤 파일을 생성할지/생성하지 않을지
xml.required = true
html.required = true
csv.required = false
// 기본 경로 $buildDir/reports/jacoco/test

// 리포트 타입마다 destination file 옵션으로 경로 변경 가능
// html.destination file("$buildDir/jacocoHtml")
// xml.destination file("$buildDir/jacoco.xml")
}
}
Binary file removed discodeit-files/binaryContent.dat
Binary file not shown.
Binary file removed discodeit-files/channels.dat
Binary file not shown.
Binary file removed discodeit-files/messages.dat
Binary file not shown.
Loading