added error response codes #8
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
name: Create with TF and Deploy to AWS S3 and CloudFront | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
permissions: | |
contents: read | |
id-token: write | |
jobs: | |
create-infrastructure: | |
runs-on: ubuntu-22.04 | |
outputs: | |
s3_bucket: ${{ steps.tf_outputs.outputs.S3_BUCKET }} | |
cf_distribution_id: ${{ steps.tf_outputs.outputs.CF_DISTRIBUTION_ID }} | |
defaults: | |
run: | |
working-directory: terraform | |
steps: | |
#Step 1: Checkout the Code from the Repository1 | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
with: | |
submodules: true # Fetch Hugo themes (true OR recursive) | |
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod | |
#Step 2: Install Terraform | |
- name: Install Terraform | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: "1.1.7" | |
cli_config_credentials_token: ${{ secrets.TF_TOKEN }} | |
#Step 3: Configure AWS Credentials | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
audience: sts.amazonaws.com | |
role-to-assume: ${{ secrets.AWS_IAM_ROLE }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
#Step 4: Terraform Init | |
- name: Terraform Init | |
run: terraform init | |
#Step 5: Terraform Plan | |
- name: Terraform Plan | |
run: terraform plan | |
#Step 6: Terraform Apply | |
- name: Terraform Apply | |
run: terraform apply -auto-approve | |
#Step 7: Terraform Output | |
- name: Terraform Output | |
id: tf_outputs | |
run: | | |
echo "S3_BUCKET=$(terraform output s3_bucket | tr -d '""')" >> "$GITHUB_OUTPUT" | |
echo "CF_DISTRIBUTION_ID=$(terraform output cloudfront_id | tr -d '""')" >> "$GITHUB_OUTPUT" | |
build_and_deploy_job: | |
needs: create-infrastructure | |
runs-on: ubuntu-22.04 | |
env: | |
HUGO_VERSION: 0.138.0 | |
CF_DISTRIBUTION_ID: ${{ needs.create-infrastructure.outputs.cf_distribution_id }} | |
S3_BUCKET: ${{ needs.create-infrastructure.outputs.s3_bucket }} | |
steps: | |
#Step 1: Checkout the Code from the Repository | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
with: | |
submodules: true # Fetch Hugo themes (true OR recursive) | |
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod | |
#Step 2: Install Hugo | |
- name: Install Hugo | |
uses: peaceiris/actions-hugo@v3 | |
with: | |
hugo-version: ${{ env.HUGO_VERSION }} | |
extended: true | |
#Step 3: Build Hugo | |
- name: Build Hugo | |
run: hugo --minify | |
#Step 4: Upload Artifact | |
- name: Upload Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: tech-blog | |
path: public/** | |
#Step 5: Configure AWS Credentials | |
- name: Configure AWS Credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
audience: sts.amazonaws.com | |
role-to-assume: ${{ secrets.AWS_IAM_ROLE }} | |
aws-region: ${{ secrets.AWS_REGION}} | |
#Step 6: Deploy to S3 | |
- name: Deploy to S3 | |
run: | | |
aws s3 sync ./public/ s3://${{ env.S3_BUCKET }} --delete | |
#Step 7: Invalidate CloudFront | |
- name: Invalidate CloudFront | |
run: | | |
aws cloudfront create-invalidation --distribution-id ${{ env.CF_DISTRIBUTION_ID }} --paths "/*" |