diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 0a2934764..afbf88db7 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -15,47 +15,47 @@ jobs: with: fetch-depth: 0 - - name: Generate matrix scenarios + - name: Setup test matrix scenarios id: setup-matrix-scenarios run: | - set -eu - matrix=$(find $GITHUB_WORKSPACE/scenarios/ -name "*.json" | awk -F'/' '{split($11, file_name, "."); split(file_name[1], cloud_region, "-");region= (length(cloud_region) > 1) ? substr($11, index($11, "-") + 1) : ""; cloud=cloud_region[1]; gsub(".json", "", region); print "{\"cloud\": \"" cloud "\", \"file_name\": \"" file_name[1] "\", " (region != "" ? "\"region\": \"" region "\", " : "") "\"scenario_type\": \"" $8 "\", \"scenario_name\": \"" $9 "\"},"}' | sort | uniq | sed 's/,$/,/') - matrix_array="[${matrix%,}]" - # Remove .md, .sh, .py and github files from the changed list - file_changes=$(git diff --name-only -r origin/main HEAD | grep -Ev '\.md$|\.sh$|\.py$|\.github/') - echo "file_changes: $file_changes" + matrix=$(find $GITHUB_WORKSPACE/scenarios/ -name "*.json" | awk -F'/' '{split($11, file_name, "."); split(file_name[1], cloud_region, "-");region= (length(cloud_region) > 1) ? substr($11, index($11, "-") + 1) : ""; cloud=cloud_region[1]; gsub(".json", "", region); print "{\"cloud\": \"" cloud "\", \"file_name\": \"" file_name[1] "\", " (region != "" ? "\"region\": \"" region "\", " : "") "\"scenario_type\": \"" $8 "\", \"scenario_name\": \"" $9 "\"},"}' | sort | uniq | sed 's/,$/,/') + matrix_array="[${matrix%,}]" + echo $matrix_array + # Remove .md, .sh, .py and github files from the changed list + file_changes=$(git diff --name-only -r origin/main HEAD | grep -Ev '\.md$|\.sh$|\.py$|\.github/') + echo "file_changes: $file_changes" - terraform_module_changes=$(echo "$file_changes" | grep -E '\.tf$' | awk -F'/' '{print $3}' | xargs) - echo "terraform_module_changes: $terraform_module_changes" - run_all_tests=false - module_matrix='[]' - scenario_matrix='[]' - if [ $(echo "$terraform_module_changes" | wc -w) -eq 1 ]; then - cloud=$(echo "$terraform_module_changes" | cut -d' ' -f1) - module_matrix=$(echo "$matrix_array" | jq --arg cloud_value "$cloud" '. | map(select(.cloud == $cloud_value))') - elif [ $(echo "$terraform_module_changes" | wc -w) -eq 0 ]; then - run_all_tests=false - else - run_all_tests=true - module_matrix=$(echo "$matrix_array") - fi - echo "module_matrix: $module_matrix" - if [ "$run_all_tests" = false ]; then - changed_scenario_names=$(echo "$file_changes" | grep -E '^scenarios/' | awk -F'/' '{print $3}' | tr ' ' '\n' | sort -u | xargs) - echo "scenario_names: $changed_scenario_names" - for scenario_name in $changed_scenario_names; do - filtered_objects=$(echo "$matrix_array" | jq --arg scenario_value "$scenario_name" '. | map(select(.scenario_name == $scenario_value))') - scenario_matrix=$(echo "$scenario_matrix $filtered_objects" | jq -s 'flatten | unique') - done - fi - updated_matrix=$(echo "$module_matrix $scenario_matrix" | jq -s 'flatten | unique') - echo "updated_matrix: $updated_matrix" - updated_matrix="${updated_matrix//$'\n'/''}" - matrix_combinations="{\"include\": ${updated_matrix%?}}" - echo "matrix_combinations={\"include\": [${updated_matrix}]}" >> "$GITHUB_OUTPUT" - echo "matrix_combinations: $matrix_combinations" + terraform_module_changes=$(echo "$file_changes" | grep -E '\.tf$' | awk -F'/' '{print $3}' | xargs) + echo "terraform_module_changes: $terraform_module_changes" + run_all_tests=false + module_matrix='[]' + scenario_matrix='[]' + if [ $(echo "$terraform_module_changes" | wc -w) -eq 1 ]; then + cloud=$(echo "$terraform_module_changes" | cut -d' ' -f1) + module_matrix=$(echo "$matrix_array" | jq --arg cloud_value "$cloud" '. | map(select(.cloud == $cloud_value))') + elif [ $(echo "$terraform_module_changes" | wc -w) -eq 0 ]; then + run_all_tests=false + else + run_all_tests=true + module_matrix=$(echo "$matrix_array") + fi + echo "module_matrix: $module_matrix" + if [ "$run_all_tests" = false ]; then + changed_scenario_names=$(echo "$file_changes" | grep -E '^scenarios/' | awk -F'/' '{print $3}' | tr ' ' '\n' | sort -u | xargs) + echo "scenario_names: $changed_scenario_names" + for scenario_name in $changed_scenario_names; do + filtered_objects=$(echo "$matrix_array" | jq --arg scenario_value "$scenario_name" '. | map(select(.scenario_name == $scenario_value))') + scenario_matrix=$(echo "$scenario_matrix $filtered_objects" | jq -s 'flatten | unique') + done + fi + updated_matrix=$(echo "$module_matrix $scenario_matrix" | jq -s 'flatten | unique') + echo "updated_matrix: $updated_matrix" + updated_matrix="${updated_matrix//$'\n'/''}" + matrix_combinations="{\"include\": ${updated_matrix%?}}" + echo "matrix_combinations={\"include\": [${updated_matrix}]}" >> "$GITHUB_OUTPUT" + echo "matrix_combinations: $matrix_combinations" outputs: - matrix-combinations: ${{ steps.setup-matrix-scenarios.outputs.matrix_combinations }} + matrix-combinations: ${{ steps.setup-matrix-scenarios.outputs.matrix_combinations }} terraform-plan: permissions: read-all @@ -117,4 +117,4 @@ jobs: - name: Terraform Plan working-directory: ${{ env.TERRAFORM_MODULES_DIR }} - run: terraform plan -var-file "$TERRAFORM_INPUT_FILE" -var="json_input=$INPUT_JSON" + run: terraform plan -var-file "$TERRAFORM_INPUT_FILE" -var="json_input=$INPUT_JSON" \ No newline at end of file