Skip to content

Commit 2a4e2ba

Browse files
committed
Validate and Construct sync Parameters
Why these changes are being introduced: First, we want to ensure that the only additional parameters supplied by the caller workflow are `--include` and `--exclude` parameters. Second, since we pass the parameters via an `env:` block, we have to be extra careful with double-quotes (which are required by the `aws s3 sync` command, but typically get stripped when expanded in a bash script). How these changes are implemented: * Add a block to the validation step to throw an error if there is any other parameter in the SYNC_PARAMS input outside of `--include` and `--exclude` * If the SYNC_PARAMS is valid, merge it together with the other stock `--exclude` parameters and set one string in the GITHUB_ENV with the full list of inclues and excludes * Add the `eval` command to properly expand the `aws s3 sync` command and preserve the double-quotes where they are required Side effects: None.
1 parent 98f7d25 commit 2a4e2ba

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

.github/workflows/cdn-shared-publish.yml

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ jobs:
6767
- name: Validate
6868
# Verify that the DOMAIN & ENVIRONMENT inputs are using the correct
6969
# values. Verify that the SOURCE_PATH and TARGET_PATH inputs are
70-
# formatted correctly.
70+
# formatted correctly and ensure this supports legacy caller workflows.
71+
# Validate the SYNC_PARAMS input to only allow `--exclude` and
72+
# `--include` parameters and then construct a single VALID_SYNC_PARAMS
73+
# environment variable with proper quoting for use in the sync step.
7174
id: validate
7275
env:
7376
DOMAIN: ${{ inputs.DOMAIN }}
7477
ENVIRONMENT: ${{ inputs.ENVIRONMENT }}
7578
SOURCE_PATH: ${{ inputs.SOURCE_PATH }}
7679
S3URI: ${{ inputs.S3URI }}
7780
TARGET_PATH: ${{ inputs.TARGET_PATH }}
81+
SYNC_PARAMS: ${{ inputs.SYNC_PARAMS }}
7882
run: |
7983
case "$DOMAIN" in
8084
standard|custom)
@@ -100,6 +104,7 @@ jobs:
100104
echo "Invalid SOURCE_PATH=$SOURCE_PATH, exiting."
101105
exit 1
102106
fi
107+
103108
if [[ "$S3URI" == "" ]]; then
104109
if [[ "${TARGET_PATH:0:1}" == "/" ]]; then
105110
echo "Valid TARGET_PATH=$TARGET_PATH, proceed."
@@ -108,7 +113,7 @@ jobs:
108113
exit 1
109114
fi
110115
else
111-
echo "Legacy caller workflow that passed ab S3_URI value."
116+
echo "Legacy caller workflow that passed an S3_URI value."
112117
if [[ "$DOMAIN" == "standard" ]]; then
113118
echo "LEGACY_TARGET_PATH=/$(echo "$S3URI" | awk -F/ '{print $5}')" >> $GITHUB_ENV
114119
echo "LEGACY_SOURCE_PATH=$(echo "$S3URI" | awk -F/ '{print $5}')" >> $GITHUB_ENV
@@ -118,6 +123,20 @@ jobs:
118123
echo "LEGACY=true" >> $GITHUB_ENV
119124
fi
120125
126+
if [[ -n "$SYNC_PARAMS" ]]; then
127+
temp_params="${SYNC_PARAMS//--include/}"
128+
temp_params="${temp_params//--exclude/}"
129+
# If there's still a -- in there, it's an invalid flag
130+
if [[ $temp_params =~ -- ]]; then
131+
echo "Invalid SYNC_PARAMS: only --include and --exclude parameters are allowed, exiting."
132+
exit 1
133+
fi
134+
echo "Valid SYNC_PARAMS, proceed."
135+
echo "VALID_SYNC_PARAMS=--exclude \".github/*\" --exclude \".git/*\" --exclude \".gitignore\" $SYNC_PARAMS" >> $GITHUB_ENV
136+
else
137+
echo "VALID_SYNC_PARAMS=--exclude \".github/*\" --exclude \".git/*\" --exclude \".gitignore\"" >> $GITHUB_ENV
138+
fi
139+
121140
- name: Set Environment
122141
# Prepare environment variables for the synchronization job.
123142
id: env
@@ -153,9 +172,9 @@ jobs:
153172
aws-region: ${{ inputs.AWS_REGION }}
154173
role-to-assume: ${{ env.AWS_ROLE }}
155174

156-
- name: Get AWS Information
175+
- name: Set S3 Target URI
157176
# Set the correct S3 URI for the synchronization job
158-
id: aws_info
177+
id: s3_target
159178
env:
160179
AWS_REGION: ${{ inputs.AWS_REGION }}
161180
DOMAIN: ${{ inputs.DOMAIN }}
@@ -184,7 +203,7 @@ jobs:
184203
env:
185204
S3_URI: ${{ env.S3_URI }}
186205
SOURCE_PATH: ${{ env.LEGACY && env.LEGACY_SOURCE_PATH || inputs.SOURCE_PATH }}
187-
SYNC_PARAMS: ${{ inputs.SYNC_PARAMS }}
206+
VALID_SYNC_PARAMS: ${{ env.VALID_SYNC_PARAMS }}
188207
run: |
189208
echo "### Content synchronization to $S3_URI." >> $GITHUB_STEP_SUMMARY
190209
if [[ "$S3_URI" == *"cdn/"* ]]; then
@@ -193,12 +212,9 @@ jobs:
193212
echo "Custom CDN content is synchronizing"
194213
fi
195214
cd "$GITHUB_WORKSPACE"
196-
aws s3 sync "$SOURCE_PATH" "$S3_URI" \
197-
--delete \
198-
--exclude ".github/*" \
199-
--exclude ".git/*" \
200-
--exclude ".gitignore" \
201-
$SYNC_PARAMS
215+
eval "aws s3 sync \"$SOURCE_PATH\" \"$S3_URI\" \
216+
--delete \
217+
$VALID_SYNC_PARAMS"
202218
echo "Content is synchronized to $S3_URI." >> $GITHUB_STEP_SUMMARY
203219
204220
- name: Invalidate cache

0 commit comments

Comments
 (0)