deploy-ready-copilot / cicd_generator.py
HIMANSHUKUMARJHA's picture
Add 10 major utility improvements
a42b16d
"""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
}