feature/ssh-server (#1)
All checks were successful
/ ssh-client (push) Successful in 10s
/ ssh-server (push) Successful in 10s

Co-authored-by: Márcio Fernandes <marcio.fernandes@outlook.pt>
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2025-09-07 13:50:18 +00:00
parent ea24e0e41a
commit ce1d7a749a
23 changed files with 727 additions and 10 deletions

View File

@@ -0,0 +1,92 @@
#!/bin/bash
# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ 🐳 Podman Build Script with Layered .env Configuration ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
#
# This script builds a Podman image using a specified Dockerfile,
# dynamically injecting build-time environment variables from a
# prioritized set of `.env` files.
#
# ──────────────── Behavior ────────────────
# - Loads variables from the following files (in order):
# build.env
# build.env.${BUILD_ENV}
# build.local.env
#
# - Later files override earlier ones (last-write-wins)
# - Variables starting with `BUILD_ENV` are excluded from build args
#
# ──────────────── Required Environment Variables ────────────────
# - BUILD_ENV: (optional) Environment name (e.g. dev, prod)
# - BUILD_ENV_DOCKER_FILE: Dockerfile to use (default: Dockerfile)
# - BUILD_ENV_IMAGE_TAG: Tag to assign to the built image
#
# ──────────────── Example Usage ────────────────
# BUILD_ENV=dev BUILD_ENV_IMAGE_TAG=myapp:dev ./build.sh
# BUILD_ENV=prod BUILD_ENV_DOCKER_FILE=Dockerfile.prod BUILD_ENV_IMAGE_TAG=myapp:prod ./build.sh
#
# ──────────────── Output ────────────────
# - Logs each loaded file
# - Displays the final build command
# - Executes the podman build with all resolved --build-arg flags
#
# Author: Márcio
# Last Updated: September 2025
POSSIBLE_BUILD_ARGS_FILES="\
build.env \
"
if [ -n "${BUILD_ENV+x}" ]; then
POSSIBLE_BUILD_ARGS_FILES="$POSSIBLE_BUILD_ARGS_FILES \
build.env.${BUILD_ENV} \
"
fi
POSSIBLE_BUILD_ARGS_FILES="$POSSIBLE_BUILD_ARGS_FILES \
build.env.local"
# load variables into this context
declare -A build_args_map
BUILD_ARGS=""
for file in $POSSIBLE_BUILD_ARGS_FILES; do
if [ -f "$file" ]; then
echo "🔧 Loading variables from $file"
set -a
source "$file"
set +a
while IFS= read -r line || [ -n "$line" ]; do
# Skip comments and empty lines
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
# Extract key and value
key="$(echo "$line" | cut -d= -f1 | xargs)"
value="$(echo "$line" | cut -d= -f2- | xargs)"
# Skip keys that start with BUILD_ENV
if [[ "$key" == BUILD_ENV* ]]; then
echo "Skipping key starting with BUILD_ENV: $key"
continue
fi
build_args_map["$key"]="$value"
done < "$file"
else
echo "⚠️ Skipping missing file: $file"
fi
done
for key in "${!build_args_map[@]}"; do
BUILD_ARGS+=" --build-arg $key=${build_args_map[$key]}"
done
# sets default docker file
BUILD_ENV_DOCKER_FILE="${BUILD_ENV_DOCKER_FILE:-Dockerfile}"
echo "Build env: $BUILD_ENV"
echo "Build DockerFile: $BUILD_ENV_DOCKER_FILE"
echo "Build Tag: $BUILD_ENV_IMAGE_TAG"
echo "Running: $BUILD_CLI -f $BUILD_ENV_DOCKER_FILE $BUILD_ARGS -t $BUILD_ENV_IMAGE_TAG ."
$BUILD_CLI build -f $BUILD_ENV_DOCKER_FILE $BUILD_ARGS -t $BUILD_ENV_IMAGE_TAG .