The user wants a GitHub Actions workflow file for a specific CI/CD pipeline.
The pipeline needs to do the following:
Set up Node.js: Install Node.js to run the build tools.
Build a React app: Use yarn build to create the production assets.
Deploy to S3: Upload the build artifacts to an AWS S3 bucket.
Invalidate CloudFront: Clear the CloudFront cache so the new assets are served.
Key Components needed in the YAML file:
Triggers (on): Usually push to main or master.
Permissions (permissions): Needs permission to write id-token if using OIDC (recommended), or just standard permissions for the GITHUB_TOKEN if not doing anything special. But for AWS, OIDC is the modern best practice. I should probably provide the OIDC version as it's more secure, but I need to make sure the user knows they need to set up the IAM Identity Provider. Alternatively, I can use standard secrets (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) which is easier for beginners to understand immediately, though less secure.
Decision: I will use the standard secret approach as the default example because it's the most common "quick start", but I'll add a note about OIDC being better for production.
Step 2: Setup Node. specify version (e.g., 18.x) and cache ('yarn').
Step 3: Install. yarn install.
Step 4: Build. yarn build. Note that the output folder is usually build or dist (Vite). I will assume build (standard CRA) but mention how to change it.
Step 5: AWS Credentials. Needs secrets.
Step 6: S3 Sync. Sync ./build to s3://....
Step 7: CloudFront Invalidation.
Refining the AWS Credentials Step:
I will use aws-actions/configure-aws-credentials.
Inputs needed: aws-access-key-id, aws-secret-access-key, aws-region.
Refining the S3 Sync Step:
Command: aws s3 sync ./build s3://$S3_BUCKET --delete--delete removes files in the bucket that are no longer in the build folder.
This looks solid. I will present this with clear instructions on how to configure the secrets. I will also briefly mention OIDC as a "Pro Tip".
Here is a complete, production-ready GitHub Actions workflow file.
This workflow triggers on pushes to the main branch, sets up Node.js, installs dependencies using Yarn, builds the app, and syncs the output to an S3 bucket. Finally, it invalidates the CloudFront cache so changes are live immediately.
1. The Workflow File
Save this file as .github/workflows/deploy.yml in your repository.
yaml
name: Deploy React App to S3
on:
push:
branches:
- main
# Optional: Trigger manually from the Actions tab
workflow_dispatch:
# Permission required for OIDC authentication with AWS (Recommended)
# If using standard Access Keys, this block is optional but good practice.
permissions:
id-token: write
contents: read
jobs:
build-and-deploy:
runs-on: ubuntu-latest
# Environment variables for the build and deployment
env:
# Your React build output folder (default for CRA is 'build', Vite is 'dist')
BUILD_DIR: build
# The S3 bucket name
S3_BUCKET: your-s3-bucket-name-here
# The CloudFront Distribution ID
CF_DISTRIBUTION_ID: your-cloudfront-distribution-id-here
# AWS Region
AWS_REGION: us-east-1
steps:
# 1. Checkout the code
- name: Checkout Repository
uses: actions/checkout@v4
# 2. Setup Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20' # Use your project's Node version
cache: 'yarn' # Enables caching for Yarn dependencies
# 3. Install dependencies
- name: Install Dependencies
run: yarn install --frozen-lockfile
# 4. Build the React App
- name: Build App
run: yarn build
# 5. Configure AWS Credentials
# Using the standard Access Key method for simplicity.
# See the "Security Note" below for the OIDC alternative.
- 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: ${{ env.AWS_REGION }}
# 6. Sync to S3
# --delete removes files in S3 that are no longer in the build folder
- name: Sync to S3
run: |
aws s3 sync ${{ env.BUILD_DIR }} s3://${{ env.S3_BUCKET }} --delete
# 7. Invalidate CloudFront Cache
- name: Invalidate CloudFront
run: |
aws cloudfront create-invalidation \
--distribution-id ${{ env.CF_DISTRIBUTION_ID }} \
--paths "/*"
2. Required Setup
Before this workflow runs, you must configure your GitHub repository secrets and AWS permissions.
A. GitHub Secrets
Go to your repository Settings > Secrets and variables > Actions and add the following two secrets:
AWS_ACCESS_KEY_ID: Your AWS Access Key.
AWS_SECRET_ACCESS_KEY: Your AWS Secret Key.
Note: Ensure the IAM User belonging to these keys has permissions for s3:PutObject, s3:ListBucket, and cloudfront:CreateInvalidation.
B. Update Variables in the File
In the env section of the YAML file above, update the values to match your project:
BUILD_DIR: Change to dist if using Vite, or keep build if using Create React App.
S3_BUCKET: Your actual bucket name (e.g., my-app-prod).
CF_DISTRIBUTION_ID: Found in your AWS CloudFront console.
3. Security Best Practice (OIDC)
The method above uses long-lived Access Keys. For better security, GitHub and AWS recommend using OIDC (OpenID Connect).
To switch to OIDC:
Remove the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY secrets from GitHub.
In AWS IAM, create an Identity Provider for GitHub Actions.
Create an IAM Role that trusts this provider and attach the necessary S3/CloudFront policies.
Update the "Configure AWS Credentials" step in the YAML to look like this: