Action for S3 Deploy

to automate deployment tasks to S3 Static Hosting based on different branches.

circle-info

if your workflow file is named deployment.yml, the path would be: .github/workflows/deployment.yml

name: Deploy to S3 and CloudFront
on:
  push:
    branches:
      - development
      - production
    tags:
    - '*'
    
jobs:
  build:
    name: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '20'

      - name: Install dependencies
        run: npm install 

      - name: Build app

        run: |
          touch .env

          if [[ "${{github.ref}}" == "refs/heads/development" ]]; then
            echo API_URL=${{ secrets.DEVELOPMENT_API_URL }} >> .env

          elif [[ "${{github.ref}}" == "refs/heads/production" ]]; then
            echo API_URL=${{ secrets.PRODUCTION_API_URL }} >> .env
          fi
          npm run build 

      - 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: ${{ secrets.AWS_REGION }}

      - name: Deploy to S3
        run: aws s3 sync ./out s3://${{ secrets.AWS_S3_STATIC_WEB_BUCKET }}

      - name: Invalidate CloudFront Cache
        run: aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*"

Last updated