In this tutorial, we'll walk through the process of deploying a static website to AWS S3 using GitHub Actions. We'll set up an S3 bucket, configure it for static website hosting, and then create a GitHub Actions workflow to automatically deploy changes to the S3 bucket whenever changes are pushed to the repository.
Prerequisites
Before we begin, make sure you have the following:
An AWS account with permissions to create S3 buckets and IAM roles.
A GitHub account.
Step 1: Create an S3 Bucket
Log in to your AWS Management Console.
Navigate to the S3 service.
Click on "Create bucket".
Provide a unique name for your bucket and choose the region.
Leave other settings as default and proceed to create the bucket.
Step 2: Configure Bucket Policy
Once the bucket is created, click on it to open its properties.
Go to the "Permissions" tab and click on "Bucket Policy".
Paste the following bucket policy, replacing
YOUR_BUCKET_NAME
with your actual bucket name:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
}
]
}
- Click "Save" to apply the policy.
Step 3: Configure Static Website Hosting
Still in the bucket properties, navigate to the "Static website hosting" tab.
Click on "Edit".
Enter
index.html
in the "Index document" field.Click "Save".
Step 4: Set Up GitHub Actions Workflow
Go to your GitHub repository where your static website code resides.
Click on the "Actions" tab.
Click on "Create new workflow" and choose "Set up a workflow yourself".
Paste the following workflow code into the editor:
name: Portfolio Deployment
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Deploy static site to S3 bucket
run: aws s3 sync . s3://YOUR_BUCKET_NAME --delete
Replace
YOUR_BUCKET_NAME
with the name of your S3 bucket.Click on "Start commit" and then "Commit new file" to save the workflow file.
Step 5: Test Deployment
Make any changes to your website code locally.
Commit and push the changes to your GitHub repository.
Go to the "Actions" tab on GitHub to monitor the workflow execution.
Once the workflow completes successfully, visit your S3 bucket URL to see the deployed website.
Any further changes made to the repository will trigger the workflow automatically, updating the deployed website accordingly.
Congratulations! You have successfully set up automated deployment of your static website to AWS S3 using GitHub Actions.
Follow me on LinkedIn.
Checkout my GitHub profile.