Skip to main content

Deploy to s3, simple bash script

I overwritten the endpoint S3_API_ENDPOINT in this example to manage Yandex Cloud with aws cli tool. This is legal approach :), described in Yandexx Cloud documentation.

#!/usr/bin/env bash

## Required environment variables
# $AWS_ACCESS_KEY_ID
# $AWS_SECRET_ACCESS_KEY
# $REGION
# $BUCKET_NAME

if [[ -z "$AWS_ACCESS_KEY_ID" ]]; then
# $var is empty, do what you want
echo "Environment variable \"AWS_ACCESS_KEY_ID\" is empty"
exit 1
fi
if [[ -z "$AWS_SECRET_ACCESS_KEY" ]]; then
# $var is empty, do what you want
echo "Environment variable \"AWS_SECRET_ACCESS_KEY\" is empty"
exit 1
fi
if [[ -z "$REGION" ]]; then
# $var is empty, do what you want
echo "Environment variable \"REGION\" is empty"
exit 1
fi
if [[ -z "$BUCKET_NAME" ]]; then
# $var is empty, do what you want
echo "Environment variable \"BUCKET_NAME\" is empty"
exit 1
fi

BUILD_DIR=./build/
S3_API_ENDPOINT=--endpoint-url=https://storage.yandexcloud.net/

rm -rf $BUILD_DIR

################
# your command to build assets
npm install
npm run build
################

if [ -d "$BUILD_DIR" ]
then
aws s3 rm --recursive s3://$BUCKET_NAME/ $S3_API_ENDPOINT
aws s3 cp --recursive $BUILD_DIR s3://$BUCKET_NAME/ $S3_API_ENDPOINT
else
echo "Directory \"$BUILD_DIR\" is not exists"
exit 1
fi