[AWS] CI/CD 배포 1 - CI(Continuous Integration)

류재성's avatar
Jun 10, 2024
[AWS] CI/CD 배포 1 - CI(Continuous Integration)
 
💡
CI/CD(Continuous Integration/Continuous Delivery 또는 Continuous Deployment)는 소프트웨어 개발 프로세스를 자동화하여 코드 변경 사항을 효율적으로 빌드, 테스트, 배포하는 방법이다.
 

지속적 통합 CI (Continuous Integration)

  • 코드 통합: 개발자가 변경한 코드를 빈번하게 통합하여 코드베이스를 업데이트한다.
  • 자동화된 빌드: 코드가 통합될 때마다 자동으로 빌드가 수행된다.
  • 자동화된 테스트: 빌드가 완료된 후 자동으로 테스트를 실행하여 코드의 품질을 검증한.
 

1. 통합 테스트

build.gradle
jar { enabled = false } ext { set('snippetsDir', file("build/generated-snippets")) } // src/docs 이하면 *.adoc 파일을 테스트시에 찾아내서 html 파일 생성해줌 tasks.named('asciidoctor') { inputs.dir snippetsDir dependsOn test } // jar에 api.html 옮기는 코드 bootJar { dependsOn asciidoctor copy { from "${asciidoctor.outputDir}" into 'src/main/resources/static/docs' // /static/docs로 복사! } }
 
notion image
 
💡
jar 파일을 하나씩만 만드는 설정 코드 java -jar ./build/libs/*.jar 로컬에서 실행할 때 파일이 여러개가 된다면 파일명까지 설정해줘야된다. 하지만 파일이 하나만 있다면 * 를 파일명이 어떻든 *를 사용해서 실행할 수 있다. 배포를 자동화할 때 스크립트를 변경하지 않고 통일이 된다.
 
 
notion image
 
notion image
 
통합 테스트가 완료되었다.
 

2. 로컬에서 빌드하기

 
./gradlew clean build
 
notion image
 
로컬에서 빌드 성공
 

3. 익셉션 정의

_core/errors/exception/MyExceptionHandler
notion image
 

4. yaml 설정

 
resources/application-dev.yml
server: servlet: encoding: charset: utf-8 force: true # url rewrite 문제 해결 session: tracking-modes: cookie port: 8080 spring: datasource: driver-class-name: org.h2.Driver url: jdbc:h2:mem:test;MODE=MySQL username: sa password: h2: console: enabled: true sql: init: data-locations: - classpath:db/data.sql jpa: hibernate: ddl-auto: create show-sql: true properties: hibernate: format_sql: true default_batch_fetch_size: 10 defer-datasource-initialization: true open-in-view: false logging: level: org.hibernate.orm.jdbc.bind: TRACE shop.mtcoding.blog: DEBUG
 
resources/application-prod.yml
server: servlet: encoding: charset: utf-8 force: true # url rewrite 문제 해결 session: tracking-modes: cookie port: 5000 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://${RDS_HOST}:3306/bodykeydb username: ${RDS_USERNAME} password: ${RDS_PASSWORD} jpa: hibernate: ddl-auto: none properties: hibernate: default_batch_fetch_size: 10 dialect: org.hibernate.dialect.MySQL8Dialect open-in-view: false logging: level: org.hibernate.orm.jdbc.bind: INFO shop.mtcoding.blog: INFO
 
💡
github action 테스트는 dev 모드, 실제 배포는 prod 모드로 한다.
 
resources/application.yml
spring: profiles: active: - dev
 

5. git hub Action 설정

 
notion image
 
깃헙에서 action - publish java package with gradle 설정
 
notion image
 
.github/workflows/deply.yml
name: bodykey on: push: branches: - master # https://github.com/actions/setup-java # actions/setup-java@v2는 사용자 정의 배포를 지원하고 Zulu OpenJDK, Eclipse Temurin 및 Adopt OpenJDK를 기본적으로 지원합니다. v1은 Zulu OpenJDK만 지원합니다. jobs: build: // 우분투 환경에서 빌드 runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 //Java Development Kit(JDK) 21을 설정합니다. temurin 배포판을 사용합니다. - name: Set up JDK 21 uses: actions/setup-java@v4 with: java-version: 21 distribution: 'temurin' // 권한 설정 - name: Permission run: chmod +x ./gradlew // 빌드 - name: Build with Gradle run: ./gradlew clean build
 
notion image
 
이 상태로 git hub 에 푸쉬하면 action 에서 확인할 수 있다. 초록 버튼이 뜬다면 CI 가 완료되었다.
 
 
 
 
 
Share article
RSSPowered by inblog