diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 00000000..b07092cb --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,90 @@ +name: CD Backend + +on: + push: + branches: [ "main", "dev" ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + # 자바 버전 설정 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # yml 파일 생성 + - name: Generate application-core.yml + run: | + mkdir -p ./core/src/main/resources + echo "${{ secrets.APPLICATION_CORE }}" | base64 -d > ./core/src/main/resources/application-core.yml + + # firebase json 파일 생성 + - name : Generate firebase_account.json + run: | + mkdir -p ./core/src/main/resources/firebase + echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/main/resources/firebase/firebase_account.json + mkdir -p ./core/src/test/resources/firebase + echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/test/resources/firebase/firebase_account.json + + # gradle 권한 부여 + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + shell: bash + + # 빌드 시 캐시 적용 + - 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: Build with Gradle + run: ./gradlew build -x test + + + # 도커 허브 로그인 + - name: Docker Hub Login + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + + # 도커 이미지 빌드 및 푸시 + - name: docker image build and push + run: | + docker build -f Dockerfile -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} . + docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} + + # 서버 백그라운드 실행 + - name: pull image and run container + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + password: ${{ secrets.EC2_PASSWORD }} + port: ${{ secrets.EC2_PORT }} + script: | + docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} + docker stop ${{ secrets.DOCKERHUB_APP_NAME }} + docker rm ${{ secrets.DOCKERHUB_APP_NAME }} + docker run -d \ + -p 8080:8080 \ + -e TZ=Asia/Seoul \ + --name ${{ secrets.DOCKERHUB_APP_NAME }} \ + ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_APP_NAME }} + + docker system prune -f \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e697a837..60c76853 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,10 +1,8 @@ name: CI Backend on: - push: - branches: [ "main", "dev" ] pull_request: - branches: [ "main", "dev" ] + branches: ["main", "dev"] permissions: contents: read @@ -28,6 +26,19 @@ jobs: java-version: '17' distribution: 'temurin' + # firebase json 파일 생성 + - name : Generate firebase_account.json + run: | + mkdir -p ./core/src/main/resources/firebase + echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/main/resources/firebase/firebase_account.json + mkdir -p ./core/src/test/resources/firebase + echo "${{ secrets.FIREBASE_ACCOUNT }}" | base64 -d > ./core/src/test/resources/firebase/firebase_account.json + + # gradle 권한 부여 + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + shell: bash + # 빌드 시 캐시 적용 - name: Gradle Caching uses: actions/cache@v3 @@ -39,11 +50,6 @@ jobs: restore-keys: | ${{ runner.os }}-gradle- - # gradle 권한 부여 - - name: Grant execute permission for gradlew - run: chmod +x ./gradlew - shell: bash - # 빌드 - name: Build with Gradle run: ./gradlew build @@ -60,4 +66,4 @@ jobs: uses: mikepenz/action-junit-report@v3 if: always() with: - report_paths: '**/build/test-results/test/TEST-*.html' \ No newline at end of file + report_paths: '**/build/test-results/test/TEST-*.html' diff --git a/.gitignore b/.gitignore index 9ccb22f3..228560fc 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,10 @@ out/ ### VS Code ### .vscode/ .DS_Store -core/src/main/resources/application.yml +core/src/main/resources/application-core-dev.yml +core/src/main/resources/application-core-prod.yml application-core.yml /api/src/main/resources/application.yml /core/src/main/generated/ +/core/src/main/resources/firebase/firebase_account.json +/core/src/test/resources/firebase/firebase_account.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..0e0ec91f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM openjdk:17-jdk +ARG JAR_FILE=api/build/libs/*.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java", "-jar", "/app.jar"] \ No newline at end of file diff --git a/api/src/main/java/dev/handsup/HandsUpApplication.java b/api/src/main/java/dev/handsup/HandsUpApplication.java index 9f667746..5d0098bd 100644 --- a/api/src/main/java/dev/handsup/HandsUpApplication.java +++ b/api/src/main/java/dev/handsup/HandsUpApplication.java @@ -12,11 +12,6 @@ @SpringBootApplication public class HandsUpApplication { - @PostConstruct - public void started() { - TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul")); - } - public static void main(String[] args) { // 타 모듈 yml 가져오기 System.setProperty("spring.config.name", "application, application-core"); diff --git a/core/src/test/resources/application.yml b/core/src/test/resources/application.yml index 89c2c9be..060e11e0 100644 --- a/core/src/test/resources/application.yml +++ b/core/src/test/resources/application.yml @@ -26,4 +26,4 @@ cloud: auto: false fcm: - certification: firebase/firebase_service_account.json + certification: firebase/firebase_account.json