From ad472bc9d98c836927a0387f29289e3340075838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kukuczka?= Date: Sun, 30 Mar 2025 12:03:46 +0200 Subject: [PATCH] improve robustness --- action.yml | 95 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/action.yml b/action.yml index 27dc20a..84f30c6 100644 --- a/action.yml +++ b/action.yml @@ -1,49 +1,86 @@ name: 'Process Deployment File' -description: 'Processes docker-stack.yml with environment variables' +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' + description: 'Stack name to substitute in deployment file' required: true org_name: - description: 'Organization name' + description: 'Organization name to substitute in deployment file' required: true repo_name: - description: 'Repository name' + description: 'Repository name to substitute in deployment file' required: true git_sha: - description: 'Git short SHA' + description: 'Git short SHA to substitute in deployment file' required: true + outputs: processed_file: - description: 'Path to processed deployment file' - value: 'docker-stack.processed.yml' + description: 'Path to the processed deployment file' + value: ${{ steps.process.outputs.processed_file }} + runs: using: 'composite' steps: - - shell: bash + - id: process + shell: bash run: | - echo "Starting processing of docker-stack.yml" - cp docker-stack.yml docker-stack.processed.yml - + 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 ".env file found, processing variables..." - while IFS= read -r line || [[ -n "$line" ]]; do - if [[ $line =~ ^[^#]+=.+ ]]; then - VAR_NAME=$(echo "$line" | cut -d= -f1) - VAR_VALUE=$(echo "$line" | cut -d= -f2-) - echo "Replacing variable from .env: $VAR_NAME=$VAR_VALUE" - sed -i "s|\${$VAR_NAME}|$VAR_VALUE|g" docker-stack.processed.yml - fi + 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 "No .env file found, skipping environment variable replacement." + echo "DEBUG: No .env file found. Skipping .env substitutions." fi - - echo "Replacing GitHub Actions input variables..." - sed -i "s|\${STACK_NAME}|${{ inputs.stack_name }}|g" docker-stack.processed.yml - sed -i "s|\${REPO_ORG}|${{ inputs.org_name }}|g" docker-stack.processed.yml - sed -i "s|\${REPO_NAME}|${{ inputs.repo_name }}|g" docker-stack.processed.yml - sed -i "s|\${GIT_SHA}|${{ inputs.git_sha }}|g" docker-stack.processed.yml - - echo "Processed file content:" - cat docker-stack.processed.yml + + # 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