|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +source ./scripts/cloud-cli-utils.sh |
| 4 | + |
| 5 | +# Function to convert S3 ARN to S3 URI |
| 6 | +function convertArnToUri { |
| 7 | + local arn="$1" |
| 8 | + |
| 9 | + # Remove "arn:aws:s3:::" from the beginning of the ARN |
| 10 | + local stripped_arn="${arn#arn:aws:s3:::}" |
| 11 | + |
| 12 | + # Extract the bucket name and object key |
| 13 | + local bucket_name="${stripped_arn%%/*}" |
| 14 | + local object_key="${stripped_arn#*/}" |
| 15 | + |
| 16 | + # Create the S3 URI |
| 17 | + local s3_uri="s3://$bucket_name/$object_key" |
| 18 | + |
| 19 | + echo "$s3_uri" |
| 20 | +} |
| 21 | + |
| 22 | +function downloadS3Artifact { |
| 23 | + # Get temporary access for the account |
| 24 | + E2E_ROLE_NAME=CodebuildE2E |
| 25 | + E2E_PROFILE_NAME=AmplifyCLIE2EProd |
| 26 | + authenticate $E2E_ACCOUNT_PROD $E2E_ROLE_NAME "$E2E_PROFILE_NAME" |
| 27 | + echo "Fetching artifact location from build" |
| 28 | + s3_arn=$(aws codebuild batch-get-builds --profile="$E2E_PROFILE_NAME" --ids "$1" --region us-east-1 --query 'builds[0].artifacts.location') |
| 29 | + # Have to remove double quote for arn |
| 30 | + s3_object_uri=$(convertArnToUri ${s3_arn//\"/}) |
| 31 | + echo $s3_object_uri |
| 32 | + echo "Downloading objects from S3 bucket..." |
| 33 | + aws s3 cp $s3_object_uri $2 --recursive --profile="$E2E_PROFILE_NAME" |
| 34 | + echo "Download complete. Files are saved in: $2" |
| 35 | +} |
| 36 | + |
| 37 | +function playTestArtifact { |
| 38 | + # Check if an S3 object URI is provided |
| 39 | + if [ $# -eq 0 ]; then |
| 40 | + echo "Provide the code build id: $0 <code_build_id>" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + local code_build_id=$1 |
| 45 | + local temp_dir=$(mktemp -d) # Create a temporary directory |
| 46 | + |
| 47 | + trap "cleanup $temp_dir" SIGINT SIGTERM # Register cleanup function to handle Ctrl+C |
| 48 | + |
| 49 | + echo "Starting test artifact playback..." |
| 50 | + downloadS3Artifact "$code_build_id" "$temp_dir" |
| 51 | + |
| 52 | + |
| 53 | + local subfolders=("$temp_dir"/*/) |
| 54 | + if [ ${#subfolders[@]} -eq 1 ]; then |
| 55 | + cd "${subfolders[0]}" || exit 1 |
| 56 | + else |
| 57 | + cd "$temp_dir" || exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + # Spin up a local HTTP server |
| 61 | + echo "Starting local HTTP server from directory $(pwd)..." |
| 62 | + npx http-server -p 0 |
| 63 | + |
| 64 | + cleanup "$temp_dir" |
| 65 | +} |
| 66 | + |
| 67 | +function cleanup { |
| 68 | + echo "Cleaning up and deleting the temporary directory..." |
| 69 | + rm -rf "$1" |
| 70 | + echo "Temporary directory deleted. Exiting script." |
| 71 | +} |
| 72 | + |
| 73 | +playTestArtifact "$@" |
0 commit comments