GitHub Actions Generator
Generate GitHub Actions workflow YAML for CI/CD pipelines with customizable triggers, jobs, and steps.
name: CI
on:
push:
branches: [main, master]
jobs:
build:
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: Build
run: npm run buildQuestions Fréquentes
Implémentation du Code
import yaml
def generate_github_actions_workflow(
name: str,
trigger: str,
jobs: list[dict]
) -> str:
workflow = {
"name": name,
"on": {},
"jobs": {}
}
if trigger == "push":
workflow["on"] = {"push": {"branches": ["main"]}}
elif trigger == "pull_request":
workflow["on"] = {"pull_request": {"branches": ["main"]}}
elif trigger == "schedule":
workflow["on"] = {"schedule": [{"cron": "0 0 * * *"}]}
else:
workflow["on"] = {trigger: {}}
for job in jobs:
job_id = job["id"]
workflow["jobs"][job_id] = {
"runs-on": job.get("runs_on", "ubuntu-latest"),
"steps": job.get("steps", [])
}
return yaml.dump(workflow, default_flow_style=False, sort_keys=False)
# Example: Node.js CI workflow
jobs = [
{
"id": "build",
"runs_on": "ubuntu-latest",
"steps": [
{"uses": "actions/checkout@v4"},
{"name": "Setup Node", "uses": "actions/setup-node@v4",
"with": {"node-version": "20"}},
{"name": "Install dependencies", "run": "npm ci"},
{"name": "Run tests", "run": "npm test"},
]
}
]
yaml_output = generate_github_actions_workflow("CI", "push", jobs)
print(yaml_output)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.