Skip to content
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

[20기_임가현, 황서아] spring_vote 미션 제출합니다. #9

Open
wants to merge 102 commits into
base: onedwo-punch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
7117e0d
git init
limgahyun Nov 30, 2024
aefb499
Merge branch 'master' of github.com:Onedwo-Punch/spring-vote-20th
limgahyun Nov 30, 2024
295e6ea
feat: add dependencies
limgahyun Nov 30, 2024
37c683e
feat: common response & exception code
limgahyun Nov 30, 2024
a7ec842
feat: baseEntity
limgahyun Nov 30, 2024
ba6a96d
feat: users entity, repository
limgahyun Nov 30, 2024
34c5b5a
feat: swagger setting
limgahyun Nov 30, 2024
e19a701
feat: mysql db setting
limgahyun Nov 30, 2024
f54dff6
feat: file structure
limgahyun Nov 30, 2024
1001bb9
feat: deploy setting
limgahyun Dec 24, 2024
0f8a9e8
feat: git action branch setting
limgahyun Dec 24, 2024
08e71d9
feat: git action trigger 변경
limgahyun Dec 24, 2024
f3bbdcb
test: git action trigger 변경
limgahyun Dec 24, 2024
ed3f238
Create CICD.yml
limgahyun Dec 24, 2024
69a8b3e
test: git action trigger 변경
limgahyun Dec 24, 2024
b0be3fc
Update CICD.yml
limgahyun Dec 24, 2024
5b7334f
Merge branch 'master' into master
limgahyun Dec 24, 2024
d4eb2e0
Merge pull request #1 from limgahyun/master
limgahyun Dec 24, 2024
0e996c5
fix: server url 변경
limgahyun Dec 24, 2024
6db114e
Merge pull request #2 from limgahyun/master
limgahyun Dec 24, 2024
cb64c0b
Merge pull request #3 from Onedwo-Punch/dev
limgahyun Dec 24, 2024
563ce26
feat: login 구현
seoahS01 Dec 26, 2024
22d45e2
Merge pull request #4 from seoahS01/master
seoahS01 Dec 26, 2024
54699e9
Revert "Feat: 회원가입, 로그인 구현"
limgahyun Dec 26, 2024
03868ae
Merge pull request #5 from Onedwo-Punch/revert-4-master
limgahyun Dec 26, 2024
2ba2279
feat: auth 관련 파일 생성
seoahS01 Dec 26, 2024
fa93694
feat: leader voting 관련 entity, repository
limgahyun Dec 29, 2024
32559c1
feat: leader vote create service
limgahyun Dec 29, 2024
d670aec
feat: leader vote by user 조회 기능
limgahyun Dec 29, 2024
fcf76ab
feat: leader vote update service
limgahyun Dec 29, 2024
e07b1f3
refactor: findUserById service
limgahyun Dec 29, 2024
6f17311
feat: get leader vote result service
limgahyun Dec 29, 2024
43bf47c
feat: get leader candidates service
limgahyun Dec 29, 2024
ce0bb28
feat: 파트별 투표 결과 조회
limgahyun Dec 29, 2024
b66f455
fix: remove unused import
limgahyun Dec 29, 2024
55840a9
feat: 득표결과 내림차순 정렬
limgahyun Dec 29, 2024
a69112f
feat: team votin 관련 entity 생성
limgahyun Dec 29, 2024
6de9a29
feat: get team candidates service
limgahyun Dec 29, 2024
d5184d4
feat: create team vote service
limgahyun Dec 29, 2024
7e1c9a0
feat: get team vote by user service
limgahyun Dec 29, 2024
0d54290
feat: update team vote service
limgahyun Dec 29, 2024
b353c9f
feat: get team vote result service
limgahyun Dec 29, 2024
5462b63
chore: remove unused import
limgahyun Dec 29, 2024
9433316
feat: 로그인 기능 보안 및 회원가입 기능 추가
seoahS01 Dec 30, 2024
ccefdb0
feat: user 관련 코드 추가
seoahS01 Dec 30, 2024
cab62f3
update: auth와 user 사이 오류 수정
seoahS01 Dec 30, 2024
4382ab0
update: user엔티티명 users로 수정
seoahS01 Dec 30, 2024
23fc331
Merge branch 'dev' into master
seoahS01 Dec 30, 2024
00f3401
fix: column name 수정
limgahyun Dec 30, 2024
f5751d6
Merge pull request #6 from limgahyun/master
limgahyun Dec 31, 2024
a83650f
Merge remote-tracking branch 'upstream/dev'
seoahS01 Dec 31, 2024
d16c06c
update: 코멘트 반영
seoahS01 Jan 1, 2025
2b7aea1
update: port 번호 8080으로 정정
seoahS01 Jan 1, 2025
c6f4b10
update: Team enum 값으로 수정 및 CommonResponse로 변환
seoahS01 Jan 1, 2025
cd41849
Merge pull request #8 from seoahS01/master
seoahS01 Jan 1, 2025
6ae98ff
refactor: findUserById refactoring
limgahyun Jan 1, 2025
60b7000
fix: swagger, cors setting
limgahyun Jan 1, 2025
ad512e6
fix: change cors setting for test
limgahyun Jan 1, 2025
ae4e5ae
feat: security role 접근 제한
limgahyun Jan 1, 2025
427c625
fix: security 접근 제한 수정
limgahyun Jan 1, 2025
2f6936c
Merge pull request #9 from limgahyun/master
limgahyun Jan 1, 2025
cccda80
Merge pull request #10 from Onedwo-Punch/dev
limgahyun Jan 1, 2025
fea17f3
update: 회원가입 수정 중
seoahS01 Jan 1, 2025
416a7f8
update: 회원가입 성공
seoahS01 Jan 1, 2025
383296e
feat: jwt secret key 환경변수 설정
limgahyun Jan 1, 2025
094c780
fix: swagger 접근 허용
limgahyun Jan 1, 2025
08b8339
Merge branch 'dev' into master
seoahS01 Jan 1, 2025
da9da90
Merge pull request #12 from seoahS01/master
seoahS01 Jan 1, 2025
3fc0312
update: 로그인 성공
seoahS01 Jan 1, 2025
28080ad
update: 회원 정보 조회 완료
seoahS01 Jan 1, 2025
e12a4d7
Merge remote-tracking branch 'upstream/dev'
seoahS01 Jan 1, 2025
9234073
update: 주석 추가
seoahS01 Jan 1, 2025
bb89b63
Merge pull request #15 from seoahS01/master
limgahyun Jan 1, 2025
4b16e84
Merge pull request #11 from Onedwo-Punch/dev
limgahyun Jan 1, 2025
c1e5601
hotfix: api access permitAll
limgahyun Jan 3, 2025
951c2b6
fix: 로그인 response 수정
Jan 3, 2025
47e36b7
Merge branch 'dev' of https://github.com/Onedwo-Punch/spring-vote-20th
limgahyun Jan 3, 2025
d3b001f
fix: team enum type 적용
limgahyun Jan 3, 2025
3f98291
fix: enum type 적용
limgahyun Jan 3, 2025
71563b3
fix: team enum type 적용
limgahyun Jan 3, 2025
7ef1db9
fix: 회원가입 response 수정
Jan 3, 2025
49a3fe6
fix: 로그인 response 수정
Jan 3, 2025
10c56e9
fix: 투표 여부 잘못 반환
Jan 3, 2025
fdf6f7e
fix: 투표 여부 진짜 반영
Jan 3, 2025
110019d
Merge pull request #17 from seoahS01/master
limgahyun Jan 3, 2025
cddb4c5
merge
limgahyun Jan 3, 2025
b0b36ee
fix: enableJpaAuditing
limgahyun Jan 3, 2025
7b539a1
Merge pull request #18 from limgahyun/master
seoahS01 Jan 3, 2025
2a1ba71
fix: 로그인 시 아이디/비밀번호 잘못 입력 시 에러 반환
Jan 3, 2025
16736e7
Merge pull request #19 from seoahS01/master
limgahyun Jan 3, 2025
b7bbc2c
feat: signInResponseDto userId 추가
limgahyun Jan 3, 2025
13cba30
Merge pull request #20 from limgahyun/master
limgahyun Jan 3, 2025
e43e6e9
fix: 인가 문제 해결
Jan 3, 2025
43be13c
Merge pull request #21 from seoahS01/master
limgahyun Jan 3, 2025
e49eda4
docs: deploy readme
limgahyun Jan 3, 2025
7e39327
Merge pull request #22 from limgahyun/master
seoahS01 Jan 4, 2025
ada2c7b
Update CICD.yml
limgahyun Jan 4, 2025
be01f7a
Update CICD.yml
limgahyun Jan 5, 2025
9b86649
Merge pull request #16 from Onedwo-Punch/dev
limgahyun Jan 5, 2025
1165a94
Update README.md
seoahS01 Jan 5, 2025
e8dfe7b
Merge pull request #23 from seoahS01/master
limgahyun Jan 5, 2025
9f34356
Merge pull request #24 from Onedwo-Punch/dev
limgahyun Jan 5, 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
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/gradlew text eol=lf
*.bat text eol=crlf
*.jar binary
121 changes: 121 additions & 0 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# github repository actions 페이지에 나타날 이름
name: CI/CD using github actions & docker

