Skip to content

Commit 2c9b7e0

Browse files
committed
feat: add multi-group support to terraform.sh
The -g/--group flag (also --groups) now accepts comma-delimited values, e.g. -g dev,live, loading each group tfvars file in order. Uses IFS-safe read -ra parsing and iterates with a proper array loop, safe under set -uo pipefail with empty input. Closes #41.
1 parent fe55ecd commit 2c9b7e0

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 2.3.2 (24/04/2026)
2+
3+
FEATURES:
4+
5+
* terraform.sh: added multi-group support. The `-g`/`--group` flag (also
6+
`--groups`) now accepts comma-delimited values, e.g. `-g dev,live`, loading
7+
each group's tfvars file in order. Closes #41.
8+
19
## 2.3.1 (24/04/2026)
210

311
FEATURES:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bin/terraform.sh \
118118
-b/--bucket-prefix `bucket_prefix` \
119119
-c/--component `component_name` \
120120
-e/--environment `environment` \
121-
-g/--group `group` (optional) \
121+
-g/--group/--groups `group` (optional, comma-delimited for multiple) \
122122
-i/--build-id `build_id` (optional) \
123123
-l/--lockfile `mode` (optional) \
124124
-p/--project `project` \
@@ -145,7 +145,7 @@ Where:
145145
* It is usual to provide, for example, the Jenkins _$BUILD_ID_ parameter to Plan jobs, and then manually reference that particular Job ID when running a corresponding apply job.
146146
* `component_name`: The name of the terraform component in the components directory to run the `action` against.
147147
* `environment`: The name of the environment the component is to be actioned against, therefore implying the variables file(s) to be included
148-
* `group` (optional): The name of the group to which the environment belongs, permitting the use of a group tfvars file as a "meta-environment" shared by more than one environment
148+
* `group` (optional): The name of the group(s) to which the environment belongs, permitting the use of group tfvars files as "meta-environments" shared by more than one environment. Supports comma-delimited values for multiple groups, e.g. `-g dev,live`
149149
* `lockfile` (optional): Passes the given lockfile mode to terraform.
150150
* `project`: The name of the project being deployed, as per the default bucket-prefix and state file keyspace
151151
* `region` (optional): The AWS region name unique to all components and terraform processes. Defaults to the value of the _AWS_DEFAULT_REGION_ environment variable.

bin/terraform.sh

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Usage: ${0} \\
3838
-b/--bucket-prefix [bucket_prefix] \\
3939
-c/--component [component_name] \\
4040
-e/--environment [environment] \\
41-
-g/--group [group]
41+
-g/--group [group] (optional, comma-delimited for multiple) \
4242
-i/--build-id [build_id] (optional) \\
4343
-l/--lockfile [mode] \\
4444
-p/--project [project] \\
@@ -80,10 +80,12 @@ environment:
8080
- prod
8181
- management
8282
83-
group:
83+
group (optional):
84+
Supports comma-delimited values for multiple groups, e.g. "dev,live"
8485
- dev
8586
- live
8687
- mytestgroup
88+
- dev,live
8789
8890
project:
8991
- The name of the project being deployed
@@ -134,7 +136,7 @@ fi
134136
readonly raw_arguments="${*}";
135137
ARGS=$(getopt \
136138
-o dhjntvwa:b:c:e:g:i:l:p:r: \
137-
-l 'help,version,bootstrap,action:,bucket-prefix:,build-id:,component:,environment:,group:,project:,region:,lockfile:,detailed-exitcode,lock-table,no-color,compact-warnings,disable-output-json' \
139+
-l 'help,version,bootstrap,action:,bucket-prefix:,build-id:,component:,environment:,group:,groups:,project:,region:,lockfile:,detailed-exitcode,lock-table,no-color,compact-warnings,disable-output-json' \
138140
-n "${0}" \
139141
-- \
140142
"${@}");
@@ -201,7 +203,7 @@ while true; do
201203
shift;
202204
fi;
203205
;;
204-
-g|--group)
206+
-g|--group|--groups)
205207
shift;
206208
if [ -n "${1}" ]; then
207209
group="${1}";
@@ -543,10 +545,10 @@ readonly global_vars_file_path="${base_path}/etc/${global_vars_file_name}";
543545
readonly region_vars_file_name="${region}.tfvars";
544546
readonly region_vars_file_path="${base_path}/etc/${region_vars_file_name}";
545547

546-
# Check for presence of a group variables file if specified, and use it if readable
548+
# Parse comma-delimited group into an array
549+
declare -a groups=();
547550
if [ -n "${group}" ]; then
548-
readonly group_vars_file_name="group_${group}.tfvars";
549-
readonly group_vars_file_path="${base_path}/etc/${group_vars_file_name}";
551+
IFS=',' read -ra groups <<< "${group}";
550552
fi;
551553

552554
# Collect the paths of the variables files to use
@@ -559,18 +561,20 @@ declare -a tf_var_file_paths;
559561
[ -f "${global_vars_file_path}" ] && tf_var_file_paths+=("${global_vars_file_path}");
560562
[ -f "${region_vars_file_path}" ] && tf_var_file_paths+=("${region_vars_file_path}");
561563

562-
# If a group has been specified, load the vars for the group. If we are to assume
564+
# If group(s) have been specified, load the vars for each group. If we are to assume
563565
# terraform correctly handles override-ordering (which to be fair we don't hence
564566
# the warning about duplicate variables below) we add this to the list after
565567
# global and region-global variables, but before the environment variables
566-
# so that the environment can explicitly override variables defined in the group.
567-
if [ -n "${group}" ]; then
568+
# so that the environment can explicitly override variables defined in the group(s).
569+
for group_name in "${groups[@]}"; do
570+
declare group_vars_file_name="group_${group_name}.tfvars";
571+
declare group_vars_file_path="${base_path}/etc/${group_vars_file_name}";
568572
if [ -f "${group_vars_file_path}" ]; then
569573
tf_var_file_paths+=("${group_vars_file_path}");
570574
else
571-
echo -e "[WARNING] Group \"${group}\" has been specified, but no group variables file is available at ${group_vars_file_path}";
575+
echo -e "[WARNING] Group \"${group_name}\" has been specified, but no group variables file is available at ${group_vars_file_path}";
572576
fi;
573-
fi;
577+
done;
574578

575579
# Environment is normally expected, but in bootstrapping it may not be provided
576580
if [ -n "${environment}" ]; then

0 commit comments

Comments
 (0)