Skip to content

Commit

Permalink
[merge]: 7~8주차 진행사항 develop에 머지 (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanguk01 authored Oct 25, 2024
2 parents a9b8aeb + 2ab1f47 commit 10018c2
Show file tree
Hide file tree
Showing 51 changed files with 1,874 additions and 74 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: CD (자동배포)

on:
push:
branches:
- develop
jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Java 21
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'

- name: Set up Docker
uses: docker/setup-buildx-action@v2

- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin

- name: Set Image Tag
id: image_tag
run: echo "IMAGE_TAG=$(date +'%Y-%m-%d_%H-%M-%S')-$(echo ${{ github.sha }} | cut -c1-8)" >> $GITHUB_ENV

- name: Decode env.properties from GitHub Secrets
run: |
echo "${{ secrets.ENV_FILE }}" | base64 --decode > ./env.properties
- name: Transfer env.properties to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
source: "./env.properties"
target: "/home/ubuntu/"

- name: Build and Push Docker image
run: docker buildx build --push --platform linux/amd64 -t kimsongmok/splanet:${{ env.IMAGE_TAG }} .

- name: Deploy to EC2
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker pull kimsongmok/splanet:${{ env.IMAGE_TAG }}
sudo docker stop splanet || true
sudo docker rm splanet || true
sudo docker network inspect splanet >/dev/null 2>&1 || sudo docker network create splanet
sudo docker run -d --name splanet \
--network splanet \
--env-file /home/ubuntu/env.properties \
-p 80:8080 --restart unless-stopped kimsongmok/splanet:${{ env.IMAGE_TAG }}
- name: Check Docker container status
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
sudo docker ps -a
sudo docker logs splanet
- name: Clean up old Docker images
uses: appleboy/[email protected]
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
script: |
docker image ls --format "{{.ID}} {{.Repository}}:{{.Tag}}" | grep 'kimsongmok/splanet' | tail -n +4 | awk '{print $1}' | xargs docker rmi -f
sudo docker system prune -f
85 changes: 85 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: CI (빌드 및 테스트)

on:
pull_request:
branches:
- master
- develop
- 'weekly/**'

jobs:
build-and-test:
runs-on: ubuntu-22.04

services:
mysql:
image: mysql:8.0
env:
MYSQL_DATABASE: ${{ secrets.MYSQL_DATABASE }}
MYSQL_USER: ${{ secrets.MYSQL_USER }}
MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
MYSQL_ROOT_PASSWORD: ${{ secrets.MYSQL_ROOT_PASSWORD }}
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h localhost"
--health-interval=10s
--health-timeout=5s
--health-retries=5
redis:
image: redis
ports:
- 6379:6379

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Cache Gradle dependencies
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
gradle-${{ runner.os }}-
- name: Decode env.properties from GitHub Secrets
run: |
echo "${{ secrets.ENV_FILE }}" | base64 --decode > ./src/main/resources/env.properties
- name: Set environment variables from env.properties
run: |
set -o allexport
source ./src/main/resources/env.properties
set +o allexport
- name: Set up JDK 21
uses: actions/setup-java@v2
with:
java-version: '21'
distribution: 'temurin'

- name: Wait for MySQL to be ready
run: |
for i in {30..0}; do
if docker exec $(docker ps -q --filter name=mysql) mysqladmin ping -h localhost; then
echo "MySQL is ready"
break
fi
echo "Waiting for MySQL..."
sleep 1
done
if [ $i -eq 0 ]; then
echo "MySQL did not become ready in time"
docker logs $(docker ps -q --filter name=mysql)
exit 1
fi
- name: Build with Gradle
run: ./gradlew build

- name: Run Tests
run: ./gradlew test
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ COPY build.gradle .
COPY settings.gradle .
COPY src src
RUN chmod +x ./gradlew
RUN ./gradlew bootJar

# Gradle 빌드에서 프로필을 지정하여 실행
RUN ./gradlew bootJar -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE}

