diff --git a/.env.sample b/.env.sample index 1ab23e8..9b4f3dd 100644 --- a/.env.sample +++ b/.env.sample @@ -5,6 +5,9 @@ AWS_ACCESS_KEY_ID=access-key-id AWS_SECRET_ACCESS_KEY=secret-access-key AWS_DEFAULT_REGION=us-east-1 +AWS_AMPLIFY_APP_ID=amplify-app-id +AWS_AMPLIFY_BRANCH_NAME=amplify-branch-name + # CUSTOM_REPLACE_KEYS and CUSTOM_REPLACE_VALUES must be comma seperated values and number of elements should be same. CUSTOM_REPLACE_KEYS="key_1", "key_2" CUSTOM_REPLACE_VALUES="value_1", "value_2" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..570bddb --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.env +.DS_Store +# Rubymine IDE files +.idea/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 8db7e70..ecc527e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM bash:5.1.16-alpine3.15 -RUN apk add --no-cache wget aws-cli +RUN apk add --no-cache zip curl jq wget aws-cli ENV GHOST_STATIC_CONTENT_DIR=/src/content diff --git a/README.md b/README.md index 62cf57e..422e79d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,29 @@ If your blog is hosted under `https://content.yourdomain.com` and you want to ho You can also replace certain text from the generated static files by passing the following arguments `custom_replace_keys` and `custom_replace_values`. For more details, refer Inputs and Example usage section. It doesn't support the multiline replacement as of now. -Additionally, it also provides the functionality to upload the static HTML files to S3 bucket. To access these files publicly, make it as a public bucket and enable static website hosting. +Optionally, you can either host the static files on AWS S3 or on AWS Amplify. + +To host Static Blog on AWS S3, provide the following input parameters: + +- `s3_bucket_name` (Make the bucket publicly accessible and enable static web hosting) + +- `aws_access_key_id` + +- `aws_secret_access_key` + +- `aws_region` + +To host Static Blog on already existing AWS Amplify application, provide the following input parameters: + +- `aws_amplify_app_id` + +- `aws_amplify_branch_name` + +- `aws_access_key_id` + +- `aws_secret_access_key` + +- `aws_region` ## Inputs @@ -52,6 +74,14 @@ Additionally, it also provides the functionality to upload the static HTML files **Optional** AWS region. +## `aws_amplify_app_id` + +**Optional** Amplify App id. + +## `aws_amplify_branch_name` + +**Optional** Amplify branch name. + ## Example usage ```yaml diff --git a/action.yml b/action.yml index 1aad0bc..c69b86e 100644 --- a/action.yml +++ b/action.yml @@ -34,6 +34,13 @@ inputs: aws_region: description: "AWS region" required: false + aws_amplify_app_id: + description: "Amplify App id" + required: false + aws_amplify_branch_name: + description: "Amplify Branch name" + required: false + runs: using: "docker" image: "Dockerfile" @@ -48,3 +55,5 @@ runs: - ${{ inputs.aws_access_key_id }} - ${{ inputs.aws_secret_access_key }} - ${{ inputs.aws_region }} + - ${{ inputs.aws_amplify_app_id }} + - ${{ inputs.aws_amplify_branch_name }} diff --git a/entrypoint.sh b/entrypoint.sh index 7d5a201..06ec52d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -11,6 +11,8 @@ if [[ $# -gt 0 && ${GITHUB_ACTIONS} -eq true ]]; then export AWS_ACCESS_KEY_ID=${INPUT_AWS_ACCESS_KEY_ID} export AWS_SECRET_ACCESS_KEY=${INPUT_AWS_SECRET_ACCESS_KEY} export AWS_DEFAULT_REGION=${INPUT_AWS_REGION} + export AWS_AMPLIFY_APP_ID=${INPUT_AWS_AMPLIFY_APP_ID} + export AWS_AMPLIFY_BRANCH_NAME=${INPUT_AWS_AMPLIFY_BRANCH_NAME} fi /src/run.sh diff --git a/run.sh b/run.sh index 57244e5..2a3eb0d 100755 --- a/run.sh +++ b/run.sh @@ -13,6 +13,8 @@ # - AWS_ACCESS_KEY_ID # - AWS_SECRET_ACCESS_KEY # - AWS_DEFAULT_REGION +# - AWS_AMPLIFY_APP_ID +# - AWS_AMPLIFY_BRANCH_NAME GHOST_HOSTED_DOMAIN_WITH_PATH=$(echo ${GHOST_HOSTED_URL} | cut -d '/' -f 3-) GHOST_HOSTED_BLOG_PATH=$(echo ${GHOST_HOSTED_URL} | cut -d '/' -f 4-) @@ -148,9 +150,62 @@ if [[ ! -z ${S3_BUCKET_NAME} ]]; then aws s3 sync ${blog_dir}/public ${s3_blog_path}/public --acl public-read --cache-control "public, max-age=604800, must-revalidate" --delete aws s3 sync ${blog_dir}/assets ${s3_blog_path}/assets --acl public-read --cache-control "public, max-age=604800, must-revalidate" --delete echo "***** S3 upload complete *****" +elif [[ ! -z ${AWS_AMPLIFY_APP_ID} ]]; then + echo "" + echo "***** Started generating zip for static blog *****" + cd ${GHOST_STATIC_CONTENT_DIR} + zip -r ${GHOST_STATIC_BLOG_PATH}.zip ${GHOST_STATIC_BLOG_PATH} + echo "***** Started create deployment for Amplify *****" + job=`aws amplify create-deployment --app-id ${AWS_AMPLIFY_APP_ID} --branch-name ${AWS_AMPLIFY_BRANCH_NAME} --output json` + # Extract values and store in variables + jobId=$(echo "$job" | jq -r '.jobId') + if [[ $? -ne 0 ]]; then + echo "Error: Unable to fetch jobId" + exit 1 + fi + zipUploadUrl=$(echo "$job" | jq -r '.zipUploadUrl') + if [[ $? -ne 0 ]]; then + echo "Error: Unable to fetch zipUploadUrl" + exit 1 + fi + echo "jobId ===>>>>> ${jobId}" + echo "zipUploadUrl ===>>>>> ${zipUploadUrl}" + curl "${zipUploadUrl}" --upload-file ${GHOST_STATIC_BLOG_PATH}.zip + if [[ $? -ne 0 ]]; then + echo "Error: Unable to upload zip" + exit 1 + fi + aws amplify start-deployment --app-id ${AWS_AMPLIFY_APP_ID} --branch-name ${AWS_AMPLIFY_BRANCH_NAME} --job-id ${jobId} + if [[ $? -ne 0 ]]; then + echo "Error: Unable to start deployment" + exit 1 + fi + while true; do + status=`aws amplify get-job --app-id ${AWS_AMPLIFY_APP_ID} --branch-name ${AWS_AMPLIFY_BRANCH_NAME} --job-id ${jobId} | jq -r '.job.summary.status'` + if [[ $? -ne 0 ]]; then + echo "Error: Unable to fetch status" + exit 1 + fi + # Check if the status is either "SUCCEED" or "FAILED" + if [ "${status}" = "SUCCEED" ]; then + echo "Job Status =>>>>>> ${status}" + break + elif [ "${status}" = "FAILED" ]; then + echo "Job Status =>>>>>> ${status}" + break + elif [ "${status}" = "CANCELLED" ]; then + echo "Job Status =>>>>>> ${status}" + break + else + echo "Job Status =>>>>>> ${status}" + fi + # Wait for a while before checking again + sleep 5 # Adjust the sleep duration as needed + done else echo " " echo "If you want to upload the static site files to S3, provide following ENV variables: S3_BUCKET_NAME, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION" + echo "Or if you want to upload the static site files to Amplify, provide following ENV variables: AWS_AMPLIFY_APP_ID, AWS_AMPLIFY_BRANCH_NAME, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION" fi echo " "