Files
prepare-deployment-file/action.yml
Radosław Kukuczka ad472bc9d9 improve robustness
2025-03-30 12:03:46 +02:00

87 lines
3.0 KiB
YAML

name: 'Process Deployment File'
description: |
Processes a Docker Compose or Stack deployment file by substituting placeholders
with values from GitHub Action inputs, environment variables, and secrets.
inputs:
compose_file:
description: 'The deployment file to process (e.g., docker-compose.yml, prod.compose.yml)'
required: true
stack_name:
description: 'Stack name to substitute in deployment file'
required: true
org_name:
description: 'Organization name to substitute in deployment file'
required: true
repo_name:
description: 'Repository name to substitute in deployment file'
required: true
git_sha:
description: 'Git short SHA to substitute in deployment file'
required: true
outputs:
processed_file:
description: 'Path to the processed deployment file'
value: ${{ steps.process.outputs.processed_file }}
runs:
using: 'composite'
steps:
- id: process
shell: bash
run: |
set -euo pipefail
INPUT_FILE="${{ inputs.compose_file }}"
OUTPUT_FILE="${INPUT_FILE%.*}.processed.${INPUT_FILE##*.}"
echo "DEBUG: Processing deployment file: $INPUT_FILE"
if [ ! -f "$INPUT_FILE" ]; then
echo "ERROR: Input file '$INPUT_FILE' does not exist."
exit 1
fi
cp "$INPUT_FILE" "$OUTPUT_FILE"
# Substitute variables from .env file if it exists
if [ -f ".env" ]; then
echo "DEBUG: Found .env file. Substituting variables from .env."
while IFS='=' read -r VAR_NAME VAR_VALUE || [ -n "$VAR_NAME" ]; do
[[ "$VAR_NAME" =~ ^#.*$ || -z "$VAR_NAME" ]] && continue
VAR_VALUE=${VAR_VALUE//\"/}
echo "DEBUG: Replacing \${$VAR_NAME} with value from .env"
sed -i "s|\${$VAR_NAME}|$VAR_VALUE|g" "$OUTPUT_FILE"
done < .env
else
echo "DEBUG: No .env file found. Skipping .env substitutions."
fi
# Substitute GitHub Actions inputs
declare -A substitutions=(
["STACK_NAME"]="${{ inputs.stack_name }}"
["REPO_ORG"]="${{ inputs.org_name }}"
["REPO_NAME"]="${{ inputs.repo_name }}"
["GIT_SHA"]="${{ inputs.git_sha }}"
)
for VAR in "${!substitutions[@]}"; do
VALUE="${substitutions[$VAR]}"
echo "DEBUG: Replacing \${$VAR} with GitHub Action input value '$VALUE'"
sed -i "s|\${$VAR}|$VALUE|g" "$OUTPUT_FILE"
done
# Substitute environment variables and secrets available in GitHub Actions environment
echo "DEBUG: Substituting environment variables and secrets from GitHub Actions environment."
env | while IFS='=' read -r ENV_VAR ENV_VAL; do
if grep -q "\${$ENV_VAR}" "$OUTPUT_FILE"; then
echo "DEBUG: Replacing \${$ENV_VAR} with environment variable value."
sed -i "s|\${$ENV_VAR}|$ENV_VAL|g" "$OUTPUT_FILE"
fi
done
echo "DEBUG: Final processed deployment file content:"
cat "$OUTPUT_FILE"
echo "processed_file=$OUTPUT_FILE" >> $GITHUB_OUTPUT