Write a GitHub Actions workflow file that sets up Node.js, builds a React app (yarn…
- User
Write a GitHub Actions workflow file that sets up Node.js, builds a React app (yarn build), and deploys assets to S3 with CloudFront.
- Assistant
Creating GitHub Actions Workflow
The user wants a GitHub Actions workflow to build a React app with Node.js, deploy it to S3 using CloudFront. I'll start by considering the steps: set triggers on pushes to main, create a job for building and deploying on ubuntu-latest. In the steps, I'll check out the code, set up Node.js, possibly cache Yarn, and install dependencies. Tests could be optional. Then, I'll configure AWS credentials and sync the build directory to S3. Finally, I’ll add CloudFront invalidation instructions and replace placeholders with user-specific details. I'll produce the YAML and a concise explanation.Building Simple YAML for Deployment
I’m focusing on creating a clear YAML output with brief notes for a GitHub Actions workflow to build and deploy a React app. I'll specify Node.js versions 18 or 20 and consider keeping it simple—no fluff about knowledge cutoffs. I'll define the workflow name and triggers for pushes to the main branch, along with workflow dispatch. For permissions, I’ll include reading and writing for contents and id-token, but stick to static credentials since that’s simpler. I'll use AWS secrets for credentials while noting OIDC for more advanced setups, which the user didn’t request.Defining Environment and Job Steps
I’m crafting the steps for the GitHub Actions workflow, where I’ll set environment variables like S3_BUCKET and DISTRIBUTION_ID. The job will run on ubuntu-latest. The steps include checking out the code, setting up Node.js with caching for Yarn, and installing dependencies. I'll also build the app, configure AWS credentials, and deploy the build to S3. For invalidating the CloudFront cache, I’ll use secrets for the distribution ID. It’s worth noting that AWS CLI v2 is pre-installed on the Ubuntu runner, ensuring a smooth deployment process. Plus, I should confirm the working directory for the React app is correct.Finalizing GitHub Actions Workflow
The user has asked for a GitHub Actions workflow that sets up Node.js, builds a React app with
yarn build, and deploys assets to S3 using CloudFront. I need to ensure the CloudFront invalidation step is included. I might also mention setting an environment key "NODE_ENV: production" during the build step, although it's not strictly necessary. I’ll create a clear YAML output with a brief context to help the user understand the setup. ```yaml.github/workflows/deploy-react-to-s3.yml
name: Build and Deploy React App to S3 + CloudFront
on: push: branches: [ main ] # Change if you use a different default branch workflow_dispatch: # Allow manual runs from the Actions tab
jobs: build-and-deploy: runs-on: ubuntu-latest
steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up Node.js uses: actions/setup-node@v4 with: node-version: '20' # Or 18, etc. cache: 'yarn' - name: Install dependencies run: yarn install --frozen-lockfile - name: Build React app run: yarn build # Configure AWS credentials using repository secrets - 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 }} - name: Deploy build assets to S3 env: S3_BUCKET_NAME: ${{ secrets.S3_BUCKET_NAME }} run: | aws s3 sync build/ s3://$S3_BUCKET_NAME \ --delete \ --cache-control "public, max-age=31536000" \ --exclude "index.html" # Upload index.html with no long-term cache so updates show quickly aws s3 cp build/index.html s3://$S3_BUCKET_NAME/index.html \ --cache-control "no-cache, no-store, must-revalidate" - name: Invalidate CloudFront cache if: ${{ success() }} env: CF_DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} run: | aws cloudfront create-invalidation \ --distribution-id $CF_DISTRIBUTION_ID \ --paths "/*"Required repository secrets (Settings → Secrets and variables → Actions): - `AWS_ACCESS_KEY_ID` - `AWS_SECRET_ACCESS_KEY` - `AWS_REGION` (e.g. `us-east-1`) - `S3_BUCKET_NAME` (e.g. `my-react-app-bucket`) - `CLOUDFRONT_DISTRIBUTION_ID` (e.g. `E1234567890ABC`) Adjust branch name, Node version, and any S3/CloudFront options to match your setup.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.