# event trigger
# main브랜치에 pull request가 발생 되었을 때 실행
on:
pull_request:
branches: [ "master" ]
push:
branches: [ "master" ]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v3

- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'

- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-

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

- name: Build Project with Gradle
run: |
echo ${{ secrets.APPLICATION_SECRET }} | base64 --decode > ./src/main/resources/application-secret.yml
./gradlew bootJar

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build docker image
run: docker build --platform linux/amd64 -t ${{ secrets.DOCKER_USERNAME }}/vote .

- name: Publish image to docker hub
run: docker push ${{ secrets.DOCKER_USERNAME }}/vote:latest

deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Set Target IP
run: |
STATUS=$(curl -o /dev/null -w "%{http_code}" "${{ secrets.SERVER_IP }}/env")
echo $STATUS
if [ $STATUS = 200 ]; then
CURRENT_UPSTREAM=$(curl -s "${{ secrets.SERVER_IP }}/env")
else
CURRENT_UPSTREAM=green
fi
echo CURRENT_UPSTREAM=$CURRENT_UPSTREAM >> $GITHUB_ENV
if [ $CURRENT_UPSTREAM = blue ]; then
echo "CURRENT_PORT=8080" >> $GITHUB_ENV
echo "STOPPED_PORT=8081" >> $GITHUB_ENV
echo "TARGET_UPSTREAM=green" >> $GITHUB_ENV
else
echo "CURRENT_PORT=8081" >> $GITHUB_ENV
echo "STOPPED_PORT=8080" >> $GITHUB_ENV
echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV
fi

