8
8
required : false
9
9
default : " false"
10
10
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
11
16
push :
12
17
branches :
13
18
- main # Trigger only on pushes to the main branch
28
33
29
34
- name : Setup Terraform
30
35
uses : hashicorp/setup-terraform@v3
31
- # with:
32
- # terraform_version: 1.6.6
33
36
34
37
- name : " Configure AWS credentials for Remote State"
35
38
id : configure-aws-creds
38
41
aws-region : ${{ secrets.BASELINES_AWS_REGION }}
39
42
role-to-assume : ${{ secrets.BASELINES_IAM_ROLE_TO_ASSUME }}
40
43
role-session-name : " baselines-role-for-ga"
41
- role-duration-seconds : 1200 # 20min
44
+ role-duration-seconds : 900 # 15min
42
45
43
46
- name : Run Terraform
44
47
env :
@@ -49,31 +52,34 @@ jobs:
49
52
DYNAMODB_TABLE : ${{ secrets.BASELINES_DYNAMODB_TABLE }}
50
53
AWS_REGION : ${{ secrets.BASELINES_AWS_REGION }}
51
54
USER_PROFILE : ${{ secrets.BASELINES_USER_PROFILE }}
52
-
53
55
run : |
54
56
55
57
# Store the root directory
56
58
root_dir=$(pwd)
57
59
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
77
83
echo "Detected folders: $changed_folders"
78
84
79
85
# Check if changed_folders is truly empty or contains valid paths
@@ -110,24 +116,39 @@ jobs:
110
116
EOF
111
117
112
118
terraform init
113
-
119
+
114
120
# Check if the folder name ends with _mods
115
121
if [[ "$folder_path" == *"_mods" ]]; then
116
122
parallelism_flag="-parallelism=1"
117
123
else
118
124
parallelism_flag=""
119
125
fi
120
126
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
126
137
else
127
- terraform apply -var "$var_flag=$var_value" --auto-approve $parallelism_flag
138
+ terraform plan $parallelism_flag
128
139
fi
129
140
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
131
152
fi
132
153
133
154
# Return to the root directory
@@ -139,8 +160,8 @@ jobs:
139
160
process_folder "baselines/guardrails/folder_hierarchy" "base_folder_name" "SandBox"
140
161
process_folder "baselines/guardrails/turbot_profiles" "user_profile" "$USER_PROFILE"
141
162
142
- # Process _mods folders if they had changes
143
- echo "Processing changed _mods folders first ..."
163
+ # Process _mods folders
164
+ echo "Processing _mods folders..."
144
165
for folder_path in $(echo "$changed_folders" | grep '_mods'); do
145
166
if ls "$folder_path"/*.tf >/dev/null 2>&1; then
146
167
if [ -f "$folder_path/default.tfvars" ]; then
@@ -152,7 +173,7 @@ jobs:
152
173
done
153
174
154
175
# Process remaining folders
155
- echo "Processing remaining changed folders..."
176
+ echo "Processing remaining folders..."
156
177
for folder_path in $changed_folders; do
157
178
# Skip already processed _mods folders and specific guardrails folders
158
179
if [[ "$folder_path" == "baselines/guardrails/folder_hierarchy" || "$folder_path" == "baselines/guardrails/turbot_profiles" || "$folder_path" == *"_mods" ]]; then
@@ -167,4 +188,4 @@ jobs:
167
188
process_folder "$folder_path"
168
189
fi
169
190
fi
170
- done
191
+ done
0 commit comments