Files
OpenMQTTGateway/scripts/ci_prepare_artifacts.sh
Alessandro Staniscia 98481c5145 [SITE] Renew the web board presentation and the ESP32 web upload + [SYS] Security checks (#2277)
* Refactor GitHub Actions workflows for build, documentation, and linting

- Consolidated build logic into reusable workflows (`task-build.yml` and `task-docs.yml`) to reduce duplication across multiple workflows.
- Introduced `environments.json` to centralize the list of PlatformIO build environments, improving maintainability and clarity.
- Updated `build.yml` and `build_and_docs_to_dev.yml` to utilize the new reusable workflows and environment definitions.
- Enhanced `release.yml` to streamline the release process and integrate documentation generation.
- Created reusable linting workflow (`task-lint.yml`) to standardize code formatting checks across the repository.
- Simplified manual documentation workflow by leveraging the new reusable documentation workflow.
- Improved artifact management and retention policies across workflows.
- Updated dependencies and versions in workflows to ensure compatibility and performance.

CI/CD pipeline agnostic of Workflow Engine and integrated on github actions

- Implemented ci.sh for orchestrating the complete build pipeline.
- Created ci_00_config.sh for centralized configuration of build scripts.
- Created ci_build_firmware.sh for building firmware for specified PlatformIO environments.
- Created ci_prepare_artifacts.sh for preparing firmware artifacts for upload or deployment.
- Created ci_set_version.sh for updating version tags in firmware configuration files.
- Created ci_build.sh to orchestrate the complete build pipeline.
- Created ci_qa.sh for code linting and formatting checks using clang-format.
- Created ci_site.sh for building and deploying VuePress documentation with version management.
- Implemented checks for required tools and dependencies in the new scripts.
- Improved internal scripts for better error handling and logging.

UPDATE the web installer manifest generation and update documentation structure
- Enhanced ci_list-env.sh to list environments from a JSON file.
- Replaced  common_wu.py and gen_wu.py scripts with new npm scripts for site generation and previewing on docsgen/gen_wu.js
- Replaced  generate_board_docs.py with docsgen/generated_board_docs.js
- Added new npm scripts for integration of site generation on build phase.
- Created preview_site.js to serve locally generated site over HTTPS with improved error handling.
- Added new CI environments for CI builds in environments.json.
- Deleted lint.yml as part of workflow cleanup.
- Enhanced task-build.yml to include linting as a job and added support for specifying PlatformIO version.
- Improved task-docs.yml to handle versioning more effectively and added clean option.

Enhance documentation
- ADD CLEAR Mark of development version of site
- Updated README.md to include detailed workflow dependencies and relationships using mermaid diagrams.
- Improved development.md with a quick checklist for contributors and clarified the code style guide.
- Enhanced quick_start.md with tips for contributors and streamlined the workflow explanation.

LINT FIX
- Refined User_config.h for better formatting consistency.
- Adjusted blufi.cpp and gatewayBT.cpp for improved code readability and consistency in formatting.
- Updated gatewaySERIAL.cpp and mqttDiscovery.cpp to enhance logging error messages.
- Improved sensorDS1820.cpp for better logging of device information.

Add security scan workflows for vulnerability detection

Add SBOM generation and upload to release workflow; update security scan summary handling

Add shellcheck suppor + FIX shellcheck warning

Enhance documentation for CI/CD scripts and workflows, adding details for security scanning and SBOM generation processes

Fix formatting and alignment in BLE connection handling

Reviewed the full web board presentation and the ESP32 web upload. The project uses a modern pattern where data is divided from the presentation layer.

- Removed the `generate_board_docs` script.
- Updated the `gen_wu` script in order to generate `boards-info.json`: the fail that containe all information about the configuration
- Created and isolate the file `boards-info.js` to streamline the parsing of PlatformIO dependencies, modules, environments and improve the handling of library information.
- Introduced vuepress component `BoardEnvironmentTable.vue` that render `boards-info.json` as UI card component
- Introduced vuepress component `FlashEnvironmentSelector.vue` that render a selectred environment from  `boards-info.json` and provide esp-web-upload feature on it
- Introduced a new board page `board-selector.md` for improved firmware selection.
- Updated `web-install.md` to enhance the firmware upload process, including a new board environment table.
- Enhanced custom descriptions in `environments.ini` to include HTML links for better user guidance and board image link

Add CC1101 initialization improvements and logging enhancements
Add installation step for PlatformIO dependencies in documentation workflow

Remove ci_set_version.sh script and associated versioning functionality

* Fix comment provisined

Fix PlatformIO version input reference in documentation workflow

Remove outdated Squeezelite-ESP32 installer documentation
2026-03-09 07:47:30 -05:00

293 lines
8.3 KiB
Bash
Executable File

#!/bin/bash
# shellcheck disable=SC2015
# Prepares firmware artifacts for upload or deployment
# Used by: CI/CD pipelines for artifact packaging
# Usage: ./prepare_artifacts.sh <environment> [OPTIONS]
set -euo pipefail
# Constants
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
readonly SCRIPT_DIR
readonly PROJECT_ROOT
# Load shared configuration (colors, logging functions, paths)
if [[ -f "${SCRIPT_DIR}/ci_00_config.sh" ]]; then
source "${SCRIPT_DIR}/ci_00_config.sh"
else
echo "ERROR: ci_00_config.sh not found" >&2
exit 1
fi
# Set absolute paths
BUILD_DIR="${PROJECT_ROOT}/${BUILD_DIR}"
DEFAULT_OUTPUT_DIR="${PROJECT_ROOT}/${ARTIFACTS_DIR}"
# Function to create output directory
prepare_output_dir() {
local output_dir="$1"
local clean_flag="${2:-false}"
if [[ -d "$output_dir" ]]; then
if [[ "$clean_flag" == "true" ]]; then
log_warn "Cleaning and recreating output directory: $output_dir"
rm -rf "$output_dir"
mkdir -p "$output_dir"
else
log_warn "Output directory already exists and will be reused: $output_dir"
fi
else
mkdir -p "$output_dir"
log_info "Created output directory: $output_dir"
fi
}
# Function to copy artifact with optional renaming
copy_artifact() {
local source="$1"
local dest="$2"
local artifact_type="$3"
if [[ ! -f "$source" ]]; then
log_warn "${artifact_type} not found: $source"
return 1
fi
if cp "$source" "$dest"; then
local size
size=$(stat -f%z "$dest" 2>/dev/null || stat -c%s "$dest" 2>/dev/null)
local size_kb=$((size / 1024))
log_info "✓ Copied ${artifact_type}: $(basename "$dest") (${size_kb} KB)"
return 0
else
log_error "Failed to copy ${artifact_type}: $source"
return 1
fi
}
# Function to prepare deployment artifacts (with renaming)
prepare_artifacts() {
local env="$1"
local output_dir="$2"
local env_dir="${BUILD_DIR}/${env}"
log_info "Preparing firmware directory for: $env"
local copied=0
# Copy and rename firmware.bin
if copy_artifact "${env_dir}/firmware.bin" "${output_dir}/${env}-firmware.bin" "firmware"; then
((copied++))
fi
# Copy and rename partitions.bin (optional)
copy_artifact "${env_dir}/partitions.bin" "${output_dir}/${env}-partitions.bin" "partitions" && ((copied++)) || true
# Copy and rename bootloader.bin (optional)
copy_artifact "${env_dir}/bootloader.bin" "${output_dir}/${env}-bootloader.bin" "bootloader" && ((copied++)) || true
# Copy boot_app0.bin if exists (ESP32 specific)
copy_artifact "${env_dir}/boot_app0.bin" "${output_dir}/${env}-boot_app0.bin" "boot_app0" && ((copied++)) || true
if [[ $copied -eq 0 ]]; then
log_error "No artifacts were copied"
return 1
fi
log_info "Copied ${copied} artifact(s) in deployment mode"
}
prepare_libraries() {
local env="$1"
local output_dir="$2"
local env_dir="${BUILD_DIR}/${env}"
# Process libraries: create temp copy with renamed folders, zip, preserve originals
log_info "Processing libraries for environment: $env"
TEMP_LIBDEPS=$(mktemp -p "$output_dir" -d) || { echo "Failed to create temp directory"; return 1; }
cp -r .pio/libdeps/"$env" "$TEMP_LIBDEPS/" || { log_error "Failed to copy libdeps for $env"; return 1; }
(
cd "$TEMP_LIBDEPS"
log_step "Replace space by _ in folder names (temp copy only)"
find . -type d -name "* *" | while read -r FNAME; do
mv "$FNAME" "${FNAME// /_}"
done
log_step "Zipping libraries per board"
for i in */; do
tar -czf "${i%/}-libraries.tgz" "$i" > /dev/null
done
mv ./*.tgz "${output_dir}"
)
rm -rf "$TEMP_LIBDEPS"
log_info "✓ Created library archives in: $output_dir"
}
prepare_sources() {
local output_dir="$1"
log_info "Preparing source code archive"
# Create and move sources tar.gz (newly generated, safe to move)
if tar -czf "${output_dir}/OpenMQTTGateway_sources.tgz" main LICENSE.txt > /dev/null; then
log_info "✓ Created source archive: OpenMQTTGateway_sources.tgz"
else
log_error "Failed to create source archive"
return 1
fi
}
# Function to list artifacts
list_artifacts() {
local output_dir="$1"
echo ""
echo "═══════════════════════════════════════"
echo " Prepared Artifacts"
echo "═══════════════════════════════════════"
if [[ -d "$output_dir" ]]; then
find "$output_dir" -type f | sort | while read -r file; do
local size
size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null)
local size_kb=$((size / 1024))
echo " $(basename "$file"): ${size_kb} KB"
done
else
echo " No artifacts found"
fi
echo "═══════════════════════════════════════"
}
# Show usage
usage() {
cat << EOF
Usage: $0 <environment> [OPTIONS]
Prepare artifacts for upload or deployment.
Arguments:
environment PlatformIO environment name, if omitted will be created source archive only.
Options:
--clean Clean existing output directory before preparing artifacts
--output DIR Output directory [default: generated/artifacts/]
--help Show this help message
Examples:
$0 esp32dev-all-test
$0 esp32dev-bt --deploy --manifest
$0 theengs-bridge --output build/artifacts --compress
EOF
}
# Main execution
main() {
local environment=""
local output_dir="$DEFAULT_OUTPUT_DIR"
local clean_flag=false
#local version="" ## WILL BE USED WHEN THE VERSION ITSELF AFFECTS THE ARTIFACTS NAMING
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--output)
output_dir="$2"
shift 2
;;
-v|--version)
if [[ -z "${2:-}" ]]; then
log_error "-v|--version requires a version string"
return 1
fi
#version="$2"
shift 2
;;
--clean)
clean_flag=true
shift
;;
--help|-h)
usage
exit 0
;;
-*)
log_error "Unknown option: $1"
usage
exit 1
;;
*)
environment="$1"
shift
;;
esac
done
# Change to project root
cd "$PROJECT_ROOT"
# TODO FOR NEXT STEP MULTI RELEASE: TAG, RC, edge
#if [[ -n "$version" ]]; then
# # Sanitize version string for directory name
# safe_version=$(echo "$version" | sed 's/[^a-zA-Z0-9._-]/_/g')
# output_dir="${output_dir}/${safe_version}"
#fi
# Validate inputs
if [[ -z "$environment" ]]; then
log_info "No environment specified, only preparing source archive"
# Create output directory
prepare_output_dir "$output_dir" "$clean_flag"
# Prepare source code archive
prepare_sources "$output_dir" || exit 1
else
# Check if build directory exists
if [[ ! -d "${BUILD_DIR}/${environment}" ]]; then
log_error "Build directory not found for environment: $environment"
log_error "Run build_firmware.sh first"
exit 1
fi
#normalize output directory path
#output_dir="${output_dir}/firmware-${environment}"
output_dir="${output_dir}/firmware_build"
# Create output directory
prepare_output_dir "$output_dir" "$clean_flag"
# Prepare artifacts based on mode
prepare_artifacts "$environment" "$output_dir" || exit 1
# Prepare libraries
prepare_libraries "$environment" "$output_dir" || exit 1
fi
# Show summary
list_artifacts "$output_dir"
log_info "Artifact preparation completed successfully"
}
# Run main if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi