Skip to content

Commit a14748c

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 a14748c

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@ 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.
7173
id: validate
7274
env:
7375
DOMAIN: ${{ inputs.DOMAIN }}
7476
ENVIRONMENT: ${{ inputs.ENVIRONMENT }}
7577
SOURCE_PATH: ${{ inputs.SOURCE_PATH }}
7678
S3URI: ${{ inputs.S3URI }}
7779
TARGET_PATH: ${{ inputs.TARGET_PATH }}
80+
SYNC_PARAMS: ${{ inputs.SYNC_PARAMS }}
7881
run: |
7982
case "$DOMAIN" in
8083
standard|custom)
@@ -100,6 +103,7 @@ jobs:
100103
echo "Invalid SOURCE_PATH=$SOURCE_PATH, exiting."
101104
exit 1
102105
fi
106+
103107
if [[ "$S3URI" == "" ]]; then
104108
if [[ "${TARGET_PATH:0:1}" == "/" ]]; then
105109
echo "Valid TARGET_PATH=$TARGET_PATH, proceed."
@@ -108,7 +112,7 @@ jobs:
108112
exit 1
109113
fi
110114
else
111-
echo "Legacy caller workflow that passed ab S3_URI value."
115+
echo "Legacy caller workflow that passed an S3_URI value."
112116
if [[ "$DOMAIN" == "standard" ]]; then
113117
echo "LEGACY_TARGET_PATH=/$(echo "$S3URI" | awk -F/ '{print $5}')" >> $GITHUB_ENV
114118
echo "LEGACY_SOURCE_PATH=$(echo "$S3URI" | awk -F/ '{print $5}')" >> $GITHUB_ENV
@@ -118,6 +122,20 @@ jobs:
118122
echo "LEGACY=true" >> $GITHUB_ENV
119123
fi
120124
125+
if [[ -n "$SYNC_PARAMS" ]]; then
126+
temp_params="${SYNC_PARAMS//--include/}"
127+
temp_params="${temp_params//--exclude/}"
128+
# If there's still a -- in there, it's an invalid flag
129+
if [[ $temp_params =~ -- ]]; then
130+
echo "Invalid SYNC_PARAMS: only --include and --exclude parameters are allowed, exiting."
131+
exit 1
132+
fi
133+
echo "Valid SYNC_PARAMS, proceed."
134+
echo "VALID_SYNC_PARAMS=--exclude \".github/*\" --exclude \".git/*\" --exclude \".gitignore\" $SYNC_PARAMS" >> $GITHUB_ENV
135+
else
136+
echo "VALID_SYNC_PARAMS=--exclude \".github/*\" --exclude \".git/*\" --exclude \".gitignore\"" >> $GITHUB_ENV
137+
fi
138+
121139
- name: Set Environment
122140
# Prepare environment variables for the synchronization job.
123141
id: env
@@ -153,9 +171,9 @@ jobs:
153171
aws-region: ${{ inputs.AWS_REGION }}
154172
role-to-assume: ${{ env.AWS_ROLE }}
155173

156-
- name: Get AWS Information
174+
- name: Set S3 Target URI
157175
# Set the correct S3 URI for the synchronization job
158-
id: aws_info
176+
id: s3_target
159177
env:
160178
AWS_REGION: ${{ inputs.AWS_REGION }}
161179
DOMAIN: ${{ inputs.DOMAIN }}
@@ -184,7 +202,7 @@ jobs:
184202
env:
185203
S3_URI: ${{ env.S3_URI }}
186204
SOURCE_PATH: ${{ env.LEGACY && env.LEGACY_SOURCE_PATH || inputs.SOURCE_PATH }}
187-
SYNC_PARAMS: ${{ inputs.SYNC_PARAMS }}
205+
VALID_SYNC_PARAMS: ${{ env.VALID_SYNC_PARAMS }}
188206
run: |
189207
echo "### Content synchronization to $S3_URI." >> $GITHUB_STEP_SUMMARY
190208
if [[ "$S3_URI" == *"cdn/"* ]]; then
@@ -193,12 +211,9 @@ jobs:
193211
echo "Custom CDN content is synchronizing"
194212
fi
195213
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
214+
eval "aws s3 sync \"$SOURCE_PATH\" \"$S3_URI\" \
215+
--delete \
216+
$VALID_SYNC_PARAMS"
202217
echo "Content is synchronized to $S3_URI." >> $GITHUB_STEP_SUMMARY
203218
204219
- name: Invalidate cache

0 commit comments

Comments
 (0)