GitHub Actions CI/CD 완벽 가이드 | 자동 배포·테스트·Docker·AWS

GitHub Actions CI/CD 완벽 가이드 | 자동 배포·테스트·Docker·AWS

이 글의 핵심

GitHub Actions로 CI/CD 파이프라인을 구축하는 완벽 가이드입니다. 자동 테스트, 빌드, 배포, Docker, AWS, 캐싱, 보안까지 실전 예제로 정리했습니다.

실무 경험 공유: 대규모 마이크로서비스 아키텍처에 GitHub Actions를 도입하면서, 배포 시간을 2시간에서 10분으로 단축하고 배포 실패율을 30%에서 5%로 줄인 경험을 공유합니다.

들어가며: “배포가 너무 번거로워요”

실무 문제 시나리오

시나리오 1: 수동 배포가 2시간 걸려요
테스트, 빌드, 배포를 수동으로 하니 2시간 걸립니다. CI/CD는 10분입니다.

시나리오 2: 배포 실패가 잦아요
수동 배포 시 실수가 많습니다. 자동화로 실패율을 줄입니다.

시나리오 3: 여러 환경 배포가 복잡해요
dev, staging, production 배포가 복잡합니다. GitHub Actions로 자동화합니다.

flowchart LR
    subgraph Manual[수동 배포]
        A1[테스트: 30분]
        A2[빌드: 20분]
        A3[배포: 1시간 10분]
        A4[실패율: 30%]
    end
    subgraph Automated[자동 배포]
        B1[테스트: 5분]
        B2[빌드: 3분]
        B3[배포: 2분]
        B4[실패율: 5%]
    end
    Manual --> Automated

1. GitHub Actions란?

핵심 개념

GitHub Actions는 GitHub에 내장된 CI/CD 플랫폼입니다.

주요 구성 요소:

  • Workflow: 자동화 프로세스
  • Job: 워크플로우 내 작업 단위
  • Step: Job 내 개별 명령
  • Action: 재사용 가능한 작업
  • Runner: 워크플로우 실행 환경

가격 (2026년 기준):

  • Public 저장소: 무료
  • Private 저장소: 월 2000분 무료, 이후 $0.008/분

2. 첫 번째 워크플로우

기본 구조

# .github/workflows/ci.yml
name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Install dependencies
        run: npm ci
      
      - name: Run tests
        run: npm test

3. 자동 테스트

Node.js 프로젝트

# .github/workflows/test.yml
name: Test

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        node-version: [18, 20, 22]
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci
      
      - name: Run linter
        run: npm run lint
      
      - name: Run tests
        run: npm test -- --coverage
      
      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage/lcov.info

Python 프로젝트

# .github/workflows/python-test.yml
name: Python Test

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        python-version: ['3.10', '3.11', '3.12']
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'
      
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install pytest pytest-cov
      
      - name: Run tests
        run: pytest --cov=src tests/

4. Docker 빌드 및 배포

Docker 이미지 빌드

# .github/workflows/docker.yml
name: Docker Build

on:
  push:
    branches: [main]
    tags: ['v*']

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: myorg/myapp
          tags: |
            type=ref,event=branch
            type=semver,pattern={{version}}
            type=sha
      
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=registry,ref=myorg/myapp:buildcache
          cache-to: type=registry,ref=myorg/myapp:buildcache,mode=max

5. AWS 배포

S3 + CloudFront (정적 사이트)

# .github/workflows/deploy-s3.yml
name: Deploy to S3

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - name: Install and build
        run: |
          npm ci
          npm run build
      
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      
      - name: Deploy to S3
        run: |
          aws s3 sync ./dist s3://my-bucket --delete
      
      - name: Invalidate CloudFront
        run: |
          aws cloudfront create-invalidation \
            --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} \
            --paths "/*"

ECS (컨테이너)

# .github/workflows/deploy-ecs.yml
name: Deploy to ECS

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v2
      
      - name: Build and push image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: my-app
          IMAGE_TAG: ${{ github.sha }}
        run: |
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
      
      - name: Deploy to ECS
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: task-definition.json
          service: my-service
          cluster: my-cluster
          wait-for-service-stability: true

6. 캐싱으로 속도 향상

npm 캐싱

