diff --git a/.github/workflows/deploy-on-ec2.yml b/.github/workflows/deploy-on-ec2.yml deleted file mode 100644 index a237292..0000000 --- a/.github/workflows/deploy-on-ec2.yml +++ /dev/null @@ -1,142 +0,0 @@ -name: Deploy to EC2 - -on: - workflow_run: - workflows: ["Backend Build and Push", "Frontend Build and Push"] - types: - - completed - branches: - - '08-cicd' - -jobs: - # --- 백엔드 배포 전용 Job --- - deploy-backend: - if: ${{ github.event.workflow_run.workflow.name == 'Backend Build and Push' && github.event.workflow_run.conclusion == 'success' }} - runs-on: ubuntu-latest - steps: - - name: Deploy Backend to EC2 instance - uses: appleboy/ssh-action@v1.0.3 - with: - host: ${{ secrets.EC2_HOST }} - username: ${{ secrets.EC2_USERNAME }} - key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} - script: | - # EC2에 docker-compose.yml이 없다면 초기 버전을 생성 - if [ ! -f docker-compose.yml ]; then - echo "Creating initial docker-compose.yml" - cat << EOF > docker-compose.yml - version: '3.8' - services: - leafy-postgres: - image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:latest - volumes: - - leafy_data:/var/lib/postgresql/data - restart: always - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 5s - timeout: 5s - retries: 5 - leafy-backend: - image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:initial - restart: on-failure - environment: - - SPRING_DATASOURCE_URL=jdbc:postgresql://leafy-postgres:5432/postgres - - SPRING_DATASOURCE_USERNAME=postgres - - SPRING_DATASOURCE_PASSWORD=postgres - depends_on: - leafy-postgres: - condition: service_healthy - leafy-front: - image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:initial - restart: on-failure - ports: - - "80:80" - depends_on: - - leafy-backend - volumes: - leafy_data: - EOF - fi - - # 1. 백엔드 이미지 태그만 최신 버전으로 변경 - export BACKEND_IMAGE=${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:${{ github.event.workflow_run.head_sha }} - sed -i "s|image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:.*|image: \$BACKEND_IMAGE|g" docker-compose.yml - - echo "Docker Hub Login..." - docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_TOKEN }}" - - echo "Pulling updated backend image..." - docker-compose pull leafy-backend - - echo "Restarting services..." - docker-compose up -d --remove-orphans - - echo "Pruning old images..." - docker image prune -af - - # --- 프론트엔드 배포 전용 Job --- - deploy-frontend: - if: ${{ github.event.workflow_run.workflow.name == 'Frontend Build and Push' && github.event.workflow_run.conclusion == 'success' }} - runs-on: ubuntu-latest - steps: - - name: Deploy Frontend to EC2 instance - uses: appleboy/ssh-action@v1.0.3 - with: - host: ${{ secrets.EC2_HOST }} - username: ${{ secrets.EC2_USERNAME }} - key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} - script: | - # EC2에 docker-compose.yml이 없다면 초기 버전을 생성 (위와 동일) - if [ ! -f docker-compose.yml ]; then - echo "Creating initial docker-compose.yml" - cat << EOF > docker-compose.yml - version: '3.8' - services: - leafy-postgres: - image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:latest - volumes: - - leafy_data:/var/lib/postgresql/data - restart: always - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 5s - timeout: 5s - retries: 5 - leafy-backend: - image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:initial - restart: on-failure - environment: - - SPRING_DATASOURCE_URL=jdbc:postgresql://leafy-postgres:5432/postgres - - SPRING_DATASOURCE_USERNAME=postgres - - SPRING_DATASOURCE_PASSWORD=postgres - depends_on: - leafy-postgres: - condition: service_healthy - leafy-front: - image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:initial - restart: on-failure - ports: - - "80:80" - depends_on: - - leafy-backend - volumes: - leafy_data: - EOF - fi - - # 1. 프론트엔드 이미지 태그만 최신 버전으로 변경 - export FRONTEND_IMAGE=${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ github.event.workflow_run.head_sha }} - sed -i "s|image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:.*|image: \$FRONTEND_IMAGE|g" docker-compose.yml - - echo "Docker Hub Login..." - docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_TOKEN }}" - - echo "Pulling updated frontend image..." - docker-compose pull leafy-frontend - - echo "Restarting services..." - docker-compose up -d --remove-orphans - - echo "Pruning old images..." - docker image prune -af diff --git a/.github/workflows/initial-setup.yml b/.github/workflows/initial-setup.yml new file mode 100644 index 0000000..d1c7f68 --- /dev/null +++ b/.github/workflows/initial-setup.yml @@ -0,0 +1,80 @@ +# .github/workflows/initial-setup.yml +name: Initial EC2 Setup + +on: + workflow_dispatch: + +jobs: + setup: + runs-on: ubuntu-latest + steps: + - name: Setup EC2 instance + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} + script: | + # --- 1. Docker 및 Docker Compose 설치 --- + if ! command -v docker &> /dev/null; then + echo "Installing Docker..." + sudo apt-get update && sudo apt-get install -y docker.io + sudo systemctl start docker && sudo systemctl enable docker + sudo usermod -aG docker ${{ secrets.EC2_USERNAME }} + sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + sudo chmod +x /usr/local/bin/docker-compose + fi + + # --- 2. docker-compose.yml 파일 생성 (검증된 버전) --- + echo "Creating docker-compose.yml..." + cat << EOF > docker-compose.yml + version: '3.8' + services: + leafy-postgres: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:latest + ports: + - "5432:5432" + environment: + - POSTGRES_USER=${{ secrets.DATASOURCE_USERNAME }} + - POSTGRES_PASSWORD=${{ secrets.DATASOURCE_PASSWORD }} + - POSTGRES_DB=${{ secrets.DATASOURCE_URL }} + volumes: + - leafy_data:/var/lib/postgresql/data + restart: always + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${{ secrets.DATASOURCE_USERNAME }}"] + interval: 5s + timeout: 5s + retries: 5 + leafy-backend: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:latest + restart: on-failure + environment: + - SPRING_DATASOURCE_URL=jdbc:postgresql://leafy-postgres:5432/${{ secrets.DATASOURCE_URL }} + - SPRING_DATASOURCE_USERNAME=${{ secrets.DATASOURCE_USERNAME }} + - SPRING_DATASOURCE_PASSWORD=${{ secrets.DATASOURCE_PASSWORD }} + - SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT=org.hibernate.dialect.PostgreSQLDialect + depends_on: + leafy-postgres: + condition: service_healthy + leafy-front: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:latest + restart: on-failure + ports: + - "80:80" + environment: + - BACKEND_HOST=leafy-backend + - BACKEND_PORT=8080 + depends_on: + - leafy-backend + volumes: + leafy_data: + EOF + + # --- 3. 서비스 시작 --- + echo "Starting services..." + sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_TOKEN }}" + sudo docker-compose pull + sudo docker-compose up -d + + echo "Initial setup complete." diff --git a/.github/workflows/leafy-backend-build-and-push.yml b/.github/workflows/leafy-backend-build-and-push.yml index d192fd7..af7cf4c 100644 --- a/.github/workflows/leafy-backend-build-and-push.yml +++ b/.github/workflows/leafy-backend-build-and-push.yml @@ -1,42 +1,98 @@ -name: Backend Build and Push +# .github/workflows/leafy-backend-build-and-push.yml + +name: Backend CI/CD on: push: branches: - - 08-cicd - # 08-cicd 브랜치에 push될 때 워크플로우가 실행됩니다. + - '08-cicd' paths: - 'leafy-backend/**' - -jobs: - build-and-push: - runs-on: ubuntu-latest - # 가장 최신의 Ubuntu 러너를 사용합니다. + - '.github/workflows/leafy-backend-build-and-push.yml' +jobs: + # --- 1. 빌드 및 푸시 Job --- + build: + runs-on: ubuntu-latest + outputs: + sha_tag: ${{ steps.get_sha.outputs.sha }} steps: - - name: Checkout Repository - uses: actions/checkout@v2 - # 현재 리포지토리를 체크아웃합니다. + - name: Checkout Repository + uses: actions/checkout@v4 + - name: Get short commit sha + id: get_sha + run: echo "sha=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_OUTPUT + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and Push Docker Image + uses: docker/build-push-action@v5 + with: + context: ./leafy-backend + file: ./leafy-backend/Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:${{ steps.get_sha.outputs.sha }},${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:latest + platforms: linux/amd64 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - # Docker Buildx를 설정합니다. - - - name: Login to Docker Hub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - # GitHub Secret에서 Docker Hub 사용자 이름을 가져옵니다. - password: ${{ secrets.DOCKERHUB_TOKEN }} - # GitHub Secret에서 Docker Hub 액세스 토큰을 가져옵니다. + # --- 2. 배포 Job --- + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Deploy to EC2 instance + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} + script: | + # --- 1. docker-compose.yml 파일이 없으면 새로 생성 --- + if [ ! -f docker-compose.yml ]; then + echo "Creating initial docker-compose.yml" + cat << 'EOF' > docker-compose.yml + services: + leafy-postgres: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:latest + ports: ["5432:5432"] + environment: + - POSTGRES_USER=${{ secrets.DATASOURCE_USERNAME }} + - POSTGRES_PASSWORD=${{ secrets.DATASOURCE_PASSWORD }} + - POSTGRES_DB=${{ secrets.DATASOURCE_URL }} + volumes: [- leafy_data:/var/lib/postgresql/data] + restart: always + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${{ secrets.DATASOURCE_USERNAME }}"] + interval: 5s + timeout: 5s + retries: 5 + leafy-backend: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:${{ needs.build.outputs.sha_tag }} + restart: on-failure + environment: + - SPRING_DATASOURCE_URL=jdbc:postgresql://leafy-postgres:5432/${{ secrets.DATASOURCE_URL }} + - SPRING_DATASOURCE_USERNAME=${{ secrets.DATASOURCE_USERNAME }} + - SPRING_DATASOURCE_PASSWORD=${{ secrets.DATASOURCE_PASSWORD }} + depends_on: + leafy-postgres: { condition: service_healthy } + leafy-front: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:latest + restart: on-failure + ports: ["80:80"] + depends_on: ["leafy-backend"] + volumes: + leafy_data: + EOF + fi + + # --- 2. 백엔드 이미지 태그만 수정 --- + sed -i "s|image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:.*|image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:${{ needs.build.outputs.sha_tag }}|g" docker-compose.yml - - name: Build and Push - uses: docker/build-push-action@v2 - with: - context: ./leafy-backend - # Dockerfile이 있는 위치입니다. - file: ./leafy-backend/Dockerfile - # Dockerfile의 경로입니다. - push: true # 이미지를 레지스트리에 푸시합니다. - tags: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:${{ github.sha }} - platforms: linux/amd64,linux/arm64,windows/amd64 + + sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_TOKEN }}" + sudo docker-compose pull leafy-backend + sudo docker-compose up -d --remove-orphans + sudo docker image prune -af diff --git a/.github/workflows/leafy-frontend-build-and-push.yml b/.github/workflows/leafy-frontend-build-and-push.yml index 5a0bbb8..b43ae83 100644 --- a/.github/workflows/leafy-frontend-build-and-push.yml +++ b/.github/workflows/leafy-frontend-build-and-push.yml @@ -1,44 +1,103 @@ name: Frontend Build and Push - +# .github/workflows/frontend-cicd.yml on: push: branches: - - 08-cicd - # 08-cicd 브랜치에 push될 때 워크플로우가 실행됩니다. + - '08-cicd' paths: - 'leafy-frontend/**' + - '.github/workflows/leafy-frontend-build-and-push.yml' jobs: - build-and-push: - runs-on: ubuntu-latest - # 가장 최신의 Ubuntu 러너를 사용합니다. - + build: + runs-on: ubuntu-latest + outputs: + sha_tag: ${{ steps.get_sha.outputs.sha }} steps: - - name: Checkout Repository - uses: actions/checkout@v2 - # 현재 리포지토리를 체크아웃합니다. - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - # Docker Buildx를 설정합니다. + - name: Checkout Repository + uses: actions/checkout@v4 + - name: Get short commit sha + id: get_sha + run: echo "sha=$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_OUTPUT + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and Push Docker Image + uses: docker/build-push-action@v5 + with: + context: ./leafy-frontend + file: ./leafy-frontend/Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ steps.get_sha.outputs.sha }},${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:latest + platforms: linux/amd64 - - name: Login to Docker Hub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - # GitHub Secret에서 Docker Hub 사용자 이름을 가져옵니다. - password: ${{ secrets.DOCKERHUB_TOKEN }} - # GitHub Secret에서 Docker Hub 액세스 토큰을 가져옵니다. - - - name: Build and Push - uses: docker/build-push-action@v2 - with: - context: ./leafy-frontend - # Dockerfile이 있는 위치입니다. - file: ./leafy-frontend/Dockerfile - # Dockerfile의 경로입니다. - push: true - # 이미지를 레지스트리에 푸시합니다. - tags: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ github.sha }} - platforms: linux/amd64,linux/arm64,windows/amd64 + deploy: + needs: build + runs-on: ubuntu-latest + steps: + - name: Deploy to EC2 instance + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_SSH_PRIVATE_KEY }} + script: | + + # --- 1. docker-compose.yml 파일이 없으면 새로 생성 --- + if [ ! -f docker-compose.yml ]; then + echo "Creating initial docker-compose.yml" + cat << 'EOF' > docker-compose.yml + version: '3.8' + services: + leafy-postgres: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:latest + ports: + - "5432:5432" + environment: + - POSTGRES_USER=${{ secrets.DATASOURCE_USERNAME }} + - POSTGRES_PASSWORD=${{ secrets.DATASOURCE_PASSWORD }} + - POSTGRES_DB=${{ secrets.DATASOURCE_URL }} + volumes: + - leafy_data:/var/lib/postgresql/data + restart: always + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${{ secrets.DATASOURCE_USERNAME }}"] + interval: 5s + timeout: 5s + retries: 5 + leafy-backend: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:latest + restart: on-failure + environment: + - SPRING_DATASOURCE_URL=jdbc:postgresql://leafy-postgres:5432/${{ secrets.DATASOURCE_URL }} + - SPRING_DATASOURCE_USERNAME=${{ secrets.DATASOURCE_USERNAME }} + - SPRING_DATASOURCE_PASSWORD=${{ secrets.DATASOURCE_PASSWORD }} + depends_on: + leafy-postgres: + condition: service_healthy + leafy-front: + image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ needs.build.outputs.sha_tag }} + restart: on-failure + ports: + - "80:80" + depends_on: + - leafy-backend + volumes: + leafy_data: + EOF + fi + echo "Starting services..." + + export IMAGE_NAME=${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:${{ needs.build.outputs.sha_tag }} + # 프론트엔드 이미지 태그만 수정 + sed -i "s|image: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-frontend:.*|image: \$IMAGE_NAME|g" docker-compose.yml + + sudo docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" -p "${{ secrets.DOCKERHUB_TOKEN }}" + sudo docker-compose pull leafy-frontend + sudo docker-compose up -d --remove-orphans + sudo docker image prune -af diff --git a/.github/workflows/leafy-postgres-build-and-push.yml b/.github/workflows/leafy-postgres-build-and-push.yml deleted file mode 100644 index b2fbd08..0000000 --- a/.github/workflows/leafy-postgres-build-and-push.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Postgres Build and Push - -on: - push: - branches: - - 08-cicd - # 08-cicd 브랜치에 push될 때 워크플로우가 실행됩니다. - paths: - - 'leafy-postgresql/**' - -jobs: - build-and-push: - runs-on: ubuntu-latest - # 가장 최신의 Ubuntu 러너를 사용합니다. - - steps: - - name: Checkout Repository - uses: actions/checkout@v2 - # 현재 리포지토리를 체크아웃합니다. - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - # Docker Buildx를 설정합니다. - - - name: Login to Docker Hub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - # GitHub Secret에서 Docker Hub 사용자 이름을 가져옵니다. - password: ${{ secrets.DOCKERHUB_TOKEN }} - # GitHub Secret에서 Docker Hub 액세스 토큰을 가져옵니다. - - - name: Build and Push - uses: docker/build-push-action@v2 - with: - context: ./leafy-postgresql - # Dockerfile이 있는 위치입니다. - file: ./leafy-postgresql/Dockerfile - # Dockerfile의 경로입니다. - push: true - # 이미지를 레지스트리에 푸시합니다. - tags: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:${{ github.sha }} - - platforms: linux/amd64,linux/arm64,windows/amd64 - diff --git a/.github/workflows/postgres-build.yml b/.github/workflows/postgres-build.yml new file mode 100644 index 0000000..5ab36a9 --- /dev/null +++ b/.github/workflows/postgres-build.yml @@ -0,0 +1,39 @@ +# .github/workflows/postgres-build.yml + +name: Postgres Build and Push + +on: + push: + branches: + - '08-cicd' + paths: + - 'leafy-postgresql/**' + - '.github/workflows/postgres-build.yml' + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Build and Push + uses: docker/build-push-action@v5 + with: + context: ./leafy-postgresql + file: ./leafy-postgresql/Dockerfile + push: true + tags: | + ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:${{ github.sha }} + ${{ secrets.DOCKERHUB_USERNAME }}/leafy-postgres:latest + + platforms: linux/amd64,linux/arm64 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f10862a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.env diff --git a/README.md b/README.md index c1b09b5..5b3b83a 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -심심하다 +심심하다2 diff --git a/leafy-backend/Dockerfile b/leafy-backend/Dockerfile index 7d2449c..f3392ae 100644 --- a/leafy-backend/Dockerfile +++ b/leafy-backend/Dockerfile @@ -1,5 +1,5 @@ # 빌드 이미지로 OpenJDK 11 & Gradle을 지정 -FROM gradle:7.6.1-jdk11 AS build +FROM gradle:jdk11 AS build # 소스코드를 복사할 작업 디렉토리를 생성 WORKDIR /app @@ -15,8 +15,8 @@ COPY . /app # Gradle 빌드를 실행하여 JAR 파일 생성 RUN gradle clean build --no-daemon -# 런타임 이미지로 OpenJDK 11-jre-slim 지정 -FROM openjdk:11-jre-slim +# 런타임 이미지로 eclipse-temurin 11 JRE를 지정 +FROM eclipse-temurin:11-jre # 애플리케이션을 실행할 작업 디렉토리를 생성 WORKDIR /app diff --git a/leafy-backend/build.gradle b/leafy-backend/build.gradle index e5546f9..e5d01d6 100644 --- a/leafy-backend/build.gradle +++ b/leafy-backend/build.gradle @@ -24,13 +24,17 @@ dependencies { compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' - testImplementation 'org.springframework.boot:spring-boot-starter-test' runtimeOnly 'org.postgresql:postgresql' implementation 'org.springframework.boot:spring-boot-starter-validation' - implementation 'org.springframework.security:spring-security-core:5.5.1' + testCompileOnly 'org.projectlombok:lombok' + testAnnotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testImplementation 'com.h2database:h2' + testImplementation 'org.springframework.security:spring-security-test' + implementation 'org.springframework.boot:spring-boot-starter-actuator' } diff --git a/leafy-backend/src/main/java/com/devwiki/leafy/controller/user/UserController.java b/leafy-backend/src/main/java/com/devwiki/leafy/controller/user/UserController.java index bc19525..c13f573 100644 --- a/leafy-backend/src/main/java/com/devwiki/leafy/controller/user/UserController.java +++ b/leafy-backend/src/main/java/com/devwiki/leafy/controller/user/UserController.java @@ -50,7 +50,7 @@ public ResponseEntity getUserById(@PathVariable Long userId) { * @return 추가된 사용자 정보 */ @PostMapping("") - public ResponseEntity addUser(@RequestBody UserRequestDto userRequestDto) { + public ResponseEntity addUser(@Valid @RequestBody UserRequestDto userRequestDto) { UserResponseDto addedUserResponseDto = userService.createUser(userRequestDto); return new ResponseEntity<>(addedUserResponseDto, HttpStatus.CREATED); } diff --git a/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantDetailDto.java b/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantDetailDto.java index ac8e423..648787f 100644 --- a/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantDetailDto.java +++ b/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantDetailDto.java @@ -1,22 +1,31 @@ package com.devwiki.leafy.dto.plant; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; import java.time.LocalDateTime; +@NoArgsConstructor @Data +@Builder +@AllArgsConstructor public class PlantDetailDto { - private Long plantId; - private String plantName; - private String plantType; - private String plantDesc; - private String imageUrl; - private float temperatureLow; - private float temperatureHigh; - private float humidityLow; - private float humidityHigh; - private int wateringInterval; - private LocalDateTime createdAt; - private LocalDateTime updatedAt; + + private Long plantId; + private String plantName; + private String plantType; + private String plantDesc; + private String imageUrl; + private float temperatureLow; + private float temperatureHigh; + private float humidityLow; + private float humidityHigh; + private int wateringInterval; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + + } diff --git a/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantMapper.java b/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantMapper.java index a1d8d68..0bd7911 100644 --- a/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantMapper.java +++ b/leafy-backend/src/main/java/com/devwiki/leafy/dto/plant/PlantMapper.java @@ -7,20 +7,20 @@ public class PlantMapper { public static PlantDetailDto toDetailDto(Plant plant) { - PlantDetailDto plantDetailDto = new PlantDetailDto(); - plantDetailDto.setPlantId(plant.getPlantId()); - plantDetailDto.setPlantName(plant.getPlantName()); - plantDetailDto.setPlantType(plant.getPlantType()); - plantDetailDto.setPlantDesc(plant.getPlantDesc()); - plantDetailDto.setImageUrl(plant.getImageUrl()); - plantDetailDto.setTemperatureLow(plant.getTemperatureLow()); - plantDetailDto.setTemperatureHigh(plant.getTemperatureHigh()); - plantDetailDto.setHumidityLow(plant.getHumidityLow()); - plantDetailDto.setHumidityHigh(plant.getHumidityHigh()); - plantDetailDto.setWateringInterval(plant.getWateringInterval()); - plantDetailDto.setCreatedAt(plant.getCreatedAt()); - plantDetailDto.setUpdatedAt(plant.getUpdatedAt()); - return plantDetailDto; + return PlantDetailDto.builder() + .plantId(plant.getPlantId()) + .plantName(plant.getPlantName()) + .plantType(plant.getPlantType()) + .plantDesc(plant.getPlantDesc()) + .imageUrl(plant.getImageUrl()) + .temperatureLow(plant.getTemperatureLow()) + .temperatureHigh(plant.getTemperatureHigh()) + .humidityLow(plant.getHumidityLow()) + .humidityHigh(plant.getHumidityHigh()) + .wateringInterval(plant.getWateringInterval()) + .createdAt(plant.getCreatedAt()) + .updatedAt(plant.getUpdatedAt()) + .build(); } diff --git a/leafy-backend/src/test/java/com/devwiki/leafy/service/plant/PlantServiceTest.java b/leafy-backend/src/test/java/com/devwiki/leafy/service/plant/PlantServiceTest.java new file mode 100644 index 0000000..df4ef4d --- /dev/null +++ b/leafy-backend/src/test/java/com/devwiki/leafy/service/plant/PlantServiceTest.java @@ -0,0 +1,59 @@ +package com.devwiki.leafy.service.plant; + + +import static org.junit.jupiter.api.Assertions.*; + +import java.time.LocalDateTime; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.devwiki.leafy.dto.plant.PlantDetailDto; +import com.devwiki.leafy.repository.plant.PlantRepository; +import com.devwiki.leafy.repository.user.UserRepository; + +@SpringBootTest +class PlantServiceTest { + + @Autowired + private PlantService plantService; + + @Autowired + private PlantRepository plantRepository; + + @Autowired + private UserRepository userRepository; + + @BeforeEach + void setUp() { + plantRepository.deleteAll(); + userRepository.deleteAll(); + } + + @Test + @DisplayName("식물 등록") + void test1() { + // given + PlantDetailDto dto = PlantDetailDto.builder() + .plantName("몬스테라") + .plantType("관엽식물") + .plantDesc("공기정화식물") + .imageUrl("https://example.com/monstera.jpg") + .temperatureLow(20.0f) + .temperatureHigh(30.0f) + .humidityLow(40.0f) + .humidityHigh(60.0f) + .wateringInterval(7) + .createdAt(LocalDateTime.now()) + .updatedAt(LocalDateTime.now()) + .build(); + // when + plantService.addPlant(dto); + // then + assertEquals(1, plantRepository.count()); + assertEquals("몬스테라", plantRepository.findAll().get(0).getPlantName()); + } +} diff --git a/leafy-frontend/src/App.vue b/leafy-frontend/src/App.vue index 2805b0f..19aac0f 100644 --- a/leafy-frontend/src/App.vue +++ b/leafy-frontend/src/App.vue @@ -2,7 +2,7 @@

