Files
honey-biscuit-workshop/workshop-pipeline/examples/.workshop/bake.sh
T
Catty Steve f7ea4dfb3e feat: add workshop-pipeline
Signed-off-by: Catty Steve <4795515+Catty2014@user.noreply.gitee.com>
Workshop-pipeline is a crate for parsing prebake/bake/finalize config.
Not finished yet, further testing required.
2025-11-14 11:05:05 +08:00

255 lines
6.0 KiB
Bash

#!/bin/bash
# .workshop/bake
# 构建阶段脚本 - 使用标准的bash语法
set -euo pipefail # 严格模式:错误退出、未定义变量检查、管道错误检查
# 颜色定义用于输出
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# 日志函数
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $*${NC}"
}
log_warn() {
echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARN: $*${NC}"
}
log_error() {
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: $*${NC}"
}
log_info() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')] INFO: $*${NC}"
}
# 环境变量(由CI/CD系统注入)
readonly WORKSPACE_DIR="${WORKSPACE_DIR:-/workspace}"
readonly BUILD_ID="${BUILD_ID:-unknown}"
readonly COMMIT_SHA="${COMMIT_SHA:-}"
readonly BRANCH_NAME="${BRANCH_NAME:-main}"
readonly BUILD_CACHE_DIR="${BUILD_CACHE_DIR:-/cache}"
# 构建配置(从prebake和环境变量读取)
readonly RUST_VERSION="${RUST_VERSION:-1.70}"
readonly BUILD_TYPE="${BUILD_TYPE:-release}"
readonly CARGO_REGISTRY="${CARGO_REGISTRY:-$BUILD_CACHE_DIR/cargo/registry}"
readonly CARGO_GIT="${CARGO_GIT:-$BUILD_CACHE_DIR/cargo/git}"
# 切换到工作目录
cd "$WORKSPACE_DIR"
# 创建必要的目录
mkdir -p target
mkdir -p "$CARGO_REGISTRY"
mkdir -p "$CARGO_GIT"
# 配置Cargo使用缓存目录
export CARGO_HOME="$BUILD_CACHE_DIR/cargo"
export CARGO_TARGET_DIR="$WORKSPACE_DIR/target"
# 构建前检查
log "Starting build process"
log "Build ID: $BUILD_ID"
log "Commit: $COMMIT_SHA"
log "Branch: $BRANCH_NAME"
log "Rust version: $RUST_VERSION"
# 检查必要的工具
check_requirements() {
log "Checking build requirements..."
if ! command -v cargo &> /dev/null; then
log_error "Cargo not found. Please ensure Rust is installed."
exit 1
fi
if ! command -v git &> /dev/null; then
log_error "Git not found. Please install git."
exit 1
fi
log "All requirements satisfied"
}
# 代码质量检查
run_lints() {
log "Running code quality checks..."
# Rustfmt检查代码格式
if command -v rustfmt &> /dev/null; then
log "Checking code formatting with rustfmt..."
cargo fmt -- --check || {
log_warn "Code formatting issues found"
# 非致命错误,继续构建
}
fi
# Clippy静态分析
log "Running Clippy..."
cargo clippy -- -D warnings || {
log_warn "Clippy warnings found"
# 非致命错误,继续构建
}
# 安全检查
if command -v cargo-audit &> /dev/null; then
log "Checking for security vulnerabilities..."
cargo audit || {
log_warn "Security vulnerabilities found"
# 非致命错误,继续构建
}
fi
}
# 运行测试
run_tests() {
log "Running tests..."
# 单元测试
cargo test --lib --bins --tests || {
log_error "Unit tests failed"
exit 1
}
# 集成测试(如果存在)
if [ -d "tests" ] && [ -n "$(find tests -name '*.rs' -print -quit)" ]; then
cargo test --tests || {
log_error "Integration tests failed"
exit 1
}
fi
# 文档测试
cargo test --doc || {
log_warn "Documentation tests failed"
# 文档测试失败不阻断构建
}
}
# 构建项目
build_project() {
log "Building project..."
case "$BUILD_TYPE" in
debug)
log "Building in debug mode"
cargo build
;;
release)
log "Building in release mode"
cargo build --release
;;
*)
log_error "Unknown build type: $BUILD_TYPE"
exit 1
;;
esac
# 检查构建是否成功
if [ $? -eq 0 ]; then
log "Build completed successfully"
else
log_error "Build failed"
exit 1
fi
}
# 生成文档
generate_docs() {
log "Generating documentation..."
if [ "$BUILD_TYPE" = "release" ]; then
cargo doc --no-deps --release || {
log_warn "Documentation generation failed"
# 文档生成失败不阻断构建
}
else
cargo doc --no-deps || {
log_warn "Documentation generation failed"
# 文档生成失败不阻断构建
}
fi
}
# 创建构建产物清单
create_artifact_manifest() {
log "Creating artifact manifest..."
local manifest_file="$WORKSPACE_DIR/artifacts.json"
cat > "$manifest_file" << EOF
{
"build_id": "$BUILD_ID",
"commit_sha": "$COMMIT_SHA",
"branch": "$BRANCH_NAME",
"build_type": "$BUILD_TYPE",
"timestamp": "$(date -Iseconds)",
"artifacts": [
{
"name": "binary",
"path": "target/$BUILD_TYPE/$(basename "$WORKSPACE_DIR")",
"type": "executable"
},
{
"name": "documentation",
"path": "target/doc",
"type": "directory"
}
]
}
EOF
log "Artifact manifest created: $manifest_file"
}
# 构建后清理
cleanup() {
log "Performing build cleanup..."
# 清理临时文件
find . -name "*.tmp" -delete
find . -name "*.log" -delete
# 保留构建缓存
log "Build cache preserved in: $BUILD_CACHE_DIR"
}
# 主构建流程
main() {
log "Starting bake phase"
check_requirements
run_lints
run_tests
build_project
generate_docs
create_artifact_manifest
cleanup
log "Bake phase completed successfully"
# 输出构建摘要
echo "=========================================="
log_info "Build Summary"
echo "------------------------------------------"
log_info "Build ID: $BUILD_ID"
log_info "Status: SUCCESS"
log_info "Artifacts:"
log_info " - Binary: target/$BUILD_TYPE/$(basename "$WORKSPACE_DIR")"
log_info " - Documentation: target/doc/"
log_info " - Manifest: artifacts.json"
echo "=========================================="
}
# 信号处理
trap 'log_error "Build interrupted"; exit 130' INT TERM
# 执行主函数
main "$@"