CAST 快速入门

本指南将带您在五分钟内为项目仓库添加一套生产级 DevSecOps 流水线。

前提条件

无需任何外部账号、令牌或 SaaS 订阅。


第一步 — 安装 CLI

pip install castops

验证安装是否成功:

cast --help

第二步 — 初始化流水线

进入项目根目录后运行:

cast init

CAST 会自动检测您的项目类型并生成工作流文件:

╭──────────────────────────────────────────────────╮
│ CAST — CI/CD Automation & Security Toolkit        │
╰──────────────────────────────────────────────────╯
Detected project type: python
Downloading template... done

✓ Created .github/workflows/devsecops.yml

Commit and push to activate your DevSecOps pipeline:
  git add .github/workflows/devsecops.yml
  git commit -m 'ci: add CAST DevSecOps pipeline'
  git push

若自动检测失败(未找到 pyproject.tomlrequirements.txt 等),请手动指定项目类型:

cast init --type python

第三步 — 提交并推送

git add .github/workflows/devsecops.yml
git commit -m "ci: add CAST DevSecOps pipeline"
git push

GitHub Actions 会立即识别该工作流并运行您的首次流水线。


第四步 — 查看首次运行结果

  1. 进入您在 GitHub 上的仓库
  2. 点击 Actions 标签页
  3. 您应该能看到 "CAST DevSecOps" 正在运行

该流水线包含六个作业:

作业 工具 预期结果
密钥检测 Gitleaks Git 历史中无密钥则通过
SAST Semgrep 使用开源规则通过;配置云端令牌可获取更多规则
SCA pip-audit 依赖项无 CVE 则通过
容器安全 Trivy 无 Dockerfile 时跳过
代码质量 Ruff 代码符合规范则通过
安全门禁 内置 所有关键检查通过则放行

第五步 — 查看安全发现

Semgrep 和 Trivy 的所有发现均会上传至 GitHub 的 Security 标签页

  1. 进入仓库 → Security 标签页
  2. 点击 "Code scanning alerts"
  3. 审查发现的问题

新发现的问题也会在后续 Pull Request 中以内联注释的形式呈现。


第六步 — 启用门禁保护(可选但推荐)

为防止安全门禁失败的 Pull Request 被合并:

  1. 进入 Settings → Branches
  2. 点击 "Add branch protection rule"
  3. Branch name pattern 设置为 main
  4. 启用 "Require status checks to pass before merging"
  5. 搜索并选择 "Security Gate"
  6. 保存规则

此后,任何存在安全问题的 Pull Request 将被阻止合并。


可选:启用 Semgrep Cloud

获取更多安全规则并使用集中式发现看板:

  1. semgrep.dev 注册账号(提供免费套餐)
  2. 进入 Settings → Tokens,创建一个 CI 令牌
  3. 在 GitHub 仓库中,进入 Settings → Secrets and variables → Actions
  4. 添加名为 SEMGREP_APP_TOKEN 的 Secret,值为您的令牌

下次运行时,流水线将自动使用您的云端令牌。


手动安装(无需 CLI)

如果您不想安装 CLI,可以直接复制模板:

# 创建工作流目录
mkdir -p .github/workflows

# 下载 Python 模板
curl -o .github/workflows/devsecops.yml \
  https://raw.githubusercontent.com/castops/cast/main/src/cast_cli/templates/python/devsecops.yml

# 提交并推送
git add .github/workflows/devsecops.yml
git commit -m "ci: add CAST DevSecOps pipeline"
git push

后续步骤