안녕하세요, {{ user.name }}님!

-

어제는 즐거운 식물 관리하셨나요?

+

오늘도 즐거운 식물 관리하세요 !!

LEAFY diff --git a/leafy-postgresql/Dockerfile b/leafy-postgresql/Dockerfile index 32a00bc..4c4c6ec 100644 --- a/leafy-postgresql/Dockerfile +++ b/leafy-postgresql/Dockerfile @@ -7,10 +7,6 @@ COPY ./init/init.sql /docker-entrypoint-initdb.d/ #postgresql.conf파일을 /etc/postgresql/postgresql.conf 로 복사, 기본 설정 파일을 덮어쓰기하여 새로운 설정 적용 COPY ./config/postgresql.conf /etc/postgresql/custom.conf -#계정정보 설정 -ENV POSTGRES_USER=myuser -ENV POSTGRES_PASSWORD=mypassword -ENV POSTGRES_DB=mydb EXPOSE 5432 diff --git a/leafy-postgresql/init/init.sql b/leafy-postgresql/init/init.sql index df6acfa..e588e74 100644 --- a/leafy-postgresql/init/init.sql +++ b/leafy-postgresql/init/init.sql @@ -56,7 +56,7 @@ CREATE TABLE plant_logs ( -- $2a$10$ke3IM6noeWfQtX6POjZHl.49gSolYbqfrSTIn8sOQubdwjP2IT94q = password789 INSERT INTO users (name, email, password, gender, birth_date) VALUES ('John', 'john123@qmail.com', '$2a$10$vYR4pPQqR/oZcUDZfXrahecEejQHY0kLkDB5s.FctPRMcEMh1PYhG', 'M', '1988-05-01'), -('Juna', 'juna123@qmail.com', '$2a$10$vYR4pPQqR/oZcUDZfXrahecEejQHY0kLkDB5s.FctPRMcEMh1PYhG', 'F', '2001-01-24'), +('Juna', 'juna123@qmail.com', '$2a$10$vYR4pPQqR/oZcUDZfXrahecEejQHY0kLkDB5s.FctPRMcEMh1PYhG', 'M', '2001-01-24'), ('Peter', 'peter789@qmail.com', '$2a$10$vYR4pPQqR/oZcUDZfXrahecEejQHY0kLkDB5s.FctPRMcEMh1PYhG', 'M', '1981-12-25'), ('Susan', 'susan321@qmail.com', '$2a$10$vYR4pPQqR/oZcUDZfXrahecEejQHY0kLkDB5s.FctPRMcEMh1PYhG', 'F', '1990-06-02'), ('David', 'david654@qmail.com', '$2a$10$vYR4pPQqR/oZcUDZfXrahecEejQHY0kLkDB5s.FctPRMcEMh1PYhG', 'M', '1992-03-11'), @@ -106,7 +106,7 @@ INSERT INTO user_plants (user_id, plant_id, plant_nickname) VALUES -- Plant_Logs 테이블에 데이터 삽입 INSERT INTO plant_logs (user_plant_id, log_date, note, watered) VALUES -(1, '2023-03-22', '관리가 어려워서 죽었습니다', false), +(1, '2023-03-22', '관리가 어려워서 죽게됐습니다.', false), (1, '2023-03-23', '새로운 아이비를 구입했습니다', true), (1, '2023-03-24', '1주일에 한 번 비료를 주기로 했습니다', false), (2, '2023-03-22', '잎이 탈색되어 있습니다', false), diff --git a/readme.md b/readme.md index 50dccb4..5b3b83a 100644 --- a/readme.md +++ b/readme.md @@ -1 +1 @@ -배포 연습 +심심하다2