/엔지니어/Git / CI·CD/GitHub Actions로 CI/CD 파이프라인
Git / CI·CD중급ubuntugithub-actionscicdautomation

GitHub Actions로 CI/CD 파이프라인 구축하기

Push 이벤트에 테스트·빌드·배포를 자동화하는 GitHub Actions 워크플로우를 단계별로 작성합니다.

워크플로우 기본 구조

YAML
# .github/workflows/ci.yml
name: CI/CD Pipeline

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

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

      - name: 의존성 설치
        run: npm ci

      - name: 린트
        run: npm run lint

      - name: 테스트
        run: npm test

환경 변수와 시크릿

YAML
steps:
  - name: 배포
    env:
      API_KEY: ${{ secrets.API_KEY }}
      DB_URL: ${{ vars.DATABASE_URL }}
    run: ./deploy.sh

조건부 실행

YAML
steps:
  - name: 프로덕션 배포
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    run: ./deploy-prod.sh

  - name: 슬랙 알림
    if: always()
    run: ./notify-slack.sh

매트릭스 전략

YAML
jobs:
  test:
    strategy:
      matrix:
        node-version: [18, 20, 22]

    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

Docker 빌드 + 푸시

YAML
- name: Docker Hub 로그인
  uses: docker/login-action@v3
  with:
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_TOKEN }}

- name: 이미지 빌드 + 푸시
  uses: docker/build-push-action@v5
  with:
    context: .
    push: true
    tags: myapp:${{ github.sha }}
    cache-from: type=gha
    cache-to: type=gha,mode=max

SSH로 서버 배포

YAML
- name: 서버 배포
  uses: appleboy/ssh-action@v1
  with:
    host: ${{ secrets.SSH_HOST }}
    username: deploy
    key: ${{ secrets.SSH_KEY }}
    script: |
      cd /opt/myapp
      git pull origin main
      docker compose up -d --build
#github-actions#cicd#automation#pipeline#deploy
편집 안내 · Editorial Note

이 가이드는 AI 도구를 활용해 초안을 구성하고 사람이 명령어·문맥을 검토해 발행했습니다. 운영체제와 도구 버전에 따라 결과가 달라질 수 있으므로 적용 전 공식 문서를 함께 확인하세요. 오류를 발견하시면 이메일로 제보해 주세요.

관련 공식 문서Git 공식 문서

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.