[commenthat CI/CD] 1. Github Actions 설정

강다빈·2026년 6월 1일

DevOps

목록 보기
4/8

1. commenthat 프로젝트 구조

MSA 구조로 설계되어 4가지 모듈이 있으며, 이 4가지 모듈을 CICD 파이프라인을 통해 자동으로 k3s로 배포하도록 할 예정

예상 파이프라인은 아래와 같음

더 자세한 설명: https://github.com/cookie-k3/commentHAT-showcase

2. Github Actions 설정

github 해당 레포지토리 - Actions - New workflow - set up a workflow yourself 클릭

ViewServiceFront

.github/workflows/ci-cd.yml

name: React CI/CD Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}
  NODE_VERSION: '18'

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    # main 브랜치 푸시 시에만 실행
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    permissions:
      contents: read
      packages: write
    
    outputs:
      image-tag: ${{ steps.meta.outputs.tags }}
      image-digest: ${{ steps.build.outputs.digest }}
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    
    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci  # package.json의 모든 의존성이 자동으로 설치됨
    
    - name: Build React app
      run: npm run build
      env:
        CI: false  # Warning을 에러로 처리하지 않음
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=sha,prefix=main-,format=short
          type=raw,value=latest,enable={{is_default_branch}}
    
    - name: Build and push Docker image
      id: build
      uses: docker/build-push-action@v5
      with:
        context: .
        platforms: linux/amd64
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
        build-args: |
          BUILDKIT_INLINE_CACHE=1

  update-manifest:
    needs: build-and-push
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        fetch-depth: 0
    
    - name: Update Kubernetes manifests
      run: |
        # 새로운 이미지 태그 추출
        NEW_TAG=$(echo "${{ needs.build-and-push.outputs.image-tag }}" | grep -E "main-[a-f0-9]{7}" | head -1 | cut -d':' -f2)
        echo "New tag: $NEW_TAG"
        
        # kustomization.yaml에서 이미지 태그 업데이트
        sed -i "s|newTag: .*|newTag: ${NEW_TAG}|g" k8s/kustomization.yaml
        
        # 변경사항 확인
        git diff k8s/kustomization.yaml
        
        # 변경사항 커밋 및 푸시
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add k8s/kustomization.yaml
        
        if git diff --staged --quiet; then
          echo "No changes to commit"
        else
          git commit -m "Deploy: Update image tag to ${NEW_TAG}"
          git push
        fi

ViewServiceBack

.github/workflows/ci-cd.yml

name: Spring Boot CI/CD Pipeline

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: cookie-k3/server-webBack  # 실제 이미지명과 일치
  JAVA_VERSION: '17'

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    # main 브랜치 푸시/머지 시에만 실행
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    permissions:
      contents: read
      packages: write
    
    outputs:
      image-tag: ${{ steps.meta.outputs.tags }}
      image-digest: ${{ steps.build.outputs.digest }}
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: ${{ env.JAVA_VERSION }}
        distribution: 'temurin'
    
    - name: Cache Gradle packages
      uses: actions/cache@v4
      with:
        path: |
          ~/.gradle/caches
          ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
        restore-keys: |
          ${{ runner.os }}-gradle-
    
    - name: Make gradlew executable
      run: chmod +x gradlew
    
    - name: Build with Gradle (JAR 파일 생성)
      run: |
        ./gradlew build -x test --no-daemon
        
        # JAR 파일이 올바른 위치에 있는지 확인
        ls -la build/libs/
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=sha,prefix=main-,format=short
          type=raw,value=latest,enable={{is_default_branch}}
    
    - name: Build and push Docker image
      id: build
      uses: docker/build-push-action@v5
      with:
        context: .
        platforms: linux/amd64  # ARM64 제외 (안정성)
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
        build-args: |
          BUILDKIT_INLINE_CACHE=1

  update-manifest:
    needs: build-and-push
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        fetch-depth: 0
    
    - name: Update Kubernetes manifests
      run: |
        # 새로운 이미지 태그 추출
        NEW_TAG=$(echo "${{ needs.build-and-push.outputs.image-tag }}" | grep -E "main-[a-f0-9]{7}" | head -1 | cut -d':' -f2)
        echo "New tag: $NEW_TAG"
        
        # kustomization.yaml에서 이미지 태그 업데이트
        sed -i "s|newTag: .*|newTag: ${NEW_TAG}|g" k8s/kustomization.yaml
        
        # 변경사항 확인
        git diff k8s/kustomization.yaml
        
        # Git 설정 및 커밋
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add k8s/kustomization.yaml
        
        if git diff --staged --quiet; then
          echo "No changes to commit"
        else
          git commit -m "Deploy: Update Spring Boot image tag to ${NEW_TAG}"
          git push
        fi

