From c822122284c1dfca80f6707c1840efa7ffd6b6b9 Mon Sep 17 00:00:00 2001 From: Park-EJ Date: Wed, 30 Apr 2025 15:20:56 +0900 Subject: [PATCH 1/3] =?UTF-8?q?chore(cicd-setup):=20Dockerfile,=20Workflow?= =?UTF-8?q?s=20=EC=9E=91=EC=84=B1=20(#106)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 70 ++++++++++++++++++++++++++++++++++++ Dockerfile | 5 +++ 2 files changed, 75 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 Dockerfile diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..d7e63231 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,70 @@ +name: CI/CD to EC2 with Docker and ECR + +on: + push: + branches: + - dev + - feat/#106-cicd-setup + +jobs: + deploy: + name: Build and Deploy + runs-on: ubuntu-latest + + steps: + - name: Checkout Source Code + uses: actions/checkout@v4 + + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: make application.properties + run: | + cd ./src/main/resources + touch ./application.properties + echo "${{ secrets.APPLI_YML }}" > ./application.properties + shell: bash + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Build with Gradle + run: ./gradlew clean build -x test + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ap-northeast-2 + + - name: Log in to Amazon ECR + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build Docker Image + run: | + docker build -t ${{ secrets.ECR_REPO_URI }}:latest . + + - name: Push to ECR + run: | + docker push ${{ secrets.ECR_REPO_URI }}:latest + + - name: SSH to EC2 and Deploy + uses: appleboy/ssh-action@v1.0.0 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_SSH_KEY }} + script: | + aws ecr get-login-password --region ap-northeast-2 \ + | docker login --username AWS --password-stdin ${{ secrets.ECR_REPO_URI }} + docker pull ${{ secrets.ECR_REPO_URI }}:latest + docker stop app || true + docker rm app || true + docker run -d --name app -p 8080:8080 ${{ secrets.ECR_REPO_URI }}:latest \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..c6551dd8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM openjdk:17-jdk + +COPY ./build/libs/wait4eat-0.0.1-SNAPSHOT.jar app.jar + +ENTRYPOINT ["java", "-jar", "/app.jar"] \ No newline at end of file From 27e5dd0ece4b2a568053d64256fd4efea719c266 Mon Sep 17 00:00:00 2001 From: Park-EJ Date: Fri, 2 May 2025 15:40:19 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat(cicd-setup):=20=ED=97=AC=EC=8A=A4?= =?UTF-8?q?=EC=B2=B4=ED=81=AC=20(#106)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 2 +- .../global/healthcheck/HealthCheckController.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/wait4eat/global/healthcheck/HealthCheckController.java diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index d7e63231..967fff47 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,7 +3,7 @@ name: CI/CD to EC2 with Docker and ECR on: push: branches: - - dev + - main - feat/#106-cicd-setup jobs: diff --git a/src/main/java/com/example/wait4eat/global/healthcheck/HealthCheckController.java b/src/main/java/com/example/wait4eat/global/healthcheck/HealthCheckController.java new file mode 100644 index 00000000..ec2e1942 --- /dev/null +++ b/src/main/java/com/example/wait4eat/global/healthcheck/HealthCheckController.java @@ -0,0 +1,14 @@ +package com.example.wait4eat.global.healthcheck; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HealthCheckController { + + @GetMapping("/health") + public ResponseEntity healthCheck() { + return ResponseEntity.ok("OK"); + } +} \ No newline at end of file From 09c2c841adb5ade26f588ce931ec5b4e5dd47cf3 Mon Sep 17 00:00:00 2001 From: Park-EJ Date: Fri, 2 May 2025 15:48:34 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor(cicd-setup):=20=ED=97=AC=EC=8A=A4?= =?UTF-8?q?=EC=B2=B4=ED=81=AC=20=EA=B2=BD=EB=A1=9C=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?(#106)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/wait4eat/global/auth/security/SecurityConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/wait4eat/global/auth/security/SecurityConfig.java b/src/main/java/com/example/wait4eat/global/auth/security/SecurityConfig.java index e6e1bbaa..1f3637ad 100644 --- a/src/main/java/com/example/wait4eat/global/auth/security/SecurityConfig.java +++ b/src/main/java/com/example/wait4eat/global/auth/security/SecurityConfig.java @@ -56,7 +56,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .logout(AbstractHttpConfigurer::disable) .rememberMe(AbstractHttpConfigurer::disable) .authorizeHttpRequests(auth -> auth - .requestMatchers("/test-sse.html", "/css/**", "/payment/**", "/images/**").permitAll() + .requestMatchers("/test-sse.html", "/css/**", "/payment/**", "/images/**", "/health").permitAll() .requestMatchers("/" + tossWebhookEndpoint).permitAll() .requestMatchers(request -> request.getRequestURI().startsWith("/api/v1/notifications/subscribe")).permitAll() .requestMatchers(request -> request.getRequestURI().startsWith("/api/v1/auth")).permitAll()