From 272b6a0d120615d77c4f94496b038f1ebf7285d3 Mon Sep 17 00:00:00 2001 From: Jindongleee Date: Tue, 15 Oct 2024 02:05:23 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89Start:=20Start=20New=20Project?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd.yml | 72 +++++++++++++++++++ build.gradle | 1 + .../gather_back_end/HelloController.java | 16 +++++ .../gather_back_end/config/SwaggerConfig.java | 29 ++++++++ .../gather_back_end/config/WebConfig.java | 16 +++++ 5 files changed, 134 insertions(+) create mode 100644 src/main/java/org/example/gather_back_end/HelloController.java create mode 100644 src/main/java/org/example/gather_back_end/config/SwaggerConfig.java create mode 100644 src/main/java/org/example/gather_back_end/config/WebConfig.java diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 8b13789..a6185b6 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -1 +1,73 @@ +name: cd with Gradle +on: + push: + branches: + - main # main 브랜치에 push될 때 + +permissions: write-all # 테스트 결과 작성을 위해 쓰기권한 추가 + +jobs: + build: + if: github.event.pull_request.merged == true || github.event_name == 'push' # PR merge 또는 push일 때 실행 + runs-on: ubuntu-latest # 환경 + steps: + - name: Checkout repository + uses: actions/checkout@v3 # 코드 체크아웃 = 가상환경 복사 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 #jdk 컴파일러 + with: + java-version: '17' + distribution: 'temurin' # 자바 개발 키트 배포판 + + - 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: Grant execute permission for Gradlew + run: chmod +x gradlew + + - name: Build with Gradle + run: ./gradlew build -x test #빌드 + + - name: Ensure target directory exists + uses: appleboy/ssh-action@master # SSH를 통해 대상 디렉토리 생성 + with: + username: ubuntu + host: ${{ secrets.SSH_HOST }} + key: ${{ secrets.SSH_KEY }} + script: | + mkdir -p /home/ubuntu/gather_back_end + + - name: List build directory + run: ls -la build/libs + + - name: Transfer Build File using SCP + uses: appleboy/scp-action@master # .jar 파일 원격 서버로 복사 + with: + username: ubuntu + host: ${{ secrets.SSH_HOST }} + key: ${{ secrets.SSH_KEY }} + source: | + build/libs/gather_back_end-0.0.1-SNAPSHOT.jar + target: "/home/ubuntu/gather_back_end" + + - name: Deploy using Docker Compose via SSH(Prod) + if: github.ref == 'refs/heads/main' + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SSH_HOST }} + username: ubuntu + key: ${{ secrets.SSH_KEY }} + script_stop: true + script: | + cd /home/ubuntu/gather_back_end || exit 1 # 경로 변경 실패 시 종료 + sudo docker compose --env-file .env down + sudo docker compose --env-file .env up --build -d diff --git a/build.gradle b/build.gradle index 3a98c3d..a388b5d 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,7 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' } tasks.named('test') { diff --git a/src/main/java/org/example/gather_back_end/HelloController.java b/src/main/java/org/example/gather_back_end/HelloController.java new file mode 100644 index 0000000..155597c --- /dev/null +++ b/src/main/java/org/example/gather_back_end/HelloController.java @@ -0,0 +1,16 @@ +package org.example.gather_back_end; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("api") +public class HelloController { + + @GetMapping + public String helloGetMapping(){ + return "hello"; + } +} diff --git a/src/main/java/org/example/gather_back_end/config/SwaggerConfig.java b/src/main/java/org/example/gather_back_end/config/SwaggerConfig.java new file mode 100644 index 0000000..bff4a0d --- /dev/null +++ b/src/main/java/org/example/gather_back_end/config/SwaggerConfig.java @@ -0,0 +1,29 @@ +package org.example.gather_back_end.config; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.servers.Server; +import lombok.RequiredArgsConstructor; +import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@OpenAPIDefinition( + info = @Info(title = "Gather", description = "4호선톤 숙쓰러운 서성한", version = "v1"), + servers = { + @Server(url = "http://158.180.73.142:8080", description = "서버 URL"), + @Server(url = "http://localhost:8080", description = "로컬 URL") + } +) +@RequiredArgsConstructor +@Configuration +public class SwaggerConfig { + + @Bean + public GroupedOpenApi SwaggerOpenApi() { + return GroupedOpenApi.builder() + .group("Swagger-api") + .pathsToMatch("/api/**") + .build(); + } +} diff --git a/src/main/java/org/example/gather_back_end/config/WebConfig.java b/src/main/java/org/example/gather_back_end/config/WebConfig.java new file mode 100644 index 0000000..239ca8d --- /dev/null +++ b/src/main/java/org/example/gather_back_end/config/WebConfig.java @@ -0,0 +1,16 @@ +package org.example.gather_back_end.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedOrigins("*") + .allowedMethods("GET", "POST", "PATCH", "DELETE") + .maxAge(3000); + } +} \ No newline at end of file