#!/usr/bin/env bash # build.sh - Script to build the tsysdevstack-toolboxes-docs container set -e # Default values IMAGE_NAME="tsysdevstack/toolboxes-docs" TAG="latest" DOCKERFILE_PATH="Dockerfile" BUILD_CONTEXT="." PLATFORMS="linux/amd64,linux/arm64" # Parse command line arguments NO_CACHE=false QUIET=false SKIP_TESTS=false while [[ $# -gt 0 ]]; do case $1 in --no-cache) NO_CACHE=true shift ;; --quiet) QUIET=true shift ;; --skip-tests) SKIP_TESTS=true shift ;; --platforms) PLATFORMS="$2" shift 2 ;; -t|--tag) TAG="$2" shift 2 ;; -h|--help) echo "Usage: $0 [OPTIONS]" echo "" echo "Options:" echo " --no-cache Do not use cache when building" echo " --quiet Suppress build output except final result" echo " --skip-tests Skip running tests after build" echo " --platforms Specify platforms to build for (default: $PLATFORMS)" echo " -t, --tag Set image tag (default: $TAG)" echo " -h, --help Show this help message" echo "" echo "Examples:" echo " $0 # Build with default settings" echo " $0 --no-cache # Build without using cache" echo " $0 --tag v1.0.0 # Build with specific tag" exit 0 ;; *) echo "Unknown option: $1" exit 1 ;; esac done # Function to validate prerequisites check_prerequisites() { echo "Checking prerequisites..." # Check if Docker is available if ! command -v docker &> /dev/null; then echo "Error: Docker is not installed or not in PATH" exit 1 fi # Check if Docker daemon is running if ! docker version &> /dev/null; then echo "Error: Docker daemon is not running" exit 1 fi # Check if Docker Buildx is available if ! docker buildx version &> /dev/null; then echo "Error: Docker Buildx is not available" exit 1 fi echo "Prerequisites OK" } # Function to run QA checks before building run_qa_checks() { echo "Running QA checks..." # Check if Dockerfile exists if [ ! -f "$DOCKERFILE_PATH" ]; then echo "Error: Dockerfile not found at $DOCKERFILE_PATH" exit 1 fi # Run hadolint on Dockerfile echo "Running hadolint on Dockerfile..." if command -v hadolint &> /dev/null; then hadolint "$DOCKERFILE_PATH" || { echo "Error: hadolint found issues in Dockerfile" exit 1 } else echo "Warning: hadolint not found, skipping Dockerfile linting" fi # Run shellcheck on shell scripts echo "Running shellcheck on scripts..." for script in run.sh build.sh test.sh; do if [ -f "$script" ]; then if command -v shellcheck &> /dev/null; then shellcheck "$script" || { echo "Error: shellcheck found issues in $script" exit 1 } else echo "Warning: shellcheck not found, skipping $script linting" fi fi done # Run yamllint on yaml files echo "Running yamllint on YAML files..." if command -v yamllint &> /dev/null; then yamllint docker-compose.yml || { echo "Error: yamllint found issues in docker-compose.yml" exit 1 } else echo "Warning: yamllint not found, skipping docker-compose.yml linting" fi echo "QA checks passed" } # Function to build the image build_image() { echo "Building Docker image: $IMAGE_NAME:$TAG" local build_args=() if [ "$NO_CACHE" = true ]; then build_args+=(--no-cache) fi if [ "$QUIET" = true ]; then build_args+=(--quiet) fi # Use Docker Buildx for multi-platform build docker buildx build \ --platform "$PLATFORMS" \ --tag "$IMAGE_NAME:$TAG" \ "${build_args[@]}" \ --load \ "$BUILD_CONTEXT" echo "Image built successfully: $IMAGE_NAME:$TAG" } # Function to tag the image with additional tags if needed tag_image() { if [[ "$TAG" != "latest" ]]; then echo "Tagging image as latest..." docker tag "$IMAGE_NAME:$TAG" "$IMAGE_NAME:latest" fi } # Main execution flow main() { check_prerequisites run_qa_checks build_image tag_image if [ "$SKIP_TESTS" = false ]; then echo "Running tests after build..." if [ -f "./test.sh" ]; then ./test.sh else echo "Warning: test.sh not found, skipping tests" fi else echo "Skipping tests as requested" fi echo "Build completed successfully!" } main "$@"