제육's 휘발성 코딩
반응형

1. 개발 환경 구성

image

  • 스프링 부트 웹 애플리케이션 구성에서 서버를 배포한 후 운영, 로컬, 테스트 환경을 분리하여 개발 환경을 구성하는 것은 중요하다. 보통 운영서버에선 실제 디비를 접근하고, 로컬에선 샘플 디비, 테스트에선 메모리 디비를 사용하여 개발 환경을 나눈다.
  • 스프링 부트에선 profile을 기반으로 환경을 분리할 수 있다. 이때 네이밍은 application-xxx.properties로 반드시 지정해야하며, java -jar -Dspring.profiles.active=prod [jar 파일명] 과 같이 원하는 profile을 지정하여 실행할 수 있다.

 

application-prod.properties - 운영 환경

<code />
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://도메인:3306/subway?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password= spring.jpa.hibernate.ddl-auto=validate
  • 운영 환경에선 실제 디비에 접근하기 위한 설정을 적용한다. ddl-auto 를 validate로 설정하여, 테이블 간 연결만 확인할 수 있도록 설정한다.

 

application-local.properties - 로컬 환경

<code />
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/localdb?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=
  • 로컬 환경에 있는 데이터베이스를 사용하여 설정한다. 참고로 로컬 환경와 운영 환경은 mysql 테스트 환경은 h2 메모리를 사용하였다.

 

application-test.properties - 테스트 환경

<code />
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.h2.console.enabled=true spring.h2.console.path=/h2-console account.name=testuser
  • 테스트 환경에선 h2 데이터베이스를 사용하여 메모리 디비를 적용하였다. jdbc:h2:mem:[이름 지정] 에서 mem을 지정하면 메모리 디비로 동작한다.

2.  

3. 배포 스크립트 작성

<code />
#!/bin/bash ## 변수 설정 txtrst='\033[1;37m' # White txtred='\033[1;31m' # Red txtylw='\033[1;33m' # Yellow txtpur='\033[1;35m' # Purple txtgrn='\033[1;32m' # Green txtgra='\033[1;30m' # Gray echo -e "${txtylw}=======================================${txtrst}" echo -e "${txtgrn} << 스크립트 🧐 >>${txtrst}" echo -e "${txtylw}=======================================${txtrst}" EXECUTION_PATH=$(pwd) SHELL_SCRIPT_PATH=$(dirname $0) BRANCH=$1 PROFILE=$2 ## 조건 설정 if [[ $# -ne 2 ]] then echo -e "${txtylw}=======================================${txtrst}" echo -e "${txtgrn} << 스크립트 🧐 >>${txtrst}" echo -e "" echo -e "${txtgrn} $0 브랜치이름 ${txtred}{ prod | dev }" echo -e "${txtylw}=======================================${txtrst}" exit 0 fi function check_df() { git fetch main=$(git rev-parse $BRANCH) remote=$(git rev-parse origin/$BRANCH) if [[ $main == $remote ]]; then echo -e "[$(date)] Nothing to do!!! 😫" exit 0 else pull build find_pid run fi } function pull() { echo -e "" echo -e ">> Pull Request 🏃♂️ " git pull origin $BRANCH } function build() { echo -e "" echo -e ">> 빌드 Start 🏃♂️ " ./gradlew clean build JAR_NAME=$(basename -- build/libs/*.jar) echo -e ">> JAR NAME : ${JAR_NAME}" } function find_pid(){ PID=$(pgrep -f ${JAR_NAME}) if [[ -z "${PID}" ]] then echo ">> 실행 중인 애플리케이션이 없습니다." else echo "kill -15 ${PID}" kill -15 ${PID} sleep 5 fi } function run(){ echo ">> 애플리케이션 실행" nohup java -jar -Dspring.profiles.active=${PROFILE} build/libs/${JAR_NAME} > ~/log/nohup.out 2>&1 & } check_df
  • 다음과 같이 쉘 스크립트를 사용하여 브랜치와 profile을 파라미터로 입력 받아서 요청한 정보를 토대로 배포를 작성할 수 있다.

image

  • 아래 처럼 배포 실행 시 빌드되는 모습을 볼 수 있다.

 


REFERENCES

https://edu.nextstep.camp/

반응형
profile

제육's 휘발성 코딩

@sasca37

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요! 맞구독은 언제나 환영입니다^^