CI / CD 구현 과정

알쓸코딩·2024년 1월 21일
0

트러블 슈팅

목록 보기
5/13

Dockerfile

FROM openjdk:17 AS builder
COPY gradlew .
COPY gradle gradle
COPY build.gradle .
COPY settings.gradle .
COPY src src
RUN chmod +x ./gradlew
RUN microdnf install findutils
RUN ./gradlew build -x test

# base-image
FROM openjdk:17
# build file path
RUN mkdir /opt/app
# copy jar file to container
COPY --from=builder build/libs/*.jar /opt/app/spring-boot-application.jar
EXPOSE 8080
# run jar file
ENTRYPOINT ["java","-jar","/opt/app/spring-boot-application.jar"]

ci.yml

name: Continuous Integration

on:
  pull_request:
    branches: [ "develop" ]
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
          - info
          - warning
          - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
      environment:
        description: 'Environment to run tests against'
        type: environment
        required: false

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-20.04

    steps:
      # 1. Compare branch 코드 내려 받기
      - name: Checkout PR
        uses: actions/checkout@v3

      # 2. 자바 환경 설정
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'

      # 3. Spring 환경 변수 설정
      - name: Set up application.yml
        run: |
          cd ./src/main/resources
          touch ./application.yml
          echo "${{ secrets.APPLICATION }}" > ./application.yml
        shell: bash

      # 4. 테스트를 위한 MySQL 설정
      - name: Setup MySQL
        uses: mirromutth/mysql-action@v1.1
        with:
          mysql database: ${{ secrets.MYSQL_DATABASE }}
          mysql user: ${{ secrets.MYSQL_USERNAME }}
          mysql password: ${{ secrets.MYSQL_PASSWORD }}

      # 5. 테스트를 위한 빌드 실행
      - name: Build with Gradle
        uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25
        with:
          arguments: build

cd.yml

name: Continuous Deployment

on:
  push:
    branches: [ "develop" ]
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
          - info
          - warning
          - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
      environment:
        description: 'Environment to run tests against'
        type: environment
        required: false

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-20.04

    steps:
      # 1. dev branch 코드 내려 받기
      - name: Checkout PR
        uses: actions/checkout@v3
        with:
          ref: ${{ github.event.push.base_ref }}

      # 2. 자바 환경 설정
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
      
      # 3. Spring 환경 변수 설정
      - name: Set up application.yml
        run: |
          cd ./src/main/resources
          touch ./application.yml
          echo "${{ secrets.APPLICATION }}" > ./application.yml
        shell: bash

      # 4. Docker 이미지 build 및 push
      - name: docker build and push
        run: |
          docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
          docker build -t minjeongs/oot-was:latest .
          docker push minjeongs/oot-was:latest

      # 5. SSH ACTION을 통한 Run-Command (Docker 이미지 pull 후 docker-compose를 통한 실행)
      - name: deploy
        uses: appleboy/ssh-action@v0.1.5
        with:
          host: ${{ secrets.SERVER_HOST }}
          port: ${{ secrets.SERVER_SSH_PORT }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          script: |
            docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
            docker system prune -a -f
            docker pull minjeongs/oot-was:latest
            docker-compose up -d

Actions Secret 설정

  • APPLICATION : application.yml
  • DOCKER_USERNAME : 비밀
  • DOCKER_PASSWORD : 비밀
  • SERVER_HOST : 223.130.137.95
  • SERVER_SSH_PORT : 22
  • SERVER_USERNAME : root
  • SERVER_PASSWORD : 비밀
  • MYSQL_DATABASE : oot
  • MYSQL_USERNAME : root
  • MYSQL_PASSWORD : 비밀

docker-compose.yml

version: '3'

services:
  database:
    container_name: mysql_db
    image: mysql/mysql-server:8.0.30
    environment:
      MYSQL_DATABASE: sportspie
      MYSQL_ROOT_HOST: '%'
      MYSQL_ROOT_PASSWORD: abc123
      TZ: 'Asia/Seoul'
    ports:
      - "3306:3306"
    volumes:
      - ./mysql/conf.d:/etc/mysql/conf.d
    command:
      - "mysqld"
      - "--character-set-server=utf8mb4"
      - "--collation-server=utf8mb4_unicode_ci"

  was:
    container_name: sportspie-was
    image: jinlee1703/sportspie-was
    ports:
      - "8080:8080"
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://110.165.17.35:3306/sportspie
      SPRING_DATASOURCE_USERNAME: "root"
      SPRING_DATASOURCE_PASSWORD: "abc123"
    depends_on:
      - database

  web:
    container_name: sportspie-web
    image: jinlee1703/sportspie-web
    command: npm start
    ports:
      - "3000:3000"
    depends_on:
      - was

  nginx:
    image: nginx:1.21.5-alpine
    ports:
      - 80:80
    volumes:
      - ./proxy/nginx.conf:/etc/nginx/nginx.conf
    container_name: proxy
    depends_on:
      - web
profile
알면 쓸데있는 코딩 모음!

0개의 댓글