Skip to content

Commit

Permalink
Merge pull request #49 from Mu-necting/setting/#48
Browse files Browse the repository at this point in the history
CI/CD 구축 완료
  • Loading branch information
mingmingmon authored Oct 28, 2024
2 parents cf71535 + c53b99a commit 04185cb
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 44 deletions.
78 changes: 78 additions & 0 deletions .github/workflows/develop-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Develop Branch CI

on:
push:
branches: [ "develop" ]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
# JDK 설정
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

# 환경 변수 파일 생성
- name: Set environment values for main application
run: |
cd ./src/main/resources
touch ./env.properties
echo "${{ secrets.ENV }}" > ./env.properties
shell: bash

- name: Set environment values for test
run: |
cd ./src/test/resources
touch ./env.properties
echo "${{ secrets.TEST_ENV }}" > ./env.properties
shell: bash

# Gradle에 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x gradlew

# Gradle test
- name: Test with Gradle
run: ./gradlew test

# Gradle build
- name: Build Gradle
run: ./gradlew build

# 압축 파일 만들기
- name: Make zip file
run: |
mkdir develop-deploy
cp ./src/main/resources/env.properties ./develop-deploy/.env
cp ./docker-compose.blue.yml ./develop-deploy/
cp ./docker-compose.green.yml ./develop-deploy/
cp ./appspec.yml ./develop-deploy/
cp ./Dockerfile ./develop-deploy/
cp ./scripts/*.sh ./develop-deploy/
cp ./build/libs/*.jar ./develop-deploy/
zip -r -qq -j ./munecting.zip ./develop-deploy
# AWS 접속
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

# S3에 저장
- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./munecting.zip s3://code-deploy-munecting

# Code Deploy 실행
- name: Code Deploy
run: aws deploy create-deployment --application-name munecting-deploy
--deployment-config-name CodeDeployDefault.OneAtATime
--deployment-group-name munecting-deploy-group
--s3-location bucket=code-deploy-munecting,bundleType=zip,key=munecting.zip
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
HELP.md
env.properties
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:21
WORKDIR /munecting
COPY Munecting-server-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
19 changes: 19 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/app
overwrite: yes

permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu
mode: 777

hooks:
ApplicationStart:
- location: develop-deploy.sh
timeout: 300
runas: ubuntu
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ dependencies {
implementation 'org.json:json:20240303'
implementation 'org.springframework.boot:spring-boot-starter-validation:2.5.2'

// Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'

Expand Down Expand Up @@ -64,7 +67,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// redisson
implementation 'org.redisson:redisson-spring-boot-starter:3.36.0'
implementation 'org.redisson:redisson-spring-boot-starter:3.37.0'

// Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Etc
runtimeOnly 'org.postgresql:postgresql'
Expand Down
14 changes: 14 additions & 0 deletions docker-compose.blue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#blue
version: '3'
services:
munecting-develop-api:
build: .
ports:
- "8081:8080"
container_name: spring-blue
networks:
- munecting-network

networks:
munecting-network:
external: true
14 changes: 14 additions & 0 deletions docker-compose.green.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#green
version: '3'
services:
munecting-develop-api:
build: .
ports:
- "8082:8080"
container_name: spring-green
networks:
- munecting-network

networks:
munecting-network:
external: true
12 changes: 12 additions & 0 deletions docker-compose.redis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
redis:
image: redis:latest
container_name: redis
networks:
- munecting-server
ports:
- "6379:6379"

networks:
munecting-network:
external: true
79 changes: 79 additions & 0 deletions scripts/develop-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

cd /home/ubuntu/app

# Redis 컨테이너 실행 여부 확인
REDIS_CONTAINER_STATUS=$(docker ps | grep redis)
if [ -z "$REDIS_CONTAINER_STATUS" ]; then
echo "Redis 컨테이너가 실행 중이지 않습니다"
echo ">>> Pulling Redis image"
docker compose -f docker-compose.redis.yml pull redis
echo ">>> Starting Redis container"
docker compose -f docker-compose.redis.yml up -d redis
else
echo "Redis 컨테이너가 이미 실행 중입니다."
fi


DOCKER_APP_NAME=spring

# blue 컨테이너 실행 여부 확인
BLUE_CONTAINER_RUNNING=$(docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml ps -q | xargs docker inspect -f '{{.State.Running}}')

# 컨테이너 스위칭
if [ "$BLUE_CONTAINER_RUNNING" != "true" ]; then
echo "[blue up]"
docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml up -d --build

ACTIVE_ENV="blue"
STANDBY_ENV="green"
ACTIVE_PORT="8081"

else
echo "[green up]"
docker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml up -d --build

ACTIVE_ENV="green"
STANDBY_ENV="blue"
ACTIVE_PORT="8082"

fi


# 헬스체크
MAX_RETRIES=30
RETRY_INTERVAL=2
HEALTH_CHECK_URL="http://localhost:$ACTIVE_PORT/actuator/health"
ACTIVE_HEALTH_CHECK_PASSED=false

echo "Waiting for application to be ready..."
for i in $(seq 1 $MAX_RETRIES); do
if curl -s "$HEALTH_CHECK_URL" | grep -q "UP"; then
echo "[Application is ready]"
ACTIVE_HEALTH_CHECK_PASSED=true
break

fi
echo "Waiting... ($i/$MAX_RETRIES)"
sleep $RETRY_INTERVAL

done

# 새로운 컨테이너가 제대로 떴는지 확인
if [ "$ACTIVE_HEALTH_CHECK_PASSED" = "true" ]; then
# 이전 컨테이너 종료
docker-compose -p ${DOCKER_APP_NAME}-${STANDBY_ENV} -f docker-compose.${STANDBY_ENV}.yml down
docker image prune -af
echo "[Down] $STANDBY_ENV Down"

else
echo "Health check failed after $MAX_RETRIES attempts"
# 새 컨테이너 종료
docker-compose -p ${DOCKER_APP_NAME}-${ACTIVE_ENV} -f docker-compose.${ACTIVE_ENV}.yml down
# 디스코드 알림
sudo ./discord_${ACTIVE_ENV}.sh

fi



Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Map<String, TrackResponseDto> convertToLikeTrackResponseDtoMap(List<Track
return tracks.stream()
.collect(Collectors.toMap(
Track::getId,
this::converToLikeTrackResponseDto
this::convertToLikeTrackResponseDto
));
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/munecting/api/global/config/PropertyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.munecting.api.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources({
@PropertySource("classpath:env.properties")
})
public class PropertyConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class SecurityConfig {
private static final String[] ALLOWED_URL = {
"/api/auth/**",
"/error/**",
"/reissue",
"/v2/api-docs",
"/swagger-resources",
"/swagger-resources/**",
Expand All @@ -48,9 +47,9 @@ public class SecurityConfig {
"/v3/api-docs/**",
"/swagger-ui/**",
"/css/**","/images/**","/js/**","/favicon.ico",
"/musics/**",
"/address/**"

"/api/musics/**",
"/api/address/**",
"/actuator/health"
};

@Bean
Expand Down
25 changes: 5 additions & 20 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ spring :
timezone: Asia/Seoul

datasource:
url: jdbc:${DATASOURCE}://${HOST}:${PORT}/${TABLE}
username: ${DATASOURCE_USERNAME}
password: ${DATASOURCE_PASSWORD}
url: jdbc:${DATASOURCE}://${DB_HOST}:${DB_PORT}/${DB_NAME}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: org.postgresql.Driver
hikari:
initialization-fail-timeout: 3600
Expand All @@ -31,20 +31,7 @@ spring :
host: ${REDIS_HOST}
port: 6379
# password: 1234
mail:
debug: true
host: smtp.naver.com
port: 465
username: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}
properties:
mail:
smtp:
auth: true
starttls:
enable: true
ssl:
enable: true


security:
cors:
Expand All @@ -70,9 +57,7 @@ spring :
google:
client-id: ${GOOGLE_CLIENT_ID}

naver:
id: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}


spotify:
client-id: ${CLIENT_ID}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
package com.munecting.api;
import org.junit.jupiter.api.Test;
Expand All @@ -13,3 +14,4 @@ void contextLoads() {
}
}
*/
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
package com.munecting.api.domain.like.service;
import com.munecting.api.domain.like.dao.LikeRepository;
Expand Down Expand Up @@ -80,3 +81,4 @@ void init() {
assertEquals(1, actualTrackCount);
}
}
*/
18 changes: 0 additions & 18 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ spring :
host: ${REDIS_HOST}
port: 6379
# password: 1234
mail:
debug: true
host: smtp.naver.com
port: 465
username: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}
properties:
mail:
smtp:
auth: true
starttls:
enable: true
ssl:
enable: true

security:
cors:
Expand All @@ -81,10 +67,6 @@ spring :
google:
client-id: ${GOOGLE_CLIENT_ID}

naver:
id: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}

spotify:
client-id: ${CLIENT_ID}
client-secret: ${CLIENT_SECRET}
Expand Down

0 comments on commit 04185cb

Please sign in to comment.