diff --git a/.github/workflows/dev-build-and-deploy.yaml b/.github/workflows/dev-build-and-deploy.yaml new file mode 100644 index 00000000..e0e000af --- /dev/null +++ b/.github/workflows/dev-build-and-deploy.yaml @@ -0,0 +1,135 @@ +name: Build And Test + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build-and-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: "17" + distribution: "corretto" + + - name: Cache Gradle + 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 with Gradle + run: ./gradlew build -x test --stacktrace --parallel + + - name: Run tests + run: ./gradlew test + + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() + with: + files: '**/build/test-results/test/TEST-*.xml' + + - name: JUnit Report Action + uses: mikepenz/action-junit-report@v3 + if: always() + with: + report_paths: '**/build/test-results/test/TEST-*.xml' + + - name: Store test results + uses: actions/upload-artifact@v3 + if: always() + with: + name: test-results + path: '**/build/test-results/test/TEST-*.xml' + + - name: Test Report Summary + if: always() + run: | + echo '## Test Report Summary' >> $GITHUB_STEP_SUMMARY + echo '```' >> $GITHUB_STEP_SUMMARY + ./gradlew test --console=plain || true + echo '```' >> $GITHUB_STEP_SUMMARY + + deploy: + needs: build-and-test + if: github.ref == 'refs/heads/main' && github.event_name == 'push' + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/spot-server:dev-${{ github.sha }} + + - name: Deploy to Dev NCP Server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.DEV_NCP_SERVER_HOST }} + username: ${{ secrets.DEV_NCP_SERVER_USERNAME }} + password: ${{ secrets.DEV_NCP_SERVER_PASSWORD }} + port: ${{ secrets.DEV_NCP_SERVER_PORT }} + script: | + docker pull ${{ secrets.DOCKERHUB_USERNAME }}/spot-server:dev-${{ github.sha }} + docker stop spot-server-dev || true + docker rm spot-server-dev || true + docker run -d --name spot-server-dev \ + -p 8080:8080 \ + -e SPRING_DATASOURCE_URL=${{ secrets.DEV_DB_URL }} \ + -e SPRING_DATASOURCE_USERNAME=${{ secrets.DEV_DB_USERNAME }} \ + -e SPRING_DATASOURCE_PASSWORD=${{ secrets.DEV_DB_PASSWORD }} \ + -e TZ=Asia/Seoul \ + ${{ secrets.DOCKERHUB_USERNAME }}/spot-server:dev-${{ github.sha }} + docker system prune -af + + create-release: + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Bump version and push tag + id: tag_version + uses: mathieudutour/github-tag-action@v6.1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + + - name: Create a GitHub release + uses: ncipollo/release-action@v1 + with: + tag: ${{ steps.tag_version.outputs.new_tag }} + name: Release ${{ steps.tag_version.outputs.new_tag }} + body: ${{ steps.tag_version.outputs.changelog }} diff --git a/.github/workflows/dev-ci.yaml b/.github/workflows/dev-ci.yaml deleted file mode 100644 index 68b7ca8a..00000000 --- a/.github/workflows/dev-ci.yaml +++ /dev/null @@ -1,71 +0,0 @@ -name: Build And Test - -on: - push: - branches: - - dev - - main - pull_request: - branches: - - dev - - main - -jobs: - build-and-test: - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - java-version: "17" - distribution: "corretto" - - - name: Cache Gradle - 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 with Gradle - run: ./gradlew build -x test --stacktrace --parallel - - - name: Run tests - run: ./gradlew test - - - name: Publish Unit Test Results - uses: EnricoMi/publish-unit-test-result-action@v2 - if: always() - with: - files: '**/build/test-results/test/TEST-*.xml' - - - name: JUnit Report Action - uses: mikepenz/action-junit-report@v3 - if: always() - with: - report_paths: '**/build/test-results/test/TEST-*.xml' - - - name: Store test results - uses: actions/upload-artifact@v3 - if: always() - with: - name: test-results - path: '**/build/test-results/test/TEST-*.xml' - - - name: Test Report Summary - if: always() - run: | - echo '## Test Report Summary' >> $GITHUB_STEP_SUMMARY - echo '```' >> $GITHUB_STEP_SUMMARY - ./gradlew test --console=plain || true - echo '```' >> $GITHUB_STEP_SUMMARY \ No newline at end of file diff --git a/.github/workflows/manual-prod-deploy.yaml b/.github/workflows/manual-prod-deploy.yaml new file mode 100644 index 00000000..45573a0f --- /dev/null +++ b/.github/workflows/manual-prod-deploy.yaml @@ -0,0 +1,37 @@ +name: Manual Production Deployment + +on: + workflow_dispatch: + inputs: + tag: + description: 'Tag to deploy (v1.0.0)' + required: true + +jobs: + deploy-to-prod: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.event.inputs.tag }} + + - name: Deploy to Prod NCP Server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.PROD_NCP_SERVER_HOST }} + username: ${{ secrets.PROD_NCP_SERVER_USERNAME }} + password: ${{ secrets.PROD_NCP_SERVER_PASSWORD }} + port: ${{ secrets.PROD_NCP_SERVER_PORT }} + script: | + docker pull ${{ secrets.DOCKERHUB_USERNAME }}/spot-server:${{ github.event.inputs.tag }} + docker stop spot-server-prod || true + docker rm spot-server-prod || true + docker run -d --name spot-server-prod \ + -p 8080:8080 \ + -e SPRING_DATASOURCE_URL=${{ secrets.PROD_DB_URL }} \ + -e SPRING_DATASOURCE_USERNAME=${{ secrets.PROD_DB_USERNAME }} \ + -e SPRING_DATASOURCE_PASSWORD=${{ secrets.PROD_DB_PASSWORD }} \ + -e TZ=Asia/Seoul \ + ${{ secrets.DOCKERHUB_USERNAME }}/spot-server:${{ github.event.inputs.tag }} + docker system prune -af \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..a9b87f5e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +# 빌드 스테이지 +FROM gradle:7.4-jdk17 AS build +WORKDIR /app +COPY . . +RUN ./gradlew build -x test + +# 실행 스테이지 +FROM openjdk:17-jdk-slim +WORKDIR /app +COPY --from=build /app/application/build/libs/*.jar app.jar +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index ed72f3b5..b0ffe759 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,16 +29,12 @@ services: container_name: spot-spring-server ports: - 8080:8080 - depends_on: # 항상 mysql 실행하고 서버 실행되게 함. - mysql: - condition: service_healthy # mysql 컨테이너의 healthcheck가 정상일 때까지 대기! environment: - SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/spot - SPRING_DATASOURCE_USERNAME: test1234 - SPRING_DATASOURCE_PASSWORD: test1234 + - SPRING_DATASOURCE_URL=${DB_URL} + - SPRING_DATASOURCE_USERNAME=${DB_USERNAME} + - SPRING_DATASOURCE_PASSWORD=${DB_PASSWORD} + - TZ=Asia/Seoul volumes: - ./:/app - ~/.gradle:/root/.gradle command: ./gradlew :application:bootRun - env_file: - - .env \ No newline at end of file diff --git a/infrastructure/jpa/src/main/resources/application-jpa.yaml b/infrastructure/jpa/src/main/resources/application-jpa.yaml index 4fee764d..a4939dbd 100644 --- a/infrastructure/jpa/src/main/resources/application-jpa.yaml +++ b/infrastructure/jpa/src/main/resources/application-jpa.yaml @@ -18,6 +18,7 @@ spring: init: mode: never # 필요한 경우 'never'로 변경 + server: port: 8080