diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6b57da4a..e06a0b95 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,39 +1,71 @@ +# GitHub Actions 워크플로우 이름 정의 name: Deploy To EC2 +# 워크플로우 트리거 설정 +# develop 브랜치에 push가 발생할 때만 실행 on: push: branches: - develop jobs: + # 배포 작업 정의 deploy: - runs-on: ubuntu-latest + runs-on: ubuntu-latest # 실행 환경 지정 + + # GitHub Actions에서 사용할 환경 변수 설정 + # GitHub Secrets에서 값을 가져옴 + env: + NAVER_CLIENT_ID: ${{ secrets.NAVER_CLIENT_ID }} + NAVER_REDIRECT_URI: ${{ secrets.NAVER_REDIRECT_URI }} + NAVER_CLIENT_SECRET: ${{ secrets.NAVER_CLIENT_SECRET }} + JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }} + OPENWEATHER_API_KEY: ${{ secrets.OPENWEATHER_API_KEY }} + steps: + # 레포지토리 코드를 체크아웃 + - uses: actions/checkout@v3 + + # JDK 17 설정 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # gradlew 파일에 실행 권한 부여 + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + # Gradle을 사용하여 프로젝트 빌드 + - name: Build with Gradle + run: ./gradlew clean build + + # 빌드된 JAR 파일을 EC2 서버로 전송 + - name: Copy jar file to EC2 + uses: appleboy/scp-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_PRIVATE_KEY }} + source: "build/libs/*SNAPSHOT.jar" # 전송할 JAR 파일 경로 + target: "/home/ubuntu/WEB1_2_PitchingMate_BE" # EC2의 대상 경로 + strip_components: 2 # source 경로에서 상위 2개 디렉토리 제거 + + # EC2 서버에 SSH로 접속하여 배포 스크립트 실행 - name: SSH로 EC2에 접속하기 uses: appleboy/ssh-action@v1.2.0 env: APPLICATION_PROPERTIES: ${{ secrets.APPLICATION_PROPERTIES }} - NAVER_CLIENT_ID: ${{ secrets.NAVER_CLIENT_ID }} - NAVER_REDIRECT_URI: ${{ secrets.NAVER_REDIRECT_URI }} - NAVER_CLIENT_SECRET: ${{ secrets.NAVER_CLIENT_SECRET }} - JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }} - OPENWEATHER_API_KEY: ${{ secrets.OPENWEATHER_API_KEY }} with: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_PRIVATE_KEY }} - envs: | - APPLICATION_PROPERTIES - NAVER_CLIENT_ID - NAVER_REDIRECT_URI - NAVER_CLIENT_SECRET - JWT_SECRET_KEY - OPENWEATHER_API_KEY - script_stop: true - script: | - cd /home/ubuntu/WEB1_2_PitchingMate_BE - git pull origin develop - echo "$APPLICATION_PROPERTIES" > src/main/resources/application-dev.yml - ./gradlew clean build - sudo fuser -k -n tcp 8080 || true - nohup java -jar build/libs/*SNAPSHOT.jar > ./output.log 2>&1 & \ No newline at end of file + envs: APPLICATION_PROPERTIES # EC2에 전달할 환경 변수 + script_stop: true # 스크립트 실행 중 오류 발생시 중단 + script: | # EC2에서 실행할 명령어들 + cd /home/ubuntu/WEB1_2_PitchingMate_BE # 프로젝트 디렉토리로 이동 + git pull origin develop # 최신 코드 가져오기 + echo "$APPLICATION_PROPERTIES" > src/main/resources/application-dev.yml # 설정 파일 생성 + sudo fuser -k -n tcp 8080 || true # 8080 포트 사용중인 프로세스 종료 + nohup java -jar *SNAPSHOT.jar > ./output.log 2>&1 & # 새로운 버전 애플리케이션 실행 \ No newline at end of file diff --git a/src/main/resources/application-common.yml b/src/main/resources/application-common.yml index 4e204977..cdbdfc04 100644 --- a/src/main/resources/application-common.yml +++ b/src/main/resources/application-common.yml @@ -22,15 +22,13 @@ springdoc: default-consumes-media-type: application/json default-produces-media-type: application/json -logging: - level: - root: INFO - com.example.mate: DEBUG + oauth: - naver_client_id: ${NAVER_CLIENT_ID} - naver_redirect_uri: ${NAVER_REDIRECT_URI} - naver_client_secret: ${NAVER_CLIENT_SECRET} + naver: + client-id: ${NAVER_CLIENT_ID} + redirect-uri: ${NAVER_REDIRECT_URI} + client-secret: ${NAVER_CLIENT_SECRET} jwt: secret_key: ${JWT_SECRET_KEY} diff --git a/src/test/java/com/example/mate/MateApplicationTests.java b/src/test/java/com/example/mate/MateApplicationTests.java index 5af75f44..545ece3c 100644 --- a/src/test/java/com/example/mate/MateApplicationTests.java +++ b/src/test/java/com/example/mate/MateApplicationTests.java @@ -5,7 +5,13 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.SpringBootTest; -@SpringBootTest +@SpringBootTest(properties = { + "jwt.secret_key=testsecretkey12345678901234567890", + "oauth.naver.client-id=test_client_id", + "oauth.naver.redirect-uri=http://localhost:8080/callback", + "oauth.naver.client-secret=test_client_secret", + "openweather.api.key=test_api_key" +}) @EnableConfigurationProperties(OAuthConfig.class) class MateApplicationTests { diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index b688048d..a9db6f16 100644 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -17,4 +17,4 @@ spring: h2: console: enabled: true - path: /h2-console \ No newline at end of file + path: /h2-console