Skip to content

Commit

Permalink
Added support of Static Hosting on AWS Amplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek Roy committed Aug 3, 2023
1 parent fd24217 commit 73b12f5
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
.DS_Store
# Rubymine IDE files
.idea/*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }}
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
55 changes: 55 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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-)
Expand Down Expand Up @@ -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 " "
Expand Down

0 comments on commit 73b12f5

Please sign in to comment.