- name: Docker Compose
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.SERVER_IP }}
key: ${{ secrets.EC2_SSH_KEY }}
script_stop: true
script: |
export JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }}
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/vote:latest
sudo docker-compose -f docker-compose-${{env.TARGET_UPSTREAM}}.yml up -d

- name: Check deploy server URL
uses: jtalk/url-health-check-action@v3
with:
url: http://${{ secrets.SERVER_IP }}:${{ env.STOPPED_PORT }}/env
max-attempts: 5
retry-delay: 10s

- name: Change nginx upstream
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.SERVER_IP }}
key: ${{ secrets.EC2_SSH_KEY }}
script_stop: true
script: |
sudo docker exec -i nginxserver bash -c 'echo "set \$service_url ${{env.TARGET_UPSTREAM}};" > etc/nginx/conf.d/service-env.inc && nginx -s reload'

- name: Stop current server
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.SERVER_IP }}
key: ${{ secrets.EC2_SSH_KEY }}
script_stop: true
script: |
sudo docker stop ${{ env.CURRENT_UPSTREAM }}
sudo docker rm ${{ env.CURRENT_UPSTREAM }}

42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### 환경변수 ###
.env

application-secret.yml
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM openjdk:17-jdk-slim

ARG JAR_FILE=/build/libs/*.jar
ARG PROFILES
ARG ENV

COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Dspring.profiles.active=${PROFILES}", "-Dserver.env=${ENV}", "-jar", "app.jar"]
181 changes: 180 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,180 @@
# spring_vote_20th
# spring_vote_20th

## ERD
전체 ERD

<img width="747" alt="스크린샷 2025-01-05 오후 2 29 35" src="https://github.com/user-attachments/assets/4ee1f4e3-8a45-42b9-ad0a-5ca8425de985" />

- 유저, 파트장 투표, 팀 투표를 분리시켜 구성하였다.

### User
사용자에 관한 테이블

<img width="344" alt="스크린샷 2025-01-05 오후 2 57 58" src="https://github.com/user-attachments/assets/6b699059-c602-4d4c-bf5e-169549a3e821" />

- 사용자는 name과 password를 통해 로그인을 할 수 있다.

### Leader Vote
파트장 투표에 관한 테이블

<img width="800" alt="스크린샷 2025-01-05 오후 4 07 38" src="https://github.com/user-attachments/assets/48147c43-fd44-4a0f-87c7-32dbf82b2571" />

- Leader Candidate에는 후보자의 이름과 파트 그리고 후보자 id가 있다.
- Leader Vote는 투표한 사용자의 id와 사용자가 투표한 후보자 id 그리고 투표 고유 id가 있다.
- 사용자 id와 후보자 id는 외래키로 받아왔다.

### Team Vote
팀 투표에 관한 테이블

<img width="594" alt="스크린샷 2025-01-05 오후 4 12 14" src="https://github.com/user-attachments/assets/084b613c-c84b-467a-a327-b37d86c28b6a" />

- Team Candidate는 팀명과 팀 고유 id로 구성되어 있다.
- Team Vote는 투표한 사용자 id, 사용자가 투표한 팀 id 그리고 투표 고유 id가 있다.
- 사용자 id와 사용자 id는 외래키로 받아왔다.

## 배포 - blue green 무중단 배포
### blue green 배포란?
https://github.com/user-attachments/assets/d603aaa1-c8fc-4ef2-829a-629b2a35b09e

```yaml
spring:
profiles:
active: local
group:
local: local, common, secret
blue: blue, common, secret
green: green, common, secret

---

spring:
config:
activate:
on-profile: blue

server:
port: 8080
serverAddress: 13.124.171.94

serverName: blue_server

---

spring:
config:
activate:
on-profile: common
import: optional:file:.env[.properties]
```
application.yml


### nginx load balancing
- blue server - 8080 port
- green server - 8081 port

http://52.79.122.106 → nginx로 8080 or 8081 port 중 실행 중인 port로 연결


### github action
```yaml


# event trigger
# main브랜치에 pull request가 발생 되었을 때 실행
on:
pull_request:
branches: [ "master" ]
push:
branches: [ "master" ]

jobs:
build:
steps:
- name: Checkout source

- name: Setup Java

- name: Gradle Caching

- name: Grant execute permission for gradlew

- name: Build Project with Gradle

- name: Login to DockerHub
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build docker image
run: docker build --platform linux/amd64 -t ${{ secrets.DOCKER_USERNAME }}/vote .

- name: Publish image to docker hub
run: docker push ${{ secrets.DOCKER_USERNAME }}/vote:latest

deploy:
steps:
- name: Set Target IP
run: |
STATUS=$(curl -o /dev/null -w "%{http_code}" "${{ secrets.SERVER_IP }}/env")
echo $STATUS
if [ $STATUS = 200 ]; then
CURRENT_UPSTREAM=$(curl -s "${{ secrets.SERVER_IP }}/env")
else
CURRENT_UPSTREAM=green
fi
echo CURRENT_UPSTREAM=$CURRENT_UPSTREAM >> $GITHUB_ENV
if [ $CURRENT_UPSTREAM = blue ]; then
echo "CURRENT_PORT=8080" >> $GITHUB_ENV
echo "STOPPED_PORT=8081" >> $GITHUB_ENV
echo "TARGET_UPSTREAM=green" >> $GITHUB_ENV
else
echo "CURRENT_PORT=8081" >> $GITHUB_ENV
echo "STOPPED_PORT=8080" >> $GITHUB_ENV
echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV
fi

- name: Docker Compose
with:
script: |
export JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }}
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/vote:latest
sudo docker-compose -f docker-compose-${{env.TARGET_UPSTREAM}}.yml up -d

- name: Check deploy server URL
with:
url: http://${{ secrets.SERVER_IP }}:${{ env.STOPPED_PORT }}/env

- name: Change nginx upstream
with:
script: |
sudo docker exec -i nginxserver bash -c 'echo "set \$service_url ${{env.TARGET_UPSTREAM}};" > etc/nginx/conf.d/service-env.inc && nginx -s reload'

- name: Stop current server
with:
script: |
sudo docker stop ${{ env.CURRENT_UPSTREAM }}
sudo docker rm ${{ env.CURRENT_UPSTREAM }}

```

### trouble shooting
**JWT_SECRET_KEY를 읽지 못해서 발생하는 빌드 에러**

1. git secrets에 JWT_SECRET_KEY 변수를 추가 → docker-compose 파일을 실행할 때 넣어주려고 했으나 인식하지 못하는 문제가 발생함.
2. CICD.yml 파일 내에서 $GITHUB_ENV에 JWT_SECRET_KEY값을 저장 → 키가 외부에 노출되면 안되기 때문에 보안 문제 발생
3. docker-compse.yml 파일에서 직접 추가 → 성공
```yaml
environment:
- JWT_SECRET_KEY=
```


**보안 규칙**
1. 8080 port, 8081 port를 번갈아가며 사용하기 때문에 두 port에 대한 inbound 규칙 추가

**비밀번호를 인식하지 못하는 문제**
1. 로그인 기능을 먼저 구현한 뒤, 테이블에 직접 데이터를 넣고 로그인 테스트를 해보았으나 비밀번호를 인식하지 못하여 실패
2. 비밀번호가 암호화되어 입력되어야 하는데 직접 데이터를 넣었기 때문에 암호화되지 않은 채로 들어가 문제가 생겼음을 인식
3. 회원가입 구현 후에 다시 로그인을 시도했으나 여전히 비밀번호를 인식하지 못함
4. import문을 잘못 가지고 왔다는 것을 깨달음... → 해결..^_^
Loading