AnalysisService

.github/workflows/ci-cd.yml

name: Python Analysis Service CI/CD

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: cookie-k3/server-analysis
  PYTHON_VERSION: '3.9'

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    # main 브랜치 푸시 시에만 실행
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    permissions:
      contents: read
      packages: write
    
    outputs:
      image-tag: ${{ steps.meta.outputs.tags }}
      image-digest: ${{ steps.build.outputs.digest }}
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    
    - name: Verify required files
      run: |
        echo "프로젝트 파일 확인..."
        ls -la
        echo "requirements.txt 확인:"
        head -10 requirements.txt || echo "requirements.txt 없음"
        echo "run_project.sh 확인:"
        ls -la run_project.sh || echo "run_project.sh 없음"
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=sha,prefix=main-,format=short
          type=raw,value=latest,enable={{is_default_branch}}
          type=raw,value=miniserver  # 기존 태그 유지
    
    - name: Build and push Docker image
      id: build
      uses: docker/build-push-action@v5
      with:
        context: .
        platforms: linux/amd64  # Python + PyTorch는 ARM64 복잡함
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
        build-args: |
          BUILDKIT_INLINE_CACHE=1

  update-manifest:
    needs: build-and-push
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: write
      actions: read
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        fetch-depth: 0
    
    - name: Update Kubernetes manifests
      run: |
        # 새로운 이미지 태그 추출
        NEW_TAG=$(echo "${{ needs.build-and-push.outputs.image-tag }}" | grep -E "main-[a-f0-9]{7}" | head -1 | cut -d':' -f2)
        echo "New tag: $NEW_TAG"
        
        # kustomization.yaml에서 이미지 태그 업데이트
        sed -i "s|newTag: .*|newTag: ${NEW_TAG}|g" k8s/kustomization.yaml
        
        # 변경사항 확인
        git diff k8s/kustomization.yaml
        
        # Git 설정 및 커밋
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add k8s/kustomization.yaml
        
        if git diff --staged --quiet; then
          echo "No changes to commit"
        else
          git commit -m "Deploy: Update Python analysis service image tag to ${NEW_TAG}"
          git push
        fi

AutoProcessor

.github/workflows/ci-cd.yml

name: Spring Boot CI/CD Pipeline (No Test)

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}
  JAVA_VERSION: '17'

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    # 테스트 job 제거로 인해 조건 단순화
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    permissions:
      contents: read
      packages: write
    
    outputs:
      image-tag: ${{ steps.meta.outputs.tags }}
      image-digest: ${{ steps.build.outputs.digest }}
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    
    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: ${{ env.JAVA_VERSION }}
        distribution: 'temurin'
    
    - name: Cache Gradle packages
      uses: actions/cache@v4
      with:
        path: |
          ~/.gradle/caches
          ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
        restore-keys: |
          ${{ runner.os }}-gradle-
    
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    
    - name: Log in to Container Registry
      uses: docker/login-action@v3
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
    
    - name: Extract metadata
      id: meta
      uses: docker/metadata-action@v5
      with:
        images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
        tags: |
          type=ref,event=branch
          type=sha,prefix=main-,format=short
          type=raw,value=latest,enable={{is_default_branch}}
    
    - name: Build and push Docker image
      id: build
      uses: docker/build-push-action@v5
      with:
        context: .
        platforms: linux/amd64
        push: true
        tags: ${{ steps.meta.outputs.tags }}
        labels: ${{ steps.meta.outputs.labels }}
        cache-from: type=gha
        cache-to: type=gha,mode=max
        build-args: |
          BUILDKIT_INLINE_CACHE=1

  update-manifest:
    needs: build-and-push
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        fetch-depth: 0
    
    - name: Update Kubernetes manifests
      run: |
        # 새로운 이미지 태그 추출 (main-short-sha 형태)
        NEW_TAG=$(echo "${{ needs.build-and-push.outputs.image-tag }}" | grep -E "main-[a-f0-9]{7}" | head -1 | cut -d':' -f2)
        echo "New tag: $NEW_TAG"
        
        # kustomization.yaml에서 이미지 태그 업데이트
        sed -i "s|newTag: .*|newTag: ${NEW_TAG}|g" k8s/kustomization.yaml
        
        # 변경사항 확인
        git diff k8s/kustomization.yaml
        
        # 변경사항 커밋 및 푸시
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add k8s/kustomization.yaml
        
        if git diff --staged --quiet; then
          echo "No changes to commit"
        else
          git commit -m "Deploy: Update image tag to ${NEW_TAG}"
          git push
        fi

도커이미지를 만드는 build-and-push와 argocd에서 감지하는 manifest 파일을 수정하는 update-manifest job으로 나뉨

profile
하루살이

0개의 댓글