Skip to content

Commit c4590fd

Browse files
authored
Allows the cache control to be set when deploying to an s3 bucket (#74)
1 parent b8e5c6c commit c4590fd

5 files changed

Lines changed: 38 additions & 1 deletion

File tree

.github/actions/test-deploy-to-s3-bucket/action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ runs:
4545
pattern: 'archive.tgz'
4646
s3-bucket: shopsmart-github-actions-tests
4747
s3-bucket-path: ${{ steps.id.outputs.id }}
48+
cache-control: max-age=3600
4849

4950
- name: 'Validate'
5051
shell: bash
5152
run: bats --tap --verbose-run ${{ github.action_path }}/deploy-to-s3-bucket.bats
5253
env:
5354
S3_BUCKET: shopsmart-github-actions-tests
5455
S3_BUCKET_PATH: ${{ steps.id.outputs.id }}
56+
CACHE_CONTROL: max-age=3600

.github/actions/test-deploy-to-s3-bucket/deploy-to-s3-bucket.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ function teardown() {
3535
[ "$status" -eq 0 ]
3636
[[ "$output" =~ $S3_TAG ]]
3737
}
38+
39+
40+
@test "it should have set the cache-control metadata" {
41+
run aws s3api head-object --no-cli-pager \
42+
--bucket "$S3_BUCKET" \
43+
--key "$S3_BUCKET_PATH/style.css" \
44+
--query 'CacheControl' \
45+
--output text
46+
47+
[ "$status" -eq 0 ]
48+
[ "$output" = "$CACHE_CONTROL" ]
49+
}

actions/deploy-to-s3-bucket/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ inputs:
3434
owner=carl
3535
default: ''
3636

37+
cache-control:
38+
description: 'Sets the cache-control headers for the content being uploaded'
39+
default: ''
40+
3741
runs:
3842
using: 'composite'
3943
steps:
@@ -58,6 +62,7 @@ runs:
5862
env:
5963
S3_BUCKET: ${{ inputs.s3-bucket }}
6064
S3_BUCKET_PATH: ${{ inputs.s3-bucket-path }}
65+
CACHE_CONTROL: ${{ inputs.cache-control }}
6166

6267
- name: 'Tag assets'
6368
shell: bash

actions/deploy-to-s3-bucket/upload-assets.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function setup() {
1212

1313
function teardown() {
1414
rm -f "$AWS_CMD_FILE"
15+
16+
unset CACHE_CONTROL
1517
}
1618

1719
function aws() {
@@ -35,3 +37,13 @@ function aws() {
3537
[ -f "$AWS_CMD_FILE" ]
3638
[[ "$(< "$AWS_CMD_FILE")" == s3\ cp\ --recursive\ my-path/\ s3://my-s3-bucket/my-s3-path/ ]]
3739
}
40+
41+
@test "it should set the cache-control" {
42+
export CACHE_CONTROL='max-age=36000'
43+
44+
run upload-assets my-path
45+
46+
[ "$status" -eq 0 ]
47+
[ -f "$AWS_CMD_FILE" ]
48+
[[ "$(< "$AWS_CMD_FILE")" == s3\ cp\ --recursive\ my-path/\ s3://my-s3-bucket/\ --cache-control\ max-age=36000 ]]
49+
}

actions/deploy-to-s3-bucket/upload-assets.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ function upload-assets() {
88
local s3_path="$S3_BUCKET"
99
[ -z "${S3_BUCKET_PATH:-}" ] || s3_path+="/$S3_BUCKET_PATH"
1010

11+
local args=()
12+
13+
if [ -n "${CACHE_CONTROL:-}" ]; then
14+
args+=(--cache-control "$CACHE_CONTROL")
15+
fi
16+
1117
echo "[DEBUG] Copying $path/ to s3://$s3_path/" >&2
12-
aws s3 cp --recursive "$path/" "s3://$s3_path/"
18+
aws s3 cp --recursive "$path/" "s3://$s3_path/" "${args[@]}"
1319
}
1420

1521
if [ "${BASH_SOURCE[0]}" = "$0" ]; then

0 commit comments

Comments
 (0)