THOMASZIMMER13/github-actions-cicd-pipelines

GitHub: THOMASZIMMER13/github-actions-cicd-pipelines

一套生产级的可复用 GitHub Actions 工作流,覆盖 CI/CD 全链路并提供安全扫描与成本估算。

Stars: 1 | Forks: 0

# GitHub Actions CI/CD 流水线 [![Ansible Lint](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/ansible-lint.yml/badge.svg)](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/ansible-lint.yml) [![Terraform Plan](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/terraform-plan.yml/badge.svg)](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/terraform-plan.yml) [![Docker Build & Push](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/docker-build-push.yml/badge.svg)](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/docker-build-push.yml) [![Security Scan](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/security-scan.yml/badge.svg)](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/security-scan.yml) [![Release](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/release.yml/badge.svg)](https://github.com/thomas/github-actions-cicd-pipelines/actions/workflows/release.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 这是一组用于 DevOps 自动化的生产级、可复用的 GitHub Actions 工作流。这些工作流覆盖完整的 DevOps 生命周期:包括代码检查、基础设施验证、容器构建、安全扫描和自动化发布。 ## 工作流 ### Ansible Lint(`ansible-lint.yml`) 使用 yamllint 和 ansible-lint 验证 Ansible Playbook 和角色。包含 pip 缓存以加快执行速度。 **触发条件:** 对 Ansible 目录中的 `*.yml` 和 `*.yaml` 文件执行推送或拉取请求。 **特性:** - yamllint 语法检查 - ansible-lint 最佳实践强制执行 - pip 依赖缓存以加快运行速度 - 可通过 `.yamllint` 和 `.ansible-lint` 配置检查规则 **用法:** ``` uses: thomas/github-actions-cicd-pipelines/.github/workflows/ansible-lint.yml@main with: ansible_directory: "./ansible" ``` ### Terraform Plan(`terraform-plan.yml`) 运行 `terraform fmt`、`validate` 和 `plan`,并上传产物。使用 Infracost 在拉取请求中发布成本估算注释。 **触发条件:** 对 `*.tf` 文件执行推送或拉取请求。 **特性:** - 使用 `terraform fmt -check` 进行格式检查 - 使用 `terraform validate` 进行配置验证 - 生成计划并上传产物 - 通过 Infracost 在 PR 中添加成本估算注释 - 支持多个 Terraform 工作区 **用法:** ``` uses: thomas/github-actions-cicd-pipelines/.github/workflows/terraform-plan.yml@main with: terraform_directory: "./terraform" terraform_version: "1.7.0" secrets: INFRACOST_API_KEY: ${{ secrets.INFRACOST_API_KEY }} ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }} ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }} ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }} ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }} ``` ### Docker Build & Push(`docker-build-push.yml`) 支持多平台(amd64/arm64)的 Docker 构建,并推送到 GitHub 容器注册表(GHCR)。支持基于 Git 标签的语义化版本控制。 **触发条件:** 匹配 `v*` 的推送标签或主分支的拉取请求。 **特性:** - 通过 Docker Buildx 进行多平台构建(linux/amd64、linux/arm64) - 推送到 GitHub 容器注册表(GHCR) - 基于 Git 标签的语义化版本标记 - 构建层缓存以加快构建速度 - 构建证明和 SBOM 生成 **用法:** ``` uses: thomas/github-actions-cicd-pipelines/.github/workflows/docker-build-push.yml@main with: image_name: "my-app" dockerfile: "./Dockerfile" context: "." platforms: "linux/amd64,linux/arm64" ``` ### Security Scan(`security-scan.yml`) 多层漏洞分析:对容器使用 Trivy,对 Terraform 使用 tfsec,并对 Ansible 使用安全规则进行扫描。 **触发条件:** 对主分支的推送或拉取请求,每周定时执行。 **特性:** - Trivy 容器镜像扫描(CVE 检测) - tfsec Terraform 配置错误静态分析 - Ansible-lint 安全规则强制执行 - 将 SARIF 报告上传到 GitHub 安全页面 - 可配置的安全严重性阈值 **用法:** ``` uses: thomas/github-actions-cicd-pipelines/.github/workflows/security-scan.yml@main with: scan_type: "all" trivy_image: "ghcr.io/thomas/my-app:latest" terraform_directory: "./terraform" ansible_directory: "./ansible" ``` ### Release(`release.yml`) 基于 Conventional Commits 自动生成变更日志的语义化发布。 **触发条件:** 对主分支的推送。 **特性:** - 根据提交信息自动进行版本号 bump - 自动生成变更日志 - 创建带有发布说明的 GitHub Release - 创建 Git 标签 **用法:** ``` uses: thomas/github-actions-cicd-pipelines/.github/workflows/release.yml@main ``` ## 组合动作 ### 设置工具(`actions/setup-tools`) 可复用的组合动作,用于安装和配置常见的 DevOps 工具(Ansible、Terraform、Docker Buildx、Trivy、tfsec)。 **用法:** ``` - uses: thomas/github-actions-cicd-pipelines/actions/setup-tools@main with: install_ansible: "true" install_terraform: "true" terraform_version: "1.7.0" install_trivy: "true" install_tfsec: "true" ``` ## 示例 `examples/` 目录包含现实中的工作流示例: | 文件 | 描述 | |------|-------------| | `ansible-deployment.yml` | 完整的 Ansible 部署流水线,包含清单管理 | | `terraform-apply.yml` | Terraform 计划 + 手动审批 + 应用工作流 | | `docker-multistage.yml` | 多阶段 Docker 构建,包含测试和部署阶段 | ## 仓库结构 ``` . ├── .github/ │ └── workflows/ │ ├── ansible-lint.yml # Ansible linting workflow │ ├── terraform-plan.yml # Terraform validation & plan │ ├── docker-build-push.yml # Multi-platform Docker build │ ├── security-scan.yml # Security scanning (Trivy, tfsec) │ └── release.yml # Semantic release & changelog ├── actions/ │ └── setup-tools/ │ └── action.yml # Composite action for tool setup ├── examples/ │ ├── ansible-deployment.yml # Ansible deployment example │ ├── terraform-apply.yml # Terraform apply example │ └── docker-multistage.yml # Docker multistage example ├── README.md ├── LICENSE └── .gitignore ``` ## 快速开始 1. **直接使用工作流**:在仓库中引用它: ```yaml jobs: lint: uses: thomas/github-actions-cicd-pipelines/.github/workflows/ansible-lint.yml@main ``` 2. **复制工作流**到你的 `.github/workflows/` 目录并进行自定义。 3. **使用组合动作**,确保流水线中工具安装的一致性。 ## 要求 - 已启用 Actions 的 GitHub 仓库 - 配置适当的密钥(请参考每个工作流所需的密钥) - 对于 Docker 工作流:通过 `GITHUB_TOKEN` 访问 GHCR - 对于 Terraform 工作流:云提供商凭证 - 对于成本估算:Infracost API 密钥 ## 作者 **Thomas** - DevOps 工程师 ## 许可证 本项目根据 MIT 许可证授权 - 详细信息请参阅 [LICENSE](LICENSE) 文件。
标签:Ansible, ansible-lint, Docker, ECS, GitHub Actions, Infrastructure as Code, lint, MIT License, OSS, pip缓存, SEO, Terraform, yamllint, 可复用工作流, 安全扫描, 安全防御评估, 容器构建, 开源框架, 持续交付, 持续集成, 时序注入, 流水线, 生产就绪, 系统提示词, 网络调试, 自动化, 自动笔记, 语义化发布, 请求拦截