# 런타임 단계
FROM eclipse-temurin:21
COPY --from=builder build/libs/*.jar app.jar

# 런타임에서도 동일하게 환경 변수 사용
ENV SPRING_PROFILES_ACTIVE=prod


ENTRYPOINT ["java", "-jar", "/app.jar"]
VOLUME /tmp
VOLUME /tmp
57 changes: 56 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.3.3'
id 'io.spring.dependency-management' version '1.1.6'
id 'com.google.protobuf' version '0.9.4'

}

group = 'com.splanet'
Expand All @@ -21,6 +23,12 @@ configurations {

repositories {
mavenCentral()
maven {
url 'https://repo.spring.io/milestone'
}
maven {
url 'https://repo.spring.io/snapshot'
}
}

dependencies {
Expand All @@ -32,12 +40,25 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
// implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'org.hibernate.validator:hibernate-validator:8.0.0.Final'
implementation 'jakarta.validation:jakarta.validation-api:3.0.2'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
// gRPC 및 Protocol Buffers 의존성
implementation 'io.grpc:grpc-netty-shaded:1.56.1'
implementation 'io.grpc:grpc-protobuf:1.56.1'
implementation 'io.grpc:grpc-stub:1.56.1'
implementation 'com.google.protobuf:protobuf-java:3.23.4'

// gRPC 관련 필요한 의존성
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'com.google.code.gson:gson:2.8.9'


compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand All @@ -46,8 +67,42 @@ dependencies {
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

//gpt
// implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'com.fasterxml.jackson.core:jackson-databind'
// implementation 'org.springframework.boot:spring-boot-starter'
implementation platform("org.springframework.ai:spring-ai-bom:0.8.0")
implementation 'org.springframework.ai:spring-ai-openai'


}

tasks.named('test') {
useJUnitPlatform()
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.23.4'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.66.0'
}
}
generateProtoTasks {
all().forEach { task ->
task.plugins {
grpc {}
}
}
}
}

sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/java', 'build/generated/source/proto/main/grpc'
}
}
}
78 changes: 39 additions & 39 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
version: '3'

services:
mysql:
container_name: mysql
image: mysql:8.0
restart: always
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
volumes:
- ./splanet-db/mysql:/var/lib/mysql
ports:
- 3306:3306
networks:
- splanet

redis:
container_name: redis
image: redis
ports:
- 6379:6379
networks:
- splanet


# 개발할때는 주석처리하여 로컬로 개발합니다.
# springboot:
# container_name: springboot_splanet
# build:
# context: .
# dockerfile: Dockerfile
# mysql:
# container_name: mysql
# image: mysql:8.0
# restart: always
# depends_on:
# - mysql
# ports:
# - 8080:8080
# environment:
# SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL}
# SPRING_DATASOURCE_USERNAME: ${SPRING_DATASOURCE_USERNAME}
# SPRING_DATASOURCE_PASSWORD: ${SPRING_DATASOURCE_PASSWORD}
# SPRING_PROFILES_ACTIVE: ${SPRING_PROFILES_ACTIVE}
# MYSQL_USER: ${MYSQL_USER}
# MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
# MYSQL_PASSWORD: ${MYSQL_PASSWORD}
# MYSQL_DATABASE: ${MYSQL_DATABASE}
# volumes:
# - ./splanet-db/mysql:/var/lib/mysql
# ports:
# - 3306:3306
# networks:
# - splanet
#
# redis:
# container_name: redis
# image: redis
# ports:
# - 6379:6379
# networks:
# - splanet


# 개발할때는 주석처리하여 로컬로 개발합니다.
springboot:
container_name: springboot_splanet
build:
context: .
dockerfile: Dockerfile
restart: always
depends_on:
- mysql
ports:
- 8080:8080
environment:
SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL}
SPRING_DATASOURCE_USERNAME: ${SPRING_DATASOURCE_USERNAME}
SPRING_DATASOURCE_PASSWORD: ${SPRING_DATASOURCE_PASSWORD}
SPRING_PROFILES_ACTIVE: ${SPRING_PROFILES_ACTIVE}
networks:
- splanet

networks:
splanet:
driver: bridge
16 changes: 16 additions & 0 deletions src/main/java/com/splanet/splanet/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.splanet.splanet.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) { //인터페이스 WebMvcConfigurer 상속
registry.addMapping("/**") //모든 경로를 허용해줄것이므로
.allowedOrigins("*") //리소스 공유 허락할 origin 지정
.allowedMethods("*"); //모든 메소드를 허용
}
}
Loading

0 comments on commit 10018c2

Please sign in to comment.