From 0f3a40c18f98908de795e5893c94ef1b415c6fea Mon Sep 17 00:00:00 2001 From: hyew0nn Date: Wed, 11 Feb 2026 15:56:28 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20AWS=20EC2=20=EC=97=B0=EA=B2=B0=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd_template.yaml | 84 +++++++++++++++++------ .github/workflows/ci_template.yaml | 7 +- build.gradle | 8 +++ src/main/resources/application.properties | 2 +- 4 files changed, 75 insertions(+), 26 deletions(-) diff --git a/.github/workflows/cd_template.yaml b/.github/workflows/cd_template.yaml index a6756d9..5cd54f3 100644 --- a/.github/workflows/cd_template.yaml +++ b/.github/workflows/cd_template.yaml @@ -1,33 +1,75 @@ -name: Deploy to cloudtype +name: Deploy EC2 + on: workflow_dispatch: push: branches: - dev + jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 - - name: Connect deploy key - uses: cloudtype-github-actions/connect@v1 + uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 with: - token: ${{ secrets.CLOUDTYPE_TOKEN }} - ghtoken: ${{ secrets.GHP_TOKEN }} - - name: Deploy - uses: cloudtype-github-actions/deploy@v1 + java-version: '17' + distribution: 'corretto' + + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: gradlew bootJar + run: ./gradlew bootJar + + - name: copy jar to server + uses: appleboy/scp-action@master with: - token: ${{ secrets.CLOUDTYPE_TOKEN }} - project: hyewon-0607/mse-project - stage: main - yaml: | - name: mse-project - app: java@17 - options: - ports: 8080 - context: - git: - url: git@github.com:${{ github.repository }}.git - ref: ${{ github.ref }} - preset: java-springboot \ No newline at end of file + host: ${{ secrets.SSH_HOST }} + username: ec2-user + key: ${{ secrets.SSH_KEY }} + port: 22 + source: "build/libs/*.jar" + target: "/home/ec2-user/app" + strip_components: 2 + + - name: SSH Commands + uses: appleboy/ssh-action@v0.1.6 + with: + host: ${{ secrets.SSH_HOST }} + username: ec2-user + key: ${{ secrets.SSH_KEY }} + port: 22 + script_stop: true + script: | + APP_DIR="/home/ec2-user/app" + APP_NAME="mse-project" + + mkdir -p $APP_DIR + + PID=$(pgrep -f "java.*$APP_NAME") + if [ -n "$PID" ]; then + echo "Found running app (PID: $PID). Terminating..." + kill -15 $PID + + for i in {1..10}; do + if ! ps -p $PID > /dev/null; then break; fi + sleep 1 + done + + kill -9 $PID 2>/dev/null || true + fi + + JAR_NAME=$(ls -t $APP_DIR/${APP_NAME}-*.jar | head -n 1) + if [ -z "$JAR_NAME" ]; then + echo "Error: No JAR file found in $APP_DIR" + exit 1 + fi + + echo "Starting deployment: $JAR_NAME" + nohup java -jar $JAR_NAME > $APP_DIR/app.log 2>&1 & + + echo "Deployment submitted successfully." \ No newline at end of file diff --git a/.github/workflows/ci_template.yaml b/.github/workflows/ci_template.yaml index cb086ca..b20694a 100644 --- a/.github/workflows/ci_template.yaml +++ b/.github/workflows/ci_template.yaml @@ -3,9 +3,8 @@ name: CI # Event Trigger 특정 액션 (Push, Pull_Request)등이 명시한 Branch에서 일어나면 동작을 수행한다. on: - push: - # 배열로 여러 브랜치를 넣을 수 있다. - branches: [ dev, feat/* ] +# push: +# branches: [ dev, feat/* ] # github pull request 생성시 pull_request: branches: @@ -28,7 +27,7 @@ jobs: - name: java setup uses: actions/setup-java@v2 with: - distribution: 'adopt' # See 'Supported distributions' for available options + distribution: 'corretto' # See 'Supported distributions' for available options java-version: '17' - name: make executable gradlew diff --git a/build.gradle b/build.gradle index 6069c1c..98b6b47 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,14 @@ java { } } +bootJar { + archiveFileName = "mse-project.jar" +} + +jar { + enabled = false +} + configurations { compileOnly { extendsFrom annotationProcessor diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index af67a11..8029585 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ spring.application.name=mse_project -spring.profiles.active=local +spring.profiles.active=docker # Session server.servlet.session.timeout=60m From 913efac7cf044b6a30f3fcde8159bd2f279d942f Mon Sep 17 00:00:00 2001 From: hyew0nn Date: Wed, 11 Feb 2026 16:00:15 +0900 Subject: [PATCH 2/3] fix: action version upgrade --- .github/workflows/ci_template.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci_template.yaml b/.github/workflows/ci_template.yaml index b20694a..c458f82 100644 --- a/.github/workflows/ci_template.yaml +++ b/.github/workflows/ci_template.yaml @@ -25,7 +25,7 @@ jobs: # with은 plugin 파라미터 입니다. (java 17버전 셋업) - name: java setup - uses: actions/setup-java@v2 + uses: actions/setup-java@v4 with: distribution: 'corretto' # See 'Supported distributions' for available options java-version: '17' From 5eec53584b0033e72edb83836e3746206665ab14 Mon Sep 17 00:00:00 2001 From: hyew0nn Date: Thu, 12 Feb 2026 11:33:33 +0900 Subject: [PATCH 3/3] feat: deploy Spring Boot application using Docker on AWS EC2 --- .github/workflows/cd_template.yaml | 45 ++++--------------- docker-compose.yaml | 27 ++++++++--- .../resources/application-docker.properties | 2 +- .../resources/application-local.properties | 6 +++ src/main/resources/application.properties | 4 +- .../MseProjectApplicationTests.java | 2 + 6 files changed, 41 insertions(+), 45 deletions(-) create mode 100644 src/main/resources/application-local.properties diff --git a/.github/workflows/cd_template.yaml b/.github/workflows/cd_template.yaml index 5cd54f3..b952e80 100644 --- a/.github/workflows/cd_template.yaml +++ b/.github/workflows/cd_template.yaml @@ -34,42 +34,15 @@ jobs: port: 22 source: "build/libs/*.jar" target: "/home/ec2-user/app" - strip_components: 2 - - name: SSH Commands + - name: Deploy with Docker Compose uses: appleboy/ssh-action@v0.1.6 with: - host: ${{ secrets.SSH_HOST }} - username: ec2-user - key: ${{ secrets.SSH_KEY }} - port: 22 - script_stop: true - script: | - APP_DIR="/home/ec2-user/app" - APP_NAME="mse-project" - - mkdir -p $APP_DIR - - PID=$(pgrep -f "java.*$APP_NAME") - if [ -n "$PID" ]; then - echo "Found running app (PID: $PID). Terminating..." - kill -15 $PID - - for i in {1..10}; do - if ! ps -p $PID > /dev/null; then break; fi - sleep 1 - done - - kill -9 $PID 2>/dev/null || true - fi - - JAR_NAME=$(ls -t $APP_DIR/${APP_NAME}-*.jar | head -n 1) - if [ -z "$JAR_NAME" ]; then - echo "Error: No JAR file found in $APP_DIR" - exit 1 - fi - - echo "Starting deployment: $JAR_NAME" - nohup java -jar $JAR_NAME > $APP_DIR/app.log 2>&1 & - - echo "Deployment submitted successfully." \ No newline at end of file + host: ${{ secrets.SSH_HOST }} + username: ec2-user + key: ${{ secrets.SSH_KEY }} + port: 22 + script: | + cd /home/ec2-user/app + docker-compose down + docker-compose up -d --build \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 3cb96ad..fb23c27 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,7 +1,22 @@ -version: "3" +version: "3.8" + services: + app: + build: . + container_name: mse-app + ports: + - "8080:8080" + environment: + SPRING_PROFILES_ACTIVE: docker + MYSQL_USER: ${MYSQL_USER} + MYSQL_PASSWORD: ${MYSQL_PASSWORD} + depends_on: + - mysql + - redis + mysql: - image: mysql:latest + image: mysql:8.0 + container_name: mse-mysql ports: - "23306:3306" environment: @@ -9,14 +24,14 @@ services: MYSQL_DATABASE: ${MYSQL_DATABASE} MYSQL_USER: ${MYSQL_USER} MYSQL_PASSWORD: ${MYSQL_PASSWORD} + redis: - image: redis:latest + image: redis:7 + container_name: mse-redis ports: - "26379:6379" + nginx: image: nginx:latest ports: - "80:80" - depends_on: - - redis - - mysql \ No newline at end of file diff --git a/src/main/resources/application-docker.properties b/src/main/resources/application-docker.properties index f7bbd78..711a064 100644 --- a/src/main/resources/application-docker.properties +++ b/src/main/resources/application-docker.properties @@ -1,6 +1,6 @@ # MySQL spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://mysql:23306/mse_db +spring.datasource.url=jdbc:mysql://mysql:3306/mse_db # DB certification spring.datasource.username=${MYSQL_USER} diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties new file mode 100644 index 0000000..bd5b242 --- /dev/null +++ b/src/main/resources/application-local.properties @@ -0,0 +1,6 @@ +# certification +spring.config.import=optional:application-local-secret.properties + +# MySQL +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/mse_db \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8029585..317f640 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ spring.application.name=mse_project -spring.profiles.active=docker +#spring.profiles.active=local # Session server.servlet.session.timeout=60m @@ -10,4 +10,4 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.jpa.properties.hibernate.format_sql=true spring.jpa.show-sql=true logging.level.org.hibernate.SQL=debug -logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace +logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace \ No newline at end of file diff --git a/src/test/java/com/mse_project/MseProjectApplicationTests.java b/src/test/java/com/mse_project/MseProjectApplicationTests.java index e45aa77..bbd4b1a 100644 --- a/src/test/java/com/mse_project/MseProjectApplicationTests.java +++ b/src/test/java/com/mse_project/MseProjectApplicationTests.java @@ -2,8 +2,10 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; @SpringBootTest +@ActiveProfiles("test") class MseProjectApplicationTests { @Test