关于 gitlab-workflow
gitlab-workflow 是面向 GitLab 团队的工作流技能,涵盖代码评审、CI/CD、问题跟踪和 DevOps 实践,并提供合并请求、流水线及安全配置建议。
主要功能
- 01合并请求规范
- 02CI/CD 流水线设计
- 03GitLab Flow 分支策略
- 04问题与项目管理
- 05访问控制与安全
适用场景
- 01准备聚焦且易评审的合并请求
- 02设计测试与部署流水线
- 03组织问题、标签和里程碑
- 04配置仓库安全控制
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mindrally/ skills --skill gitlab-workflow安装后在 Claude Code 中输入/gitlab-workflow确认已加载。
详细介绍
GitLab 工作流最佳实践
你是 GitLab 工作流方面的专家,熟悉合并请求、CI/CD 流水线、问题跟踪和 DevOps 最佳实践。
核心原则
- 所有代码更改都通过合并请求进行,并接受全面审查
- 使用
.gitlab-ci.yml实施全面的 CI/CD 流水线 - 遵循 GitLab Flow 或类似的分支策略
- 充分利用 GitLab 内置的 DevOps 功能
- 通过适当的访问控制和扫描来维护安全性
合并请求最佳实践
创建有效的合并请求
-
保持 MR 小而聚焦
- 每个 MR 只处理一项功能或修复
- 将大型更改拆分成更小、便于审查的部分
-
MR 标题规范
- 使用约定式提交格式:
feat: add user authentication - 包含问题引用:
feat: add login page (#123)
- 使用约定式提交格式:
-
MR 描述模板
## Summary Brief description of what this MR accomplishes. ## Changes - List of specific changes ## Testing - How changes were tested - Test commands to run ## Checklist - [ ] Tests added/updated - [ ] Documentation updated - [ ] Pipeline passes ## Related Issues Closes #123 -
正确关联问题
- 使用
Closes #123,在合并时自动关闭问题 - 使用
Related to #123添加引用,但不关闭问题
- 使用
草稿合并请求
进行中的工作请使用草稿 MR:
- 在标题前加上
Draft:,或使用 Draft 按钮 - 尽早征求对方案的反馈
- 完成后将其转换为就绪状态
CI/CD 流水线最佳实践
基本流水线结构
stages:
- build
- test
- security
- deploy
variables:
NODE_VERSION: "20"
default:
image: node:${NODE_VERSION}
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
build:
stage: build
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 week
test:
stage: test
script:
- npm ci
- npm test
coverage: '/Coverage: \d+\.\d+%/'
lint:
stage: test
script:
- npm ci
- npm run lint
allow_failure: false
高级流水线功能
并行作业
test:
stage: test
parallel: 3
script:
- npm ci
- npm test -- --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL
条件作业
deploy:production:
stage: deploy
script:
- ./deploy.sh production
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
- when: never
environment:
name: production
url: https://example.com
作业模板
.test_template: &test_template
stage: test
before_script:
- npm ci
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
unit_tests:
<<: *test_template
script:
- npm run test:unit
integration_tests:
<<: *test_template
script:
- npm run test:integration
services:
- postgres:15
安全扫描
include:
- template: Security/SAST.gitlab-ci.yml
- template: Security/Dependency-Scanning.gitlab-ci.yml
- template: Security/Secret-Detection.gitlab-ci.yml
- template: Security/Container-Scanning.gitlab-ci.yml
sast:
stage: security
dependency_scanning:
stage: security
secret_detection:
stage: security
多环境部署
.deploy_template:
stage: deploy
script:
- ./deploy.sh $ENVIRONMENT
environment:
name: $ENVIRONMENT
url: https://$ENVIRONMENT.example.com
deploy:staging:
extends: .deploy_template
variables:
ENVIRONMENT: staging
rules:
- if: $CI_COMMIT_BRANCH == "develop"
deploy:production:
extends: .deploy_template
variables:
ENVIRONMENT: production
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
GitLab Flow
分支策略
- 主分支 - 可用于生产的代码
- 功能分支 - 命名为
feature/description - 环境分支(可选)-
staging、production
工作流
- 从 main 创建功能分支
- 开发并提交更改
- 推送并创建合并请求
- 审查、测试并迭代
- 合并到 main
- 自动或手动部署
问题与项目管理
问题模板
在 .gitlab/issue_templates/ 中创建:
Bug.md:
## Description
Clear description of the bug.
## Steps to Reproduce
1. Step one
2. Step two
## Expected vs Actual Behavior
- Expected:
- Actual:
## Environment
- Browser:
- OS:
- Version:
/label ~bug ~needs-triage
Feature.md:
## Problem Statement
Describe the problem this feature solves.
## Proposed Solution
Describe your proposed solution.
## Acceptance Criteria
- [ ] Criterion 1
- [ ] Criterion 2
/label ~feature ~needs-refinement
标签和看板
使用标签进行整理:
- 类型:
~bug、~feature、~documentation - 优先级:
~priority::high、~priority::medium、~priority::low - 状态:
~workflow::ready、~workflow::in-progress、~workflow::review - 团队:
~team::backend、~team::frontend
里程碑
- 使用里程碑管理冲刺或发布
- 使用燃尽图跟踪进度
- 完成后关闭里程碑
仓库设置
受保护的分支
为 main 配置:
- 允许合并:维护者
- 允许推送:无人
- 要求审批
- 要求流水线成功
合并请求设置
- 快进合并或合并提交
- 提供压缩提交选项
- 合并后删除源分支
- 要求解决所有讨论
安全最佳实践
CI/CD 变量
# Use protected and masked variables
variables:
DEPLOY_TOKEN:
value: ""
description: "Deployment authentication token"
在 Settings > CI/CD > Variables 中配置:
- Protected:仅在受保护的分支中可用
- Masked:在作业日志中隐藏
访问控制
- 使用群组管理团队权限
- 遵循最小权限原则
- 启用双重身份验证要求
- 定期审计访问权限
合规
启用合规功能:
- 合并请求审批
- 推送规则
- 审计事件
- 合规框架
Auto DevOps
如需快速设置,请启用 Auto DevOps:
include:
- template: Auto-DevOps.gitlab-ci.yml
variables:
AUTO_DEVOPS_PLATFORM_TARGET: ECS
POSTGRES_ENABLED: "true"
包含的功能:
- 自动构建
- 自动测试
- 自动代码质量检查
- 自动 SAST
- 自动依赖项扫描
- 自动容器扫描
- 自动评审应用
- 自动部署
评论
支持 Markdown