File size: 3,716 Bytes
a42b16d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""CI/CD pipeline generation."""

from __future__ import annotations

from typing import Any, Dict, List, Optional


class CICDGenerator:
    """Generates CI/CD pipeline configurations."""

    def generate_github_actions(
        self,
        framework: str,
        platform: str,
        test_command: Optional[str] = None
    ) -> Dict[str, Any]:
        """Generate GitHub Actions workflow."""
        
        # Framework-specific test commands
        test_commands = {
            "next.js": "npm run test",
            "react": "npm test",
            "django": "python manage.py test",
            "fastapi": "pytest",
            "flask": "pytest",
            "express": "npm test",
        }
        
        test_cmd = test_command or test_commands.get(framework.lower(), "npm test")
        
        # Platform-specific deployment steps
        deploy_steps = {
            "vercel": """
      - name: Deploy to Vercel
        uses: amondnet/vercel-action@v20
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.ORG_ID }}
          vercel-project-id: ${{ secrets.PROJECT_ID }}
""",
            "netlify": """
      - name: Deploy to Netlify
        uses: netlify/actions/cli@master
        with:
          args: deploy --prod --dir=build
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
""",
            "aws": """
      - name: Deploy to AWS
        run: |
          aws s3 sync build s3://${{ secrets.S3_BUCKET }}
          aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_ID }} --paths "/*"
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
""",
        }
        
        deploy_step = deploy_steps.get(platform.lower(), """
      - name: Deploy
        run: echo "Configure deployment for ${{ secrets.DEPLOY_PLATFORM }}"
""")
        
        workflow = f"""name: CI/CD Pipeline

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
      - name: Install dependencies
        run: npm install
      - name: Run tests
        run: {test_cmd}
      - name: Build
        run: npm run build

  deploy:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
    steps:
      - uses: actions/checkout@v3
      - name: Setup
        run: npm install
      - name: Build
        run: npm run build
{deploy_step}
"""
        
        return {
            "workflow": workflow,
            "file": ".github/workflows/deploy.yml",
            "framework": framework,
            "platform": platform,
            "steps": ["test", "build", "deploy"]
        }

    def generate_gitlab_ci(self, framework: str, platform: str) -> Dict[str, Any]:
        """Generate GitLab CI configuration."""
        config = f""".gitlab-ci.yml:
  stages:
    - test
    - build
    - deploy
  
  test:
    stage: test
    script:
      - npm install
      - npm test
  
  build:
    stage: build
    script:
      - npm install
      - npm run build
    artifacts:
      paths:
        - build/
  
  deploy:
    stage: deploy
    script:
      - echo "Deploy to {platform}"
    only:
      - main
"""
        return {
            "config": config,
            "file": ".gitlab-ci.yml",
            "framework": framework,
            "platform": platform
        }