Skip to content

Commit

Permalink
Merge pull request #3 from coyainsurance/feature/hooks
Browse files Browse the repository at this point in the history
Add pre/post hooks
  • Loading branch information
pecigonzalo authored Oct 4, 2019
2 parents 73d1457 + 58ae110 commit cf90638
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 2 deletions.
1 change: 1 addition & 0 deletions .buildkite/hooks/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Folder required for tests
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ Target file to zip the cache. Defaults to a hashed name in `/tmp`
### `load_settings`
If non-empty, will load s3 optimized configuration

### Hooks
Hooks can be disabled by setting `BUILDKITE_PLUGIN_S3_CACHE_HOOKS` to any value

#### pre-cache
Loads pre-cache hooks from `${BUILDKITE_BUILD_CHECKOUT_PATH}/.buildkite/hooks/pre-cache`

#### post-cache
Loads post-cache hooks from `${BUILDKITE_BUILD_CHECKOUT_PATH}/.buildkite/hooks/post-cache`

## License
MIT (see [LICENSE](LICENSE))
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.1.0
v1.2.0
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: '2'
version: "2"
services:
tests:
image: buildkite/plugin-tester
volumes:
- ".:/plugin:ro"
- "./tests/hooks:/plugin/.buildkite/hooks:ro"
5 changes: 5 additions & 0 deletions hooks/post-checkout
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ if [[ "${#paths[@]}" -gt 0 ]]; then

echo "--- :compression: Uncompressing cache with tar"
tar -xzf "${CACHE_FILE}"

if [[ -z "${BUILDKITE_PLUGIN_S3_CACHE_HOOKS:-}" && -f "${BUILDKITE_BUILD_CHECKOUT_PATH}/.buildkite/hooks/post-cache" ]]; then
echo "Running post-cache hook"
"${BUILDKITE_BUILD_CHECKOUT_PATH}/.buildkite/hooks/post-cache"
fi
else
echo "No cache found in ${S3_PATH}"
fi
Expand Down
6 changes: 6 additions & 0 deletions hooks/post-command
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
mapfile -t paths < <(plugin_read_list DIRECTORIES)

if [ ${#paths[@]} -gt 0 ]; then

if [[ -z "${BUILDKITE_PLUGIN_S3_CACHE_HOOKS:-}" && -f "$BUILDKITE_BUILD_CHECKOUT_PATH/.buildkite/hooks/pre-cache" ]]; then
echo "Running pre-cache hook"
"$BUILDKITE_BUILD_CHECKOUT_PATH/.buildkite/hooks/pre-cache"
fi

echo "--- :compression: Compressing cache with tar"
# Filter existing paths
shopt -s extglob
Expand Down
2 changes: 2 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ load '/usr/local/lib/bats/load.bash'
export BUILDKITE_PLUGIN_S3_CACHE_BUCKET="test"
export BUILDKITE_PLUGIN_S3_CACHE_DIRECTORIES="./cache"
export BUILDKITE_LABEL="This test build"
export BUILDKITE_BUILD_CHECKOUT_PATH="$PWD"

stub aws \
"s3 ls s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : echo 48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz" \
Expand All @@ -38,6 +39,7 @@ load '/usr/local/lib/bats/load.bash'
export BUILDKITE_PLUGIN_S3_CACHE_DIRECTORIES="tests/"
export BUILDKITE_LABEL="This test build"
export BUILDKITE_PLUGIN_S3_CACHE_FILE="/tmp/cache.tar.gz"
export BUILDKITE_BUILD_CHECKOUT_PATH="$PWD"

stub tar \
"-zcf : touch /tmp/cache.tar.gz"
Expand Down
89 changes: 89 additions & 0 deletions tests/hooks.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env bats

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

# Uncomment to enable stub debug output:
# export AWS_STUB_DEBUG=/dev/tty
# export TAR_STUB_DEBUG=/dev/tty

@test "Pre-cache hooks" {
export BUILDKITE_PLUGIN_S3_CACHE_LOAD_SETTINGS=""
export BUILDKITE_PIPELINE_SLUG="test"
export BUILDKITE_BRANCH=master
export BUILDKITE_PLUGIN_S3_CACHE_BUCKET="test"
export BUILDKITE_PLUGIN_S3_CACHE_DIRECTORIES="tests/"
export BUILDKITE_LABEL="This test build"
export BUILDKITE_PLUGIN_S3_CACHE_FILE="/tmp/cache.tar.gz"
export BUILDKITE_BUILD_CHECKOUT_PATH="$PWD"

stub tar \
"-zcf : touch /tmp/cache.tar.gz"

stub aws \
"s3 cp /tmp/cache.tar.gz s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : "

run "$PWD/hooks/post-command"

assert_success
assert_output --partial "Running pre-cache hook"
assert_output --partial "Im pre-cache"
assert_output --partial "Compressing cache with tar"
assert_output --partial "Uploading cache to"
unstub tar
unstub aws
}

@test "Post-cache hooks" {
export BUILDKITE_PLUGIN_S3_CACHE_LOAD_SETTINGS=""
export BUILDKITE_PIPELINE_SLUG="test"
export BUILDKITE_BRANCH=master
export BUILDKITE_PLUGIN_S3_CACHE_BUCKET="test"
export BUILDKITE_PLUGIN_S3_CACHE_DIRECTORIES="./cache"
export BUILDKITE_LABEL="This test build"
export BUILDKITE_BUILD_CHECKOUT_PATH="$PWD"

stub aws \
"s3 ls s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : echo 48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz" \
"s3 ls s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : echo 48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz" \
"s3 cp s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz /tmp/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : "
stub tar \
"-xzf : "

run "$PWD/hooks/post-checkout"

assert_success
assert_output --partial "Downloading cache from"
assert_output --partial "Uncompressing cache"
assert_output --partial "Running post-cache hook"
assert_output --partial "Im post-cache"
unstub aws
unstub tar
}

@test "Cache hooks disable" {
export BUILDKITE_PLUGIN_S3_CACHE_LOAD_SETTINGS=""
export BUILDKITE_PIPELINE_SLUG="test"
export BUILDKITE_BRANCH=master
export BUILDKITE_PLUGIN_S3_CACHE_BUCKET="test"
export BUILDKITE_PLUGIN_S3_CACHE_DIRECTORIES="./cache"
export BUILDKITE_LABEL="This test build"
export BUILDKITE_BUILD_CHECKOUT_PATH="$PWD"
export BUILDKITE_PLUGIN_S3_CACHE_HOOKS="asd"

stub aws \
"s3 ls s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : echo 48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz" \
"s3 ls s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : echo 48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz" \
"s3 cp s3://test/test/master/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz /tmp/48ff2c8af2cb1b7b760080c23e4e1c92be6ee3cefef0ad8866d1d45b4485b195.tar.gz : "
stub tar \
"-xzf : "

run "$PWD/hooks/post-checkout"

assert_success
assert_output --partial "Downloading cache from"
assert_output --partial "Uncompressing cache"
refute_output --partial "Running post-cache hook"
refute_output --partial "Im post-cache"
unstub aws
unstub tar
}
4 changes: 4 additions & 0 deletions tests/hooks/post-cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -euo pipefail

echo "Im post-cache"
4 changes: 4 additions & 0 deletions tests/hooks/pre-cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -euo pipefail

echo "Im pre-cache"

0 comments on commit cf90638

Please sign in to comment.