name: commitlint displayName: Commitlint 提交信息检查 triggers: push: branches: - master pull_request: types: - opened - synchronize - reopened - edited stages: - name: lint displayName: 提交信息检查 steps: - step: shell@general name: commitlint-check displayName: 验证提交信息格式 inputs: commands: | # 安装 Node.js (如果未安装) if ! command -v node &> /dev/null; then echo "安装 Node.js..." curl -fsSL https://deb.nodesource.com/setup_18.x | bash - apt-get install -y nodejs || yum install -y nodejs npm fi # 显示 Node.js 版本 node --version npm --version # 安装 commitlint echo "安装 commitlint..." npm install -g @commitlint/cli @commitlint/config-conventional # 创建 commitlint 配置文件 cat > .commitlintrc.json << 'EOF' { "extends": ["@commitlint/config-conventional"] } EOF # 验证提交信息 echo "开始检查提交信息..." # 获取最近的提交进行验证 if [ -n "$CI_COMMIT_REF_NAME" ] && [ -n "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" ]; then # PR/MR 事件:验证所有新提交 echo "检查 Pull Request 提交信息..." BASE_SHA=$(git merge-base origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME:-master} HEAD) npx commitlint --from "$BASE_SHA" --to HEAD --verbose || exit 1 else # Push 事件:验证最新的提交 echo "检查 Push 提交信息..." npx commitlint --from HEAD~1 --to HEAD --verbose || exit 1 fi