This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configure OIDC provider first | |
name: Deploy with Terraform and Deploy out/ to AWS S3 bucket | |
on: | |
push: | |
branches: [main] | |
env: | |
REGION: ${{ secrets.AWS_REGION }} | |
IAM_ROLE: ${{ secrets.AWS_ASSUME_ROLE_ARN }} # this is a IAM role in AWS account store as secret in github account | |
BUCKET: www.justinleafyeh.com | |
WWW_DOMAIN: www.justinleafyeh.com | |
BASE_DOMAIN: justinleafyeh.com | |
DIST: out | |
DIST_ID: ${{ secrets.AWS_DISTRIBUTION_ID }} | |
# permission can be added at job level or workflow level | |
permissions: | |
id-token: write # This is required for requesting the JWT | |
contents: read # This is required for actions/checkout | |
jobs: | |
# Needs to add state locking or store the state in S3 bucket | |
terraform-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Configure AWS Credentials with GitHub OIDC | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ${{ env.REGION }} | |
role-to-assume: ${{ env.IAM_ROLE }} | |
role-session-name: my-github-actions-session | |
- name: Set up Terraform | |
uses: hashicorp/setup-terraform@v3 | |
- name: Update Infra with Terraform | |
run: | | |
cd infra/setup-terraform | |
terraform init | |
terraform plan -out plan.tfplan -var="www_domain=${{ env.WWW_DOMAIN }}" -var="base_domain=${{ env.BASE_DOMAIN }}" | |
terraform apply -auto-approve plan.tfplan | |
build-and-deploy: | |
# needs: 'terraform-deploy' | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node-version: [20.x] | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
clean: false | |
- name: Configure AWS Credentials with GitHub OIDC | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ${{ env.REGION }} | |
role-to-assume: ${{ env.IAM_ROLE }} | |
role-session-name: my-github-actions-session | |
- name: Install Dependencies | |
uses: actions/setup-node@v4 | |
with: | |
node-version: ${{ matrix.node-version }} | |
- name: Install dependencies | |
run: npm install | |
- name: Build | |
run: npm run build_fix | |
- name: Deploy out/ folder to S3 | |
run: | | |
aws s3 sync ${{env.DIST}} s3://${{ env.BUCKET }}/out --exclude "*.js" --delete | |
aws s3 sync ${{env.DIST}} s3://${{ env.BUCKET }}/out --exclude "*" --include "*.js" --content-type application/javascript --delete | |
- name: Invalidate CloudFront cache | |
run: aws cloudfront create-invalidation --distribution-id ${{env.DIST_ID}} --paths "/*" |