用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/microwind/ai-skills --skill ci-cd命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | CI/CD流水线 |
| description | 当构建CI/CD流水线时,分析构建流程,优化部署策略,解决集成问题。验证自动化测试,设计持续集成,和最佳实践。 |
| license | MIT |
CI/CD流水线是现代软件交付的核心。不当的CI/CD配置会导致构建缓慢、部署失败和质量问题。在设计CI/CD流水线前需要仔细分析交付需求。
核心原则: 好的CI/CD流水线应该快速、可靠、自动化、可监控。坏的CI/CD流水线会导致构建瓶颈、部署风险和质量问题。
始终:
触发短语:
问题:
构建缓慢,资源浪费
错误示例:
- 缺少构建缓存
- 依赖重复下载
- 串行构建流程
- 资源配置不足
解决方案:
1. 配置构建缓存机制
2. 优化依赖管理策略
3. 实施并行构建
4. 合理分配构建资源
问题:
部署不稳定,回滚频繁
错误示例:
- 环境配置错误
- 部署脚本问题
- 健康检查缺失
- 回滚策略不当
解决方案:
1. 标准化环境配置
2. 完善部署脚本
3. 添加健康检查
4. 实施自动回滚
问题:
代码质量差,缺陷频发
错误示例:
- 测试覆盖不足
- 代码检查缺失
- 安全扫描遗漏
- 质量门禁缺失
解决方案:
1. 提升测试覆盖率
2. 集成代码检查工具
3. 添加安全扫描
4. 设置质量门禁
import yaml
import json
import os
import subprocess
from pathlib import Path
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
import re
@dataclass
class PipelineStage:
"""流水线阶段信息"""
name: str
type: str # build, test, deploy, etc.
commands: List[str]
dependencies: List[str]
issues: List[str]
@dataclass
class PipelineIssue:
"""流水线问题"""
severity: str # critical, high, medium, low
type: str
file: str
message: str
suggestion: str
line: Optional[int] = None
class CICDAnalyzer:
def __init__(self, repo_path: str):
self.repo_path = Path(repo_path)
self.stages: List[PipelineStage] = []
.issues: [PipelineIssue] = []
() -> [, ]:
:
.scan_ci_files()
.analyze_build_config()
.analyze_test_config()
.analyze_deploy_config()
.analyze_security_config()
.generate_report()
Exception e:
{: }
() -> :
ci_files = []
github_workflows = .repo_path / /
github_workflows.exists():
ci_files.extend(github_workflows.glob())
ci_files.extend(github_workflows.glob())
gitlab_ci = .repo_path /
gitlab_ci.exists():
ci_files.append(gitlab_ci)
jenkinsfile = .repo_path /
jenkinsfile.exists():
ci_files.append(jenkinsfile)
azure_pipelines = .repo_path /
azure_pipelines.exists():
ci_files.append(azure_pipelines)
ci_files:
.issues.append(PipelineIssue(
severity=,
=,
file=,
message=,
suggestion=
))
:
ci_file ci_files:
.analyze_ci_file(ci_file)
() -> :
:
(ci_file, , encoding=) f:
content = f.read()
ci_file.suffix [, ]:
.analyze_yaml_ci(content, (ci_file))
ci_file.name == :
.analyze_jenkins_file(content, (ci_file))
Exception e:
.issues.append(PipelineIssue(
severity=,
=,
file=(ci_file),
message=,
suggestion=
))
() -> :
:
yaml_data = yaml.safe_load(content)
yaml_data yaml_data:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
yaml_data:
.analyze_jobs(yaml_data[], file_path)
.check_environment_variables(yaml_data, file_path)
.check_cache_configuration(yaml_data, file_path)
yaml.YAMLError e:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
content:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
stages = re.findall(, content)
(stages) == :
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
content:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
job_name, job_config jobs.items():
job_config:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
job_config:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
:
.analyze_job_steps(job_config[], job_name, file_path)
() -> :
has_checkout =
has_build =
has_test =
step steps:
step:
step[]:
has_checkout =
step[].lower():
has_build =
step[].lower():
has_test =
step:
command = step[]
command.lower():
has_checkout =
command.lower() command.lower():
has_build =
command.lower():
has_test =
has_checkout:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
has_build job_name.lower():
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
has_test job_name.lower():
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
yaml_data:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
content = (yaml_data)
sensitive_patterns = [
,
,
,
]
pattern sensitive_patterns:
re.search(pattern, content, re.IGNORECASE):
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
content = (yaml_data)
content:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
build_files = [
,
,
,
,
,
]
build_file_found =
build_file build_files:
(.repo_path / build_file).exists():
build_file_found =
.analyze_build_file(.repo_path / build_file)
build_file_found:
.issues.append(PipelineIssue(
severity=,
=,
file=,
message=,
suggestion
))
() -> :
:
(build_file, , encoding=) f:
content = f.read()
build_file.name == :
.analyze_package_json(content, (build_file))
build_file.name == :
.analyze_pom_xml(content, (build_file))
build_file.name == :
.analyze_build_gradle(content, (build_file))
Exception e:
.issues.append(PipelineIssue(
severity=,
=,
file=(build_file),
message=,
suggestion=
))
() -> :
:
package_data = json.loads(content)
package_data:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
:
scripts = package_data[]
scripts:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
scripts:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
json.JSONDecodeError e:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
content:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
content:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
content:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
content:
.issues.append(PipelineIssue(
severity=,
=,
file=file_path,
message=,
suggestion=
))
() -> :
test_configs = [
,
,
,
]
test_config_found =
test_config test_configs:
(.repo_path / test_config).exists():
test_config_found =
test_config_found:
.issues.append(PipelineIssue(
severity=,
=,
file=,
message=,
suggestion=
))
() -> :
deploy_configs = [
,
,
,
,
]
deploy_config_found =
deploy_config deploy_configs:
(.repo_path / deploy_config).exists():
deploy_config_found =
deploy_config_found:
.issues.append(PipelineIssue(
severity=,
=,
file=,
message=,
suggestion=
))
() -> :
security_files = [
,
,
,
]
security_config_found =
security_file security_files:
(.repo_path / security_file).exists():
security_config_found =
security_config_found:
.issues.append(PipelineIssue(
severity=,
=,
file=,
message=,
suggestion=
))
() -> [, ]:
summary = {
: (.issues),
: ([i i .issues i.severity == ]),
: ([i i .issues i.severity == ]),
: ([i i .issues i.severity == ]),
: ([i i .issues i.severity == ]),
: (.stages)
}
recommendations = .generate_recommendations()
{
: summary,
: [.stage_to_dict(stage) stage .stages],
: [.issue_to_dict(issue) issue .issues],
: recommendations,
: .calculate_health_score(summary)
}
() -> [, ]:
{
: stage.name,
: stage.,
: stage.commands,
: stage.dependencies,
: stage.issues
}
() -> [, ]:
{
: issue.severity,
: issue.,
: issue.file,
: issue.message,
: issue.suggestion,
: issue.line
}
() -> [[, ]]:
recommendations = []
issue_types = {}
issue .issues:
issue_types[issue.] = issue_types.get(issue., ) +
issue_types.get(, ) > :
recommendations.append({
: ,
: ,
: ,
suggestion:
})
issue_types.get(, ) > :
recommendations.append({
: ,
: ,
: ,
suggestion:
})
issue_types.get(, ) > :
recommendations.append({
: ,
: ,
: ,
suggestion:
})
recommendations
() -> :
score =
score -= summary[] *
score -= summary[] *
score -= summary[] *
score -= summary[] *
(, score)
:
():
.repo_path = Path(repo_path)
() -> [, ]:
optimizations = []
build_optimization = .optimize_build_configuration()
build_optimization:
optimizations.append(build_optimization)
test_optimization = .optimize_test_configuration()
test_optimization:
optimizations.append(test_optimization)
deploy_optimization = .optimize_deploy_configuration()
deploy_optimization:
optimizations.append(deploy_optimization)
{
: optimizations,
: {
: (optimizations),
: .estimate_improvements(optimizations)
}
}
() -> [[, ]]:
github_workflows = .repo_path / /
github_workflows.exists():
workflow_file github_workflows.glob():
:
(workflow_file, , encoding=) f:
content = f.read()
content:
{
: ,
: ,
:
}
Exception:
() -> [[, ]]:
github_workflows = .repo_path / /
github_workflows.exists():
workflow_file github_workflows.glob():
:
(workflow_file, , encoding=) f:
content = f.read()
content:
{
: ,
: ,
:
}
Exception:
() -> [[, ]]:
github_workflows = .repo_path / /
github_workflows.exists():
workflow_file github_workflows.glob():
:
(workflow_file, , encoding=) f:
content = f.read()
content:
{
: ,
: ,
:
}
Exception:
() -> [, ]:
improvements = {
: ,
: ,
:
}
opt optimizations:
opt[] == :
improvements[] +=
opt[] == :
improvements[] +=
opt[] == :
improvements[] +=
improvements[] +=
improvements
():
analyzer = CICDAnalyzer()
report = analyzer.analyze_pipeline()
()
()
()
()
()
rec report[]:
()
optimizer = CICDOptimizer()
optimization = optimizer.optimize_pipeline()
()
opt optimization[]:
()
__name__ == :
main()
import time
import requests
from datetime import datetime, timedelta
from typing import Dict, Any, List, Optional
from dataclasses import dataclass
import json
@dataclass
class BuildMetrics:
"""构建指标"""
build_id: str
status: str
duration: float
start_time: datetime
end_time: Optional[datetime]
trigger: str
branch: str
@dataclass
class PipelineMetrics:
"""流水线指标"""
pipeline_id: str
name: str
status: str
duration: float
stages: List[str]
artifacts: List[str]
class CICDPerformanceMonitor:
def __init__(self, api_url: str, api_token: str):
self.api_url = api_url
self.api_token = api_token
self.builds: List[BuildMetrics] = []
self.pipelines: List[PipelineMetrics] = []
def () -> [, ]:
headers = {
: ,
:
}
:
workflow_runs_url =
response = requests.get(workflow_runs_url, headers=headers)
response.raise_for_status()
data = response.json()
workflow_runs = data.get(, [])
recent_runs = workflow_runs[:]
metrics = {
: (workflow_runs),
: .calculate_success_rate(recent_runs),
: .calculate_average_duration(recent_runs),
: .get_slowest_runs(recent_runs, ),
: .summarize_workflows(recent_runs)
}
metrics
Exception e:
{: }
() -> :
runs:
successful_runs = ([run run runs run[] == ])
(successful_runs / (runs)) *
() -> :
runs:
durations = []
run runs:
run[] == :
start_time = datetime.fromisoformat(run[].replace(, ))
end_time = datetime.fromisoformat(run[].replace(, ))
duration = (end_time - start_time).total_seconds()
durations.append(duration)
(durations) / (durations) durations
() -> [[, ]]:
completed_runs = [run run runs run[] == ]
run completed_runs:
start_time = datetime.fromisoformat(run[].replace(, ))
end_time = datetime.fromisoformat(run[].replace(, ))
run[] = (end_time - start_time).total_seconds()
sorted_runs = (completed_runs, key= x: x[], reverse=)
sorted_runs[:limit]
() -> [, ]:
workflow_summary = {}
run runs:
workflow_name = run.get(, )
workflow_name workflow_summary:
workflow_summary[workflow_name] = {
: ,
: ,
: ,
:
}
summary = workflow_summary[workflow_name]
summary[] +=
run[] == :
summary[] +=
run[] == :
summary[] +=
workflow_name, summary workflow_summary.items():
workflow_runs = [run run runs run.get() == workflow_name]
summary[] = .calculate_average_duration(workflow_runs)
workflow_summary
() -> [, ]:
headers = {
: .api_token
}
:
pipelines_url =
response = requests.get(pipelines_url, headers=headers)
response.raise_for_status()
data = response.json()
pipelines = data[:]
metrics = {
: (data),
: .calculate_pipeline_success_rate(pipelines),
: .calculate_pipeline_average_duration(pipelines),
: .summarize_pipelines(pipelines)
}
metrics
Exception e:
{: }
() -> :
pipelines:
successful_pipelines = ([p p pipelines p[] == ])
(successful_pipelines / (pipelines)) *
() -> :
pipelines:
durations = []
pipeline pipelines:
pipeline.get():
durations.append(pipeline[])
(durations) / (durations) durations
() -> [, ]:
status_summary = {}
pipeline pipelines:
status = pipeline[]
status status_summary:
status_summary[status] =
status_summary[status] +=
status_summary
() -> [, ]:
report = {
: datetime.now().isoformat(),
: metrics,
: .generate_performance_recommendations(metrics),
: .analyze_trends()
}
report
() -> []:
recommendations = []
metrics:
success_rate = metrics[]
success_rate < :
recommendations.append()
success_rate < :
recommendations.append()
metrics:
avg_duration = metrics[]
avg_duration > :
recommendations.append()
avg_duration > :
recommendations.append()
recommendations
() -> [, ]:
{
: ,
: ,
:
}
:
():
.monitor = monitor
.alert_thresholds = {
: ,
: ,
:
}
() -> [[, ]]:
alerts = []
metrics:
success_rate = metrics[]
success_rate < .alert_thresholds[]:
alerts.append({
: ,
: ,
: ,
: .alert_thresholds[]
})
metrics:
avg_duration = metrics[]
avg_duration > .alert_thresholds[]:
alerts.append({
: ,
: ,
: ,
: .alert_thresholds[]
})
alerts
() -> :
:
()
Exception e:
()
():
monitor = CICDPerformanceMonitor(
api_url=,
api_token=
)
metrics = monitor.collect_github_actions_metrics(, )
report = monitor.generate_performance_report(metrics)
()
(json.dumps(report, indent=, ensure_ascii=))
alert_system = CICDAlertSystem(monitor)
alerts = alert_system.check_alerts(metrics)
alerts:
()
alert alerts:
()
alert_system.send_alert(alert)
__name__ == :
main()