PHpullh
학습 라이브러리/AI Prompts/GitHub Actions 자동화 워크플로우

AI PROMPTS · 인프라 & 자동화

GitHub Actions 자동화 워크플로우

PR이 생성되거나 데이터 파일이 변경될 때 자동으로 검증하는 GitHub Actions 워크플로우입니다.

인프라 & 자동화

핵심 설명

PR이 생성되거나 데이터 파일이 변경될 때 자동으로 검증하는 GitHub Actions 워크플로우입니다.

.github/workflows/validate.yml

name: Validate Data Files

on:
  push:
    branches: [main, develop]
    paths: ['data/**/*.js']
  pull_request:
    paths: ['data/**/*.js']

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Validate JS Files
        run: |
          for file in data/*.js; do
            echo "검증 중: $file"
            node -e "
              const fs = require('fs');
              const code = fs.readFileSync('$file', 'utf8');
              // IIFE 실행 테스트
              const vm = require('vm');
              const ctx = { window: { DATA: {} } };
              vm.createContext(ctx);
              vm.runInContext(code, ctx);
              // 데이터 검증
              const data = ctx.window.DATA;
              Object.entries(data).forEach(([key, val]) => {
                if (!val.items) throw new Error(`${key}: items 없음`);
                val.items.forEach((item, i) => {
                  const req = ['id','cat','title','desc'];
                  req.forEach(f => {
                    if (!item[f]) throw new Error(
                      `${key}[${i}]: '${f}' 필드 없음`
                    );
                  });
                });
                console.log(`  ✅ ${key}: ${val.items.length}개 항목 유효`);
              });
            "
          done

      - name: Check Duplicate IDs
        run: |
          node -e "
            const fs = require('fs');
            const allIds = [];
            ['kotlin','python','go','java'].forEach(lang => {
              const ctx = { window: { DATA: {} } };
              require('vm').runInNewContext(
                fs.readFileSync(`data/${lang}.js`,'utf8'), ctx
              );
              const items = ctx.window.DATA[lang]?.items || [];
              items.forEach(item => {
                if (allIds.includes(item.id)) {
                  throw new Error(`중복 ID: ${item.id}`);
                }
                allIds.push(item.id);
              });
            });
            console.log(`✅ 총 ${allIds.length}개 고유 ID 확인`);
          "

      - name: Deploy to GitHub Pages
        if: github.ref == 'refs/heads/main'
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: .