diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..cd043d0 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,93 @@ +name: Java CI/CD with Gradle + +on: + push: + branches: [ "develop" ] + pull_request: + branches: [ "develop"] + types: [ closed ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + env: + PROFILE: ${{ secrets.PROFILE }} + DB_PROD_HOST: ${{ secrets.DB_PROD_HOST }} + DB_PROD_USERNAME: ${{ secrets.DB_PROD_USERNAME }} + DB_PROD_PASSWD: ${{ secrets.DB_PROD_PASSWD }} + WAS_HOST: ${{ secrets.WAS_HOST }} + JWT_SECRET: ${{ secrets.JWT_SECRET }} + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Build with Gradle + run: ./gradlew --warning-mode all build -i + + docker-build: + runs-on: ubuntu-latest + needs: build + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build with Gradle (without Test) + run: ./gradlew clean build -x test --stacktrace + + - name: Docker Hub build & push + run: | + docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_PWD }} + docker build --build-arg PROFILE=${{ secrets.PROFILE }} \ + --build-arg DB_PROD_HOST=${{ secrets.DB_PROD_HOST }} \ + --build-arg DB_PROD_USERNAME=${{ secrets.DB_PROD_USERNAME }} \ + --build-arg DB_PROD_PASSWD=${{ secrets.DB_PROD_PASSWD }} \ + --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} \ + -t likelion12/likelion12baby:latest . + docker images + docker push likelion12/likelion12baby:latest + + - name: deploy + uses: appleboy/ssh-action@master + with: + username: ubuntu + host: ${{ secrets.WAS_HOST }} + key: ${{ secrets.PRIVATE_KEY }} + port: ${{ secrets.WAS_PORT }} + script: | + echo "Checking Docker installation..." + CONTAINER_ID=$(sudo docker ps -q --filter ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}) + if [ ! -z "$CONTAINER_ID" ]; then + echo "Docker could not be found, installing Docker" + sudo docker stop $CONTAINER_ID + sudo docker rm $CONTAINER_ID + fi + echo "Pulling Docker image..." + sudo docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }} + sudo docker run -d -e PROFILE=${{ secrets.PROFILE }} \ + -e DB_PROD_HOST=${{ secrets.DB_PROD_HOST }} \ + -e DB_PROD_USERNAME=${{ secrets.DB_PROD_USERNAME }} \ + -e DB_PROD_PASSWD=${{ secrets.DB_PROD_PASSWD }} \ + -e JWT_SECRET=${{ secrets.JWT_SECRET }} \ + -p 8080:8080 ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }} + + sudo docker logs $(sudo docker ps -q --filter ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}) + + sudo docker image prune -f \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6ad92c4..feddecf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ HELP.md -.gradle build/ !gradle/wrapper/gradle-wrapper.jar !**/src/main/**/build/ diff --git a/.gradle/8.9/checksums/checksums.lock b/.gradle/8.9/checksums/checksums.lock new file mode 100644 index 0000000..22fa4ec Binary files /dev/null and b/.gradle/8.9/checksums/checksums.lock differ diff --git a/.gradle/8.9/checksums/md5-checksums.bin b/.gradle/8.9/checksums/md5-checksums.bin new file mode 100644 index 0000000..a46a08b Binary files /dev/null and b/.gradle/8.9/checksums/md5-checksums.bin differ diff --git a/.gradle/8.9/checksums/sha1-checksums.bin b/.gradle/8.9/checksums/sha1-checksums.bin new file mode 100644 index 0000000..c84a8d9 Binary files /dev/null and b/.gradle/8.9/checksums/sha1-checksums.bin differ diff --git a/.gradle/8.9/dependencies-accessors/gc.properties b/.gradle/8.9/dependencies-accessors/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/8.9/executionHistory/executionHistory.bin b/.gradle/8.9/executionHistory/executionHistory.bin new file mode 100644 index 0000000..9c76838 Binary files /dev/null and b/.gradle/8.9/executionHistory/executionHistory.bin differ diff --git a/.gradle/8.9/executionHistory/executionHistory.lock b/.gradle/8.9/executionHistory/executionHistory.lock new file mode 100644 index 0000000..10c48c7 Binary files /dev/null and b/.gradle/8.9/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.9/fileChanges/last-build.bin b/.gradle/8.9/fileChanges/last-build.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/.gradle/8.9/fileChanges/last-build.bin differ diff --git a/.gradle/8.9/fileHashes/fileHashes.bin b/.gradle/8.9/fileHashes/fileHashes.bin new file mode 100644 index 0000000..484101b Binary files /dev/null and b/.gradle/8.9/fileHashes/fileHashes.bin differ diff --git a/.gradle/8.9/fileHashes/fileHashes.lock b/.gradle/8.9/fileHashes/fileHashes.lock new file mode 100644 index 0000000..c1065d8 Binary files /dev/null and b/.gradle/8.9/fileHashes/fileHashes.lock differ diff --git a/.gradle/8.9/fileHashes/resourceHashesCache.bin b/.gradle/8.9/fileHashes/resourceHashesCache.bin new file mode 100644 index 0000000..45575ee Binary files /dev/null and b/.gradle/8.9/fileHashes/resourceHashesCache.bin differ diff --git a/.gradle/8.9/gc.properties b/.gradle/8.9/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock new file mode 100644 index 0000000..239f203 Binary files /dev/null and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/cache.properties b/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 0000000..66f39a5 --- /dev/null +++ b/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Tue Jul 30 01:49:23 KST 2024 +gradle.version=8.9 diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin new file mode 100644 index 0000000..d84ecda Binary files /dev/null and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/.gradle/file-system.probe b/.gradle/file-system.probe new file mode 100644 index 0000000..96a6906 Binary files /dev/null and b/.gradle/file-system.probe differ diff --git a/.gradle/vcs-1/gc.properties b/.gradle/vcs-1/gc.properties new file mode 100644 index 0000000..e69de29 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d6820fa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM openjdk:17-alpine + +ARG PROFILE +ARG DB_PROD_HOST +ARG DB_PROD_USERNAME +ARG DB_PROD_PASSWD +ARG JWT_SECRET + +ENV PROFILE=${PROFILE} +ENV DB_PROD_HOST=${DB_PROD_HOST} +ENV DB_PROD_USERNAME=${DB_PROD_USERNAME} +ENV DB_PROD_PASSWD=${DB_PROD_PASSWD} +ENV JWT_SECRET=${JWT_SECRET} + +# Install Redis +RUN apk update && \ + apk add redis + +# Copy application JAR +ARG JAR_PATH=build/libs/*.jar +COPY ${JAR_PATH} /home/server.jar + +# Expose ports +EXPOSE 8080 6379 + +# Entry point +ENTRYPOINT sh -c "redis-server --daemonize yes && java -jar /home/server.jar" diff --git a/build.gradle b/build.gradle index 0eca979..6c70cb2 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,7 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' runtimeOnly 'com.mysql:mysql-connector-j' + implementation 'mysql:mysql-connector-java:8.0.32' } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index e644113..2c35211 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a441313..09523c0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index b740cf1..f5feea6 100755 --- a/gradlew +++ b/gradlew @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -84,7 +86,8 @@ done # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum diff --git a/gradlew.bat b/gradlew.bat index 25da30d..9b42019 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,92 +1,94 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%"=="" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%"=="" set DIRNAME=. -@rem This is normally unused -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if %ERRORLEVEL% equ 0 goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. 1>&2 -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 -echo. 1>&2 -echo Please set the JAVA_HOME variable in your environment to match the 1>&2 -echo location of your Java installation. 1>&2 - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if %ERRORLEVEL% equ 0 goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -set EXIT_CODE=%ERRORLEVEL% -if %EXIT_CODE% equ 0 set EXIT_CODE=1 -if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% -exit /b %EXIT_CODE% - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/com/example/likelion12/TestController.java b/src/main/java/com/example/likelion12/TestController.java new file mode 100644 index 0000000..f0cc9f3 --- /dev/null +++ b/src/main/java/com/example/likelion12/TestController.java @@ -0,0 +1,13 @@ +package com.example.likelion12; + +import lombok.Getter; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + @GetMapping("") + public String likelion(){ + return "test"; + } +} diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 2ab2e88..1bd88fb 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -11,7 +11,7 @@ spring: enabled: false jpa: hibernate: - ddl-auto: create + ddl-auto: update properties: hibernate: format_sql: true @@ -22,4 +22,4 @@ logging.level: org.hibernate.SQL: debug org.hibernate.type: trace org.springframework.web : DEBUG - org.springframework.cache: DEBUG + org.springframework.cache: DEBUG \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml new file mode 100644 index 0000000..11b5968 --- /dev/null +++ b/src/main/resources/application-prod.yml @@ -0,0 +1,24 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://${DB_PROD_HOST}:3306/likelionbaby12?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul + username: ${DB_PROD_USERNAME} + password: ${DB_PROD_PASSWD} + + devtools: + restart: + enabled: false + jpa: + hibernate: + ddl-auto: update + properties: + hibernate: + format_sql: true + default_batch_fetch_size: 100 + open-in-view: false + +logging.level: + org.hibernate.SQL: debug + org.hibernate.type: trace + org.springframework.web : DEBUG + org.springframework.cache: DEBUG \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 188ea6c..830a8e6 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,14 +1,35 @@ spring: profiles: active: ${PROFILE} + --- spring: config: activate: on-profile: local + jpa: + hibernate: + ddl-auto: update + show-sql: true + --- +spring: + config: + activate: + on-profile: prod + jpa: + hibernate: + ddl-auto: update + show-sql: true +--- + logging: level: org: springframework: - web: DEBUG \ No newline at end of file + web: DEBUG + +jwt: + secret: ${JWT_SECRET} + access-token-expiration: 3600000 # 1 hour in milliseconds + refresh-token-expiration: 86400000 # 1 day in milliseconds diff --git a/src/test/java/com/example/demo/Likelion12ApplicationTests.java b/src/test/java/com/example/demo/Likelion12ApplicationTests.java deleted file mode 100644 index bc28f2d..0000000 --- a/src/test/java/com/example/demo/Likelion12ApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class Likelion12ApplicationTests { - - @Test - void contextLoads() { - } - -}