Skip to content

Commit c1a591a

Browse files
authored
Update Baselines GitHub Action (#858)
1 parent 8d83375 commit c1a591a

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

.github/workflows/baselines.yml

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ on:
88
required: false
99
default: "false"
1010
type: string
11+
runType:
12+
description: "Run type: 'all' to run for all baseline folders, 'changed' to run for only changed baseline folders"
13+
required: false
14+
default: "changed"
15+
type: string
1116
push:
1217
branches:
1318
- main # Trigger only on pushes to the main branch
@@ -28,8 +33,6 @@ jobs:
2833

2934
- name: Setup Terraform
3035
uses: hashicorp/setup-terraform@v3
31-
# with:
32-
# terraform_version: 1.6.6
3336

3437
- name: "Configure AWS credentials for Remote State"
3538
id: configure-aws-creds
@@ -38,7 +41,7 @@ jobs:
3841
aws-region: ${{ secrets.BASELINES_AWS_REGION }}
3942
role-to-assume: ${{ secrets.BASELINES_IAM_ROLE_TO_ASSUME }}
4043
role-session-name: "baselines-role-for-ga"
41-
role-duration-seconds: 1200 # 20min
44+
role-duration-seconds: 900 # 15min
4245

4346
- name: Run Terraform
4447
env:
@@ -49,31 +52,34 @@ jobs:
4952
DYNAMODB_TABLE: ${{ secrets.BASELINES_DYNAMODB_TABLE }}
5053
AWS_REGION: ${{ secrets.BASELINES_AWS_REGION }}
5154
USER_PROFILE: ${{ secrets.BASELINES_USER_PROFILE }}
52-
5355
run: |
5456
5557
# Store the root directory
5658
root_dir=$(pwd)
5759
58-
# # Detect changes between the current branch and the main branch
59-
# if git rev-parse origin/main >/dev/null 2>&1; then
60-
# # Compare the current branch with the main branch
61-
# echo "Running git diff between the current branch and origin/main for baselines/*..."
62-
# changed_folders=$(git diff --name-only origin/main HEAD -- 'baselines/*' | xargs -n1 dirname | sort -u)
63-
# else
64-
# # If origin/main doesn't exist, it's likely the first commit
65-
# echo "Running git diff against empty tree for baselines/*..."
66-
# changed_folders=$(git diff --name-only $(git hash-object -t tree /dev/null) HEAD -- 'baselines/*' | xargs -n1 dirname | sort -u)
67-
# fi
68-
69-
# # Debug output: Show what was detected as changed folders
70-
# echo "Detected changed folders: $changed_folders"
71-
72-
# For the initial run, bypass git diff and force processing all folders
73-
echo "Processing all folders in baselines/* for the initial run..."
74-
changed_folders=$(find baselines -type d | sort -u)
75-
76-
# Debug output: Show what was detected as changed folders
60+
# Get the dryRun input (default is "false")
61+
dry_run="${{ github.event.inputs.dryRun }}"
62+
echo "Dry run mode: $dry_run"
63+
64+
# Get the run type input from workflow_dispatch or default to 'changed'
65+
run_type="${{ github.event.inputs.runType }}"
66+
echo "Run type: $run_type"
67+
68+
# Detect changed folders or run all folders based on runType
69+
if [ "$run_type" = "all" ]; then
70+
echo "Processing all folders in baselines/*..."
71+
changed_folders=$(find baselines -type d | sort -u)
72+
else
73+
if git rev-parse origin/main >/dev/null 2>&1; then
74+
echo "Running git diff between the current branch and origin/main for baselines/*..."
75+
changed_folders=$(git diff --name-only origin/main HEAD -- 'baselines/*' | xargs -n1 dirname | sort -u)
76+
else
77+
echo "Running git diff against empty tree for baselines/*..."
78+
changed_folders=$(git diff --name-only $(git hash-object -t tree /dev/null) HEAD -- 'baselines/*' | xargs -n1 dirname | sort -u)
79+
fi
80+
fi
81+
82+
# Debug output: Show what was detected as folders to process
7783
echo "Detected folders: $changed_folders"
7884
7985
# Check if changed_folders is truly empty or contains valid paths
@@ -110,24 +116,39 @@ jobs:
110116
EOF
111117
112118
terraform init
113-
119+
114120
# Check if the folder name ends with _mods
115121
if [[ "$folder_path" == *"_mods" ]]; then
116122
parallelism_flag="-parallelism=1"
117123
else
118124
parallelism_flag=""
119125
fi
120126
121-
# Check if var_value is provided
122-
if [ -n "$var_value" ]; then
123-
# If var_value ends with .tfvars, use --var-file, otherwise use -var
124-
if [[ "$var_value" == *.tfvars ]]; then
125-
terraform apply --var-file="$var_value" --auto-approve $parallelism_flag
127+
# Run plan or apply based on dryRun
128+
if [ "$dry_run" = "true" ]; then
129+
echo "Running terraform plan in $folder_path (dry run)..."
130+
if [ -n "$var_value" ]; then
131+
# If var_value ends with .tfvars, use --var-file, otherwise use -var
132+
if [[ "$var_value" == *.tfvars ]]; then
133+
terraform plan --var-file="$var_value" $parallelism_flag
134+
else
135+
terraform plan -var "$var_flag=$var_value" $parallelism_flag
136+
fi
126137
else
127-
terraform apply -var "$var_flag=$var_value" --auto-approve $parallelism_flag
138+
terraform plan $parallelism_flag
128139
fi
129140
else
130-
terraform apply --auto-approve $parallelism_flag
141+
echo "Running terraform apply in $folder_path..."
142+
if [ -n "$var_value" ]; then
143+
# If var_value ends with .tfvars, use --var-file, otherwise use -var
144+
if [[ "$var_value" == *.tfvars ]]; then
145+
terraform apply --var-file="$var_value" --auto-approve $parallelism_flag
146+
else
147+
terraform apply -var "$var_flag=$var_value" --auto-approve $parallelism_flag
148+
fi
149+
else
150+
terraform apply --auto-approve $parallelism_flag
151+
fi
131152
fi
132153
133154
# Return to the root directory
@@ -139,8 +160,8 @@ jobs:
139160
process_folder "baselines/guardrails/folder_hierarchy" "base_folder_name" "SandBox"
140161
process_folder "baselines/guardrails/turbot_profiles" "user_profile" "$USER_PROFILE"
141162
142-
# Process _mods folders if they had changes
143-
echo "Processing changed _mods folders first..."
163+
# Process _mods folders
164+
echo "Processing _mods folders..."
144165
for folder_path in $(echo "$changed_folders" | grep '_mods'); do
145166
if ls "$folder_path"/*.tf >/dev/null 2>&1; then
146167
if [ -f "$folder_path/default.tfvars" ]; then
@@ -152,7 +173,7 @@ jobs:
152173
done
153174
154175
# Process remaining folders
155-
echo "Processing remaining changed folders..."
176+
echo "Processing remaining folders..."
156177
for folder_path in $changed_folders; do
157178
# Skip already processed _mods folders and specific guardrails folders
158179
if [[ "$folder_path" == "baselines/guardrails/folder_hierarchy" || "$folder_path" == "baselines/guardrails/turbot_profiles" || "$folder_path" == *"_mods" ]]; then
@@ -167,4 +188,4 @@ jobs:
167188
process_folder "$folder_path"
168189
fi
169190
fi
170-
done
191+
done

0 commit comments

Comments
 (0)