Skip to content

Commit

Permalink
Feature: Add debug flag support for AWS CLI in CloudFront invalidatio…
Browse files Browse the repository at this point in the history
…n plugin
  • Loading branch information
excessivedemon committed Aug 28, 2024
1 parent e6572a7 commit a0238fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ steps:
distribution-id: <cloudfront-distribution-id>
paths:
- <path/files/to/be/invalidated>
debug: true
```
## Configuration
Expand All @@ -26,6 +27,10 @@ The id of the Cloudfront distribution to create an invalidation for.

One or more [invalidation paths].

### `debug`

Adds the `--debug` flag to all AWS CLI commands, providing detailed output for troubleshooting.

## Development

To run the tests:
Expand Down
13 changes: 10 additions & 3 deletions hooks/post-command
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ if [[ ${BUILDKITE_COMMAND_EXIT_STATUS:-0} != '0' ]]; then
fi

distribution_id="$(plugin_read_config DISTRIBUTION_ID)"
debug="$(plugin_read_config DEBUG false)"

# Add the --debug flag to the AWS CLI command if debug is enabled
aws_debug_flag=""
if [[ "$debug" == "true" ]]; then
aws_debug_flag="--debug"
fi

# This tries to create an invalidation with an empty path list
#
# - If we have permission to call create-invalidation for this distribution,
# we'll recieve an InvalidArgument error, and we can proceed with a real invalidation
# we'll receive an InvalidArgument error, and we can proceed with a real invalidation
# - Otherwise, either an AccessDenied or NoSuchDistribution error are the most likely results,
# in which case we exit with error
if ! aws cloudfront create-invalidation --distribution-id "$distribution_id" --invalidation-batch 'Paths={Quantity=0,Items=[]},CallerReference=cloudfront-invalidation-buildkite-plugin' 2>&1 | grep InvalidArgument > /dev/null; then
if ! aws cloudfront create-invalidation --distribution-id "$distribution_id" --invalidation-batch 'Paths={Quantity=0,Items=[]},CallerReference=cloudfront-invalidation-buildkite-plugin' $aws_debug_flag 2>&1 | grep InvalidArgument > /dev/null; then
echo "Unable to invalidate cloudfront cache: create-invalidation not possible for distribution ($distribution_id) with current credentials"
exit 1
fi
Expand All @@ -31,7 +38,7 @@ done <<< "$(plugin_read_list PATHS)"
echo "~~~ :cloudfront: Creating Cloudfront invalidation for distribution $distribution_id on paths" "${paths[@]}"
SLEEP_PERIOD=15
PERIOD_LIMIT=480
until aws cloudfront create-invalidation --distribution-id "$distribution_id" --paths "${paths[@]}" --query Invalidation.Id --output text
until aws cloudfront create-invalidation --distribution-id "$distribution_id" --paths "${paths[@]}" --query Invalidation.Id --output text $aws_debug_flag
do
if [ $SLEEP_PERIOD == $PERIOD_LIMIT ]; then
echo "Maximum retries reached - giving up..."
Expand Down
21 changes: 20 additions & 1 deletion tests/post-command.bats
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bats

load '/usr/local/lib/bats/load.bash'
bats_load_library load.bash

# Uncomment the following to get more detail on failures of stubs
# export AWS_STUB_DEBUG=/dev/tty
Expand Down Expand Up @@ -87,10 +87,29 @@ load '/usr/local/lib/bats/load.bash'

@test "Stops executing if a credential problem is detected" {
export BUILDKITE_COMMAND_EXIT_STATUS=0
export BUILDKITE_PLUGIN_AWS_CLOUDFRONT_INVALIDATION_DISTRIBUTION_ID=test_id

stub aws "cloudfront create-invalidation --distribution-id test_id --invalidation-batch 'Paths={Quantity=0,Items=[]},CallerReference=cloudfront-invalidation-buildkite-plugin' : echo AccessDenied" \

run $PWD/hooks/post-command

assert_failure
unstub aws
}

@test "AWS CLI includes --debug flag when DEBUG=true" {
export BUILDKITE_COMMAND_EXIT_STATUS=0
export BUILDKITE_PLUGIN_AWS_CLOUDFRONT_INVALIDATION_DEBUG=true
export BUILDKITE_PLUGIN_AWS_CLOUDFRONT_INVALIDATION_DISTRIBUTION_ID=test_id
export BUILDKITE_PLUGIN_AWS_CLOUDFRONT_INVALIDATION_PATHS_0=/something/*

stub aws \
"cloudfront create-invalidation --distribution-id test_id --invalidation-batch 'Paths={Quantity=0,Items=[]},CallerReference=cloudfront-invalidation-buildkite-plugin' --debug : echo 'InvalidArgument'" \
"cloudfront create-invalidation --distribution-id test_id --paths /something/* --query Invalidation.Id --output text --debug : echo 'cloudfront invalidated with --debug'"

run $PWD/hooks/post-command

assert_success
assert_output --partial "--debug"
unstub aws
}

0 comments on commit a0238fd

Please sign in to comment.