Skip to content

Commit

Permalink
Merge pull request #7 from Automattic/add/artifact-support
Browse files Browse the repository at this point in the history
Add artifact support
  • Loading branch information
jkmassel authored Oct 11, 2021
2 parents e63eb2a + 957b23f commit 204cd14
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
39 changes: 39 additions & 0 deletions bin/download_artifact
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash -eu

# Usage
# download_artifact $file_name [$destination]
#
# $file_name should be the name of the file - it'll automatically be combined with the current build ID to differentiate between
# the same file in different jobs. It'll be identical to the `basename` of whatever you passed to `store_artifact`.
#
# $destination is an optional argument – by default, `download_artifact` will download the artifact to the present working directory,
# but if you'd like it stored elsewhere, just pass the path as the second argument.

ARTIFACT_FILE_NAME=${1-}

if [ -z "$ARTIFACT_FILE_NAME" ]; then
echo "You must pass the name of the file you want to download"
exit 1
fi

BUCKET=${ARTIFACTS_S3_BUCKET-}

if [ -z "$BUCKET" ]; then
echo "You must pass set the \`ARTIFACTS_S3_BUCKET\` environment variable with the S3 bucket you'd like to use"
exit 2
fi

OUTPUT_PATH=${2-.}

KEY="$BUILDKITE_BUILD_ID/$ARTIFACT_FILE_NAME"

# If the bucket has transfer acceleration enabled, use it!
ACCELERATION_STATUS=$(aws s3api get-bucket-accelerate-configuration --bucket $BUCKET | jq '.Status' -r || true)

if [ "$ACCELERATION_STATUS" = "Enabled" ]; then
echo "Downloading with transfer acceleration"
aws s3 cp "s3://$BUCKET/$KEY" $OUTPUT_PATH --endpoint-url https://s3-accelerate.amazonaws.com
else
aws s3 cp "s3://$BUCKET/$KEY" $OUTPUT_PATH
fi

40 changes: 40 additions & 0 deletions bin/upload_artifact
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash -eu

# Usage
# store_artifact $file_path
#
# $file_path is the path to a file on disk. It'll automatically be combined with the current build ID to differentiate between
# the same file in different jobs so that it can be correctly re-downloaded within the same job.

ARTIFACT_PATH=$1

if [ -z "$ARTIFACT_PATH" ]; then
echo "You must pass the file you want to be stored"
exit 1
fi

if [ ! -f "$ARTIFACT_PATH" ]; then
echo "No file found at $ARTIFACT_PATH"
exit 2
fi

BUCKET=${ARTIFACTS_S3_BUCKET-}

if [ -z "$BUCKET" ]; then
echo "You must pass set the \`ARTIFACTS_S3_BUCKET\` environment variable with the S3 bucket you'd like to use"
exit 3
fi

BASENAME=$(basename $ARTIFACT_PATH)
KEY="$BUILDKITE_BUILD_ID/$BASENAME"

# If the bucket has transfer acceleration enabled, use it!
ACCELERATION_STATUS=$(aws s3api get-bucket-accelerate-configuration --bucket $BUCKET | jq '.Status' -r || true)

if [ "$ACCELERATION_STATUS" = "Enabled" ]; then
echo "Uploading with transfer acceleration"
aws s3 cp $ARTIFACT_PATH "s3://$BUCKET/$KEY" --endpoint-url https://s3-accelerate.amazonaws.com
else
aws s3 cp $ARTIFACT_PATH "s3://$BUCKET/$KEY"
fi

0 comments on commit 204cd14

Please sign in to comment.