- name: Setup Node.js
  uses: actions/setup-node@v4
  with:
    node-version: '20'
    cache: 'npm'

- name: Install dependencies
  run: npm ci

커스텀 캐싱

- name: Cache dependencies
  uses: actions/cache@v3
  with:
    path: |
      ~/.npm
      node_modules
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-node-

7. 환경별 배포

dev, staging, production

# .github/workflows/deploy.yml
name: Deploy

on:
  push:
    branches:
      - dev
      - staging
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Determine environment
        id: env
        run: |
          if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
            echo "environment=production" >> $GITHUB_OUTPUT
          elif [[ "${{ github.ref }}" == "refs/heads/staging" ]]; then
            echo "environment=staging" >> $GITHUB_OUTPUT
          else
            echo "environment=dev" >> $GITHUB_OUTPUT
          fi
      
      - name: Deploy to ${{ steps.env.outputs.environment }}
        run: |
          echo "Deploying to ${{ steps.env.outputs.environment }}"
          # 배포 스크립트 실행

8. Secrets 관리

GitHub Secrets 설정

  1. 저장소 → Settings → Secrets and variables → Actions
  2. New repository secret 클릭
  3. Name과 Value 입력

사용 예시

- name: Use secret
  env:
    API_KEY: ${{ secrets.API_KEY }}
    DATABASE_URL: ${{ secrets.DATABASE_URL }}
  run: |
    echo "API Key is set"
    npm run deploy

9. 실전 예제: 풀스택 앱 배포

# .github/workflows/fullstack-deploy.yml
name: Fullstack Deploy

on:
  push:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci
      
      - name: Run tests
        run: npm test
      
      - name: Run linter
        run: npm run lint

  build-frontend:
    needs: test
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - name: Install and build
        run: |
          npm ci
          npm run build
      
      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: frontend-build
          path: dist/

  build-backend:
    needs: test
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: ./backend
          push: true
          tags: myorg/backend:${{ github.sha }}

  deploy:
    needs: [build-frontend, build-backend]
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Download frontend build
        uses: actions/download-artifact@v4
        with:
          name: frontend-build
          path: dist/
      
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      
      - name: Deploy frontend to S3
        run: aws s3 sync ./dist s3://my-frontend-bucket --delete
      
      - name: Deploy backend to ECS
        run: |
          aws ecs update-service \
            --cluster my-cluster \
            --service my-backend-service \
            --force-new-deployment

정리 및 체크리스트

핵심 요약

  • GitHub Actions: GitHub 내장 CI/CD 플랫폼
  • 자동 테스트: 푸시/PR 시 자동 실행
  • Docker 빌드: 자동 이미지 빌드 및 푸시
  • AWS 배포: S3, CloudFront, ECS 자동 배포
  • 캐싱: npm, Docker 레이어 캐싱으로 속도 향상
  • Secrets: 민감 정보 안전하게 관리

CI/CD 체크리스트

  • 워크플로우 파일 작성
  • 자동 테스트 설정
  • 빌드 자동화
  • 배포 자동화
  • Secrets 설정
  • 캐싱 최적화
  • 환경별 배포 전략

같이 보면 좋은 글

  • Docker Compose 실전 가이드
  • Kubernetes 실전 가이드
  • AWS 배포 완벽 가이드

이 글에서 다루는 키워드

GitHub Actions, CI/CD, DevOps, Docker, AWS, 자동화, 배포

자주 묻는 질문 (FAQ)

Q. GitHub Actions 비용은 얼마인가요?

A. Public 저장소는 무료입니다. Private 저장소는 월 2000분 무료, 이후 $0.008/분입니다.

Q. Jenkins vs GitHub Actions, 어떤 게 나은가요?

A. GitHub Actions는 설정이 간단하고 GitHub와 통합이 좋습니다. Jenkins는 더 유연하지만 설정이 복잡합니다.

Q. Self-hosted runner를 사용할 수 있나요?

A. 네, 자체 서버에 runner를 설치하여 사용할 수 있습니다. 비용 절감과 보안 강화에 유리합니다.

Q. 워크플로우 실행 시간을 줄이려면?

A. 캐싱, 병렬 실행, 불필요한 단계 제거, Self-hosted runner 사용 등으로 시간을 줄일 수 있습니다.

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3