Skip to content

Commit

Permalink
Merge pull request #121 from kakao-tech-campus-2nd-step3/weekly/11
Browse files Browse the repository at this point in the history
11주차 종합 PR
  • Loading branch information
Thornappl2 authored Nov 15, 2024
2 parents 66cf959 + 48fff7b commit 866a1f2
Show file tree
Hide file tree
Showing 125 changed files with 5,602 additions and 960 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Build and Deploy to EC2

# 워크플로우가 언제 실행될 것인지 조건 명시
on:
push: # 모든 브랜치 대상

# AWS 관련 값 변수로 설정
env:
AWS_REGION: ap-northeast-2
AWS_S3_BUCKET: devcard-deploy-bucket
AWS_CODE_DEPLOY_APPLICATION: Devcard-Application-CD
AWS_CODE_DEPLOY_GROUP: Devcard-Deployment-Group

jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4

# JDK 21 설치
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

# 환경 변수를 사용하여 application-secret.properties 파일 생성
- name: make application-secret.properties
run: |
mkdir -p ./src/main/resources
cd ./src/main/resources
touch ./application-secret.properties
echo "kakao.javascript.key=${{ secrets.KAKAO_JAVASCRIPT_KEY }}" >> ./application-secret.properties
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> ./application-secret.properties
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> ./application-secret.properties
echo "GH_CLIENT_ID=${{ secrets.GH_CLIENT_ID }}" >> ./application-secret.properties
echo "GH_CLIENT_SECRET=${{ secrets.GH_CLIENT_SECRET }}" >> ./application-secret.properties
echo "GH_REDIRECT_URI=${{ secrets.GH_REDIRECT_URI }}" >> ./application-secret.properties
# 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash

- name: Build and Test
env:
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: ./gradlew build test --info

# 빌드 파일을 zip 형식으로 압축
- name: Make zip file
run: zip -r ./$GITHUB_SHA.zip .
shell: bash

# AWS 권한
- 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 }}

# S3 버킷에 빌드파일(zip 파일)을 업로드
- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$AWS_S3_BUCKET/$GITHUB_SHA.zip

# EC2 인스턴스에 S3에 저장되어 있던 zip 파일을 받아와 배포 시작
- name: EC2에 배포
run: aws deploy create-deployment --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} --s3-location bucket=$AWS_S3_BUCKET,key=$GITHUB_SHA.zip,bundleType=zip
17 changes: 17 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 0.0
os: linux

files:
- source: /
destination: /home/ubuntu/app
overwrite: yes

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

hooks:
ApplicationStart:
- location: scripts/deploy.sh
timeout: 60
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation 'com.google.zxing:javase:3.5.1'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'mysql:mysql-connector-java:8.0.33'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
29 changes: 29 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# 빌드 파일 탐색 및 이름 설정
BUILD_JAR=$(ls /home/ubuntu/app/build/libs/*-SNAPSHOT.jar)
JAR_NAME=$(basename $BUILD_JAR)
echo ">>> build 파일명: $JAR_NAME" >> /home/ubuntu/deploy.log

# 빌드 파일 복사
echo ">>> build 파일 복사" >> /home/ubuntu/deploy.log
DEPLOY_PATH=/home/ubuntu/app/
cp $BUILD_JAR $DEPLOY_PATH

# 현재 실행 중인 애플리케이션 종료
echo ">>> 현재 실행중인 애플리케이션 pid 확인 후 일괄 종료" >> /home/ubuntu/deploy.log
CURRENT_PID=$(pgrep -f $JAR_NAME)

if [ -z "$CURRENT_PID" ]; then
echo ">>> 현재 실행 중인 애플리케이션이 없습니다." >> /home/ubuntu/deploy.log
else
echo ">>> 실행 중인 애플리케이션 종료: $CURRENT_PID" >> /home/ubuntu/deploy.log
kill -15 $CURRENT_PID
sleep 5
fi

# 애플리케이션 배포
DEPLOY_JAR=$DEPLOY_PATH$JAR_NAME
echo ">>> DEPLOY_JAR 배포" >> /home/ubuntu/deploy.log
echo ">>> $DEPLOY_JAR을 실행합니다" >> /home/ubuntu/deploy.log
nohup java -jar $DEPLOY_JAR >> /home/ubuntu/deploy.log 2>&1 &
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
"/js/**",
"/images/**",
"/h2-console/**",
"/walletList"
"/walletList",
"/qrcodes/**",
"/notice",
"/notice/**",
"/qna",
"/qna/**",
"/shared/**"
).permitAll();
// 그 외의 모든 요청은 인증 필요
auth.anyRequest().authenticated();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.devcard.devcard.auth.controller.page;

import com.devcard.devcard.auth.entity.Member;
import com.devcard.devcard.auth.model.OauthMemberDetails;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MypageController {

@GetMapping("/mypage")
public String mypage(@AuthenticationPrincipal OauthMemberDetails oauthMemberDetails, Model model) {
if (oauthMemberDetails != null) {
Member member = oauthMemberDetails.getMember();
model.addAttribute("member", member);
}
return "mypage";
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.devcard.devcard.auth.controller;
package com.devcard.devcard.auth.controller.rest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller("gitHubLoginController")
public class LoginController {

@GetMapping("login")
@GetMapping("/login")
public String login() {
return "login";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.devcard.devcard.auth.controller;
package com.devcard.devcard.auth.controller.rest;

import com.devcard.devcard.auth.entity.Member;
import com.devcard.devcard.auth.model.OauthMemberDetails;
import com.devcard.devcard.auth.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.security.core.Authentication;

import java.util.HashMap;
import java.util.Map;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/devcard/devcard/auth/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public class Member {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String githubId;

@Column(name = "email")
private String email;

private String profileImg;
private String username;
private String nickname;
Expand All @@ -30,8 +33,8 @@ public class Member {
@CreationTimestamp
private Timestamp createDate;

@OneToOne(mappedBy = "member", cascade = CascadeType.ALL)
private Card card;
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private List<Card> cards = new ArrayList<>();

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private List<Group> groups = new ArrayList<>();
Expand All @@ -50,7 +53,7 @@ public Member(String githubId, String email, String profileImg, String username,
}

public void updateFromAttributes(Map<String, Object> attributes) {
this.email = (String) attributes.get("email");
this.email = email;
this.profileImg = (String) attributes.get("avatar_url");
this.username = (String) attributes.get("name");
this.nickname = (String) attributes.get("login");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public OauthMemberDetails(Member member, Map<String, Object> attributes){

@Override
public <A> A getAttribute(String name) {
return OAuth2User.super.getAttribute(name);
return (A) attributes.get(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.devcard.devcard.auth.repository;

import com.devcard.devcard.auth.entity.Member;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<Member, Long> {

Member findByGithubId(String githubId);

List<Member> findByIdIn(List<Long> ids);
}
Loading

0 comments on commit 866a1f2

Please sign in to comment.