diff --git a/.github/workflows/be-cd.yml b/.github/workflows/be-cd.yml new file mode 100644 index 0000000..a490f33 --- /dev/null +++ b/.github/workflows/be-cd.yml @@ -0,0 +1,70 @@ +name: Uniro-server CD + +on: + push: + branches: + - be + +jobs: + uniro-ci: + name: Build & Push Docker Image + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'temurin' + + - name: Cache Gradle packages + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} + + - name: Create application.properties from secret + run: | + echo "${{ secrets.BE_SPRING_APPLICATION_SECRET }}" > ./uniro_backend/src/main/resources/application.properties + shell: bash + + - name: Build Spring Boot Application + run: | + cd uniro_backend + ./gradlew clean build -x test + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Docker Hub Login + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_LOGIN_USERNAME }} + password: ${{ secrets.DOCKERHUB_LOGIN_ACCESSTOKEN }} + + - name: Build & Push Multi-Arch Docker Image + run: | + cd uniro_backend + docker buildx create --use + docker buildx build --platform linux/amd64,linux/arm64 -t uniro5th/uniro-docker-repo:develop --build-arg SPRING_PROFILE=dev --push . + + deploy-run: + name: Deploy to Server + needs: uniro-ci + runs-on: ubuntu-latest + steps: + - name: Run Docker Container on Server + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.BE_SERVER_IP }} + username: ${{ secrets.BE_SERVER_USER }} + key: ${{ secrets.BE_SERVER_KEY }} + script: | + cd ~/myapp + chmod +x ./deploy.sh + ./deploy.sh diff --git a/.github/workflows/be-ci.yml b/.github/workflows/be-ci.yml new file mode 100644 index 0000000..c724bfc --- /dev/null +++ b/.github/workflows/be-ci.yml @@ -0,0 +1,42 @@ +name: Uniro-server CI + +on: + pull_request: + branches: + - be + +jobs: + build-springboot: + name: Build and analyze (SpringBoot) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: 17 + distribution: 'temurin' + + - name: Cache Gradle packages + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} + + - name: Create application.properties from secret + run: | + echo "${{ secrets.BE_SPRING_APPLICATION_SECRET }}" > ./uniro_backend/src/main/resources/application.properties + shell: bash + + - name: Debug application.properties + run: cat ./uniro_backend/src/main/resources/application.properties + + - name: Build and analyze (SpringBoot) + run: | + cd uniro_backend + ./gradlew clean build -x test \ No newline at end of file diff --git a/uniro_backend/Dockerfile b/uniro_backend/Dockerfile new file mode 100644 index 0000000..e8f6db7 --- /dev/null +++ b/uniro_backend/Dockerfile @@ -0,0 +1,10 @@ +FROM openjdk:17 + +ARG JAR_FILE=./build/libs/*.jar +ARG SPRING_PROFILE + +COPY ${JAR_FILE} uniro-server.jar + +ENV SPRING_PROFILE=${SPRING_PROFILE} + +ENTRYPOINT ["java", "-Duser.timezone=Asia/Seoul","-Dspring.profiles.active=${SPRING_PROFILE}" ,"-jar" ,"uniro-server.jar"] \ No newline at end of file diff --git a/uniro_backend/build.gradle b/uniro_backend/build.gradle index cbe08a2..f5f78cc 100644 --- a/uniro_backend/build.gradle +++ b/uniro_backend/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.4.2' + id 'org.springframework.boot' version '3.3.8' id 'io.spring.dependency-management' version '1.1.7' id 'org.hibernate.orm' version '6.3.1.Final' } @@ -65,6 +65,9 @@ dependencies { //webClient implementation 'org.springframework.boot:spring-boot-starter-webflux' + + // actuator + implementation 'org.springframework.boot:spring-boot-starter-actuator' } tasks.named('test') { diff --git a/uniro_backend/docker-compose.yml b/uniro_backend/docker-compose.yml new file mode 100644 index 0000000..319bb0f --- /dev/null +++ b/uniro_backend/docker-compose.yml @@ -0,0 +1,28 @@ +version: '3.8' + +services: + spring-green: + image: uniro5th/uniro-docker-repo:develop + container_name: spring-green + environment: + - SPRING_PROFILES_ACTIVE=dev + restart: always + ports: + - "8080:8080" + networks: + - my_network1 + + spring-blue: + image: uniro5th/uniro-docker-repo:develop + container_name: spring-blue + environment: + - SPRING_PROFILES_ACTIVE=dev + restart: always + ports: + - "8081:8080" + networks: + - my_network1 + +networks: + my_network1: + external: true diff --git a/uniro_backend/src/main/resources/application-dev.yml b/uniro_backend/src/main/resources/application-dev.yml new file mode 100644 index 0000000..56670dd --- /dev/null +++ b/uniro_backend/src/main/resources/application-dev.yml @@ -0,0 +1,41 @@ +spring: + config: + import: application.properties + datasource: + url: ${DB_URL} + username: ${DB_USER} + password: ${DB_PASSWORD} + driver-class-name: com.mysql.cj.jdbc.Driver + jpa: + hibernate: + ddl-auto: + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL8Dialect + format_sql: true + show_sql: true + open-in-view: false + defer-datasource-initialization: true + sql: + init: + schema-locations: classpath:h2gis-setting.sql + h2: + console: + enabled: true + path: /h2-console +map: + api: + key: ${google.api.key} + +management: + endpoints: + jmx: + exposure: + exclude: "*" + web: + exposure: + include: info, health, prometheus + prometheus: + metrics: + export: + enabled: true \ No newline at end of file diff --git a/uniro_backend/src/main/resources/application-local.yml b/uniro_backend/src/main/resources/application-local.yml index 8ca1b45..941128a 100644 --- a/uniro_backend/src/main/resources/application-local.yml +++ b/uniro_backend/src/main/resources/application-local.yml @@ -23,4 +23,17 @@ spring: path: /h2-console map: api: - key: ${google.api.key} \ No newline at end of file + key: ${google.api.key} + +management: + endpoints: + jmx: + exposure: + exclude: "*" + web: + exposure: + include: info, health, prometheus + prometheus: + metrics: + export: + enabled: true \ No newline at end of file diff --git a/uniro_backend/src/test/resources/application-test.yml b/uniro_backend/src/main/resources/application-test.yml similarity index 92% rename from uniro_backend/src/test/resources/application-test.yml rename to uniro_backend/src/main/resources/application-test.yml index 290adb9..944a3b1 100644 --- a/uniro_backend/src/test/resources/application-test.yml +++ b/uniro_backend/src/main/resources/application-test.yml @@ -1,4 +1,8 @@ spring: + config: + activate: + on-profile: test + datasource: url: jdbc:h2:mem:uniro-local-db;DATABASE_TO_UPPER=FALSE;mode=mysql driverClassName: org.h2.Driver diff --git a/uniro_backend/src/test/java/com/softeer5/uniro_backend/node/client/MapClientImplTest.java b/uniro_backend/src/test/java/com/softeer5/uniro_backend/node/client/MapClientImplTest.java index 98d17b9..b887b25 100644 --- a/uniro_backend/src/test/java/com/softeer5/uniro_backend/node/client/MapClientImplTest.java +++ b/uniro_backend/src/test/java/com/softeer5/uniro_backend/node/client/MapClientImplTest.java @@ -10,12 +10,14 @@ import org.locationtech.jts.geom.Point; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest +@ActiveProfiles("test") public class MapClientImplTest { @Autowired private MapClientImpl mapClientImpl;