Define Deployment Workflow
I'm currently focused on defining the deployment workflow. The goal is clear: create a GitHub Actions file for automated CD. I've started outlining the key steps. Thinking about triggers and actions. Considering various deployment strategies and environments.
Outlining the Actions
I'm now detailing the steps, envisioning the specific actions within the GitHub Actions workflow for the React app. It includes Node.js setup, dependency installation, building with yarn build, deploying to S3, and CloudFront invalidation. I've broken down each step, considering secrets management. I'm focusing on the trigger (push to main) and the runner (ubuntu-latest).
Drafting YAML Structure
I'm presently building the YAML file's skeleton, starting with the job definition and initial steps. I've included actions/checkout for the code and actions/setup-node specifying Node.js version 20 with yarn caching. Next, I'll add dependency installation using yarn install --frozen-lockfile to keep the lock file consistent. The focus now is on constructing the build step.
Detailing the S3 Deployment
I'm presently focusing on the deployment to S3. I've added a step using aws s3 sync to transfer the built React app to the specified bucket. This incorporates AWS credentials setup using aws-actions/configure-aws-credentials and a --delete flag to remove stale files. I'm considering using build as the directory but making a comment about ./dist in case of using Vite. I'm moving toward handling the CloudFront invalidation.
Implementing CloudFront
I've added the CloudFront invalidation step to the YAML, using the aws cloudfront create-invalidation command with the distribution ID secret and a path of /*. Now I'm focusing on refining the workflow. I'm revisiting permissions, specifically IAM policies for the AWS user, and determining which environment variables need to be passed during the build. While OIDC is more secure, I'll stick with access keys for simplicity, but I will make sure the YAML is well-formatted and easy to understand. The final stage is structuring the complete YAML output.
Documenting Secrets and Permissions
I'm now detailing the necessary GitHub Secrets, including AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, S3_BUCKET_NAME, and CLOUDFRONT_DISTRIBUTION_ID. I'm also specifying the required AWS IAM permissions with a JSON snippet, ensuring the workflow has appropriate access. I'll note the distinction between build and dist folders, making sure to comment on dist for Vite users, while defaulting to build as the target for React Scripts. Finally, I'll generate the complete YAML output.
Here is a complete GitHub Actions workflow file.
You should save this file in your repository at .github/workflows/deploy.yml.
name: Deploy React App to S3 and CloudFront
on:
push:
branches:
- main # Trigger on push to main branch
# Permissions required to interact with AWS via OIDC (if configured) or standard runner operations
permissions:
contents: read
jobs:
build-and-deploy:
name: Build and Deploy
runs-on: ubuntu-latest
steps:
# 1. Check out the repository code
- name: Checkout Code
uses: actions/checkout@v4
# 2. Set up Node.js environment
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20' # Use specific LTS version
cache: 'yarn' # Caches dependencies for faster builds
# 3. Install dependencies
- name: Install Dependencies
run: yarn install --frozen-lockfile
# 4. Build the React application
# Use secrets here if your React build requires environment variables (e.g. REACT_APP_API_URL)
- name: Build
run: yarn build
env:
CI: false # Prevents build failure on simple warnings
# 5. Configure AWS Credentials
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
# 6. Deploy to S3
# Note: If using Vite, change './build' to './dist'
- name: Sync files to S3
run: |
aws s3 sync ./build s3://${{ secrets.S3_BUCKET_NAME }} --delete
# 7. Invalidate CloudFront Cache
# This ensures users receive the latest version immediately
- name: Invalidate CloudFront
run: |
aws cloudfront create-invalidation \
--distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} \
--paths "/*"
Configuration Steps
To make this workflow work, you need to set up Secrets in your GitHub repository.
- Go to your GitHub Repository.
- Click Settings > Secrets and variables > Actions.
- Click New repository secret and add the following:
| Secret Name | Description |
|---|
AWS_ACCESS_KEY_ID | Your AWS IAM User Access Key. |
AWS_SECRET_ACCESS_KEY | Your AWS IAM User Secret Key. |
AWS_REGION | The AWS region where your bucket is (e.g., us-east-1). |
S3_BUCKET_NAME | The exact name of your S3 bucket (e.g., my-react-app-bucket). |
CLOUDFRONT_DISTRIBUTION_ID | The ID of your CloudFront distribution (e.g., E1234567890). |
Required AWS IAM Permissions
The AWS IAM User (whose keys you provide) needs a policy allowing S3 upload and CloudFront invalidation. You can attach this JSON policy to the IAM user:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "SyncToS3",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:ListBucket",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::YOUR-BUCKET-NAME",
"arn:aws:s3:::YOUR-BUCKET-NAME/*"
]
},
{
"Sid": "InvalidateCF",
"Effect": "Allow",
"Action": "cloudfront:CreateInvalidation",
"Resource": "arn:aws:cloudfront::YOUR-ACCOUNT-ID:distribution/YOUR-DIST-ID"
}
]
}
A Note on Folders (build vs dist)
- If you created your app with Create React App, the output folder is
./build (the script above uses this).
- If you created your app with Vite, the output folder is usually
./dist. You will need to update the "Sync files to S3" step to: aws s3 sync ./dist ...