|
| 1 | +name: Deploy Portfolio to AWS |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +env: |
| 9 | + AWS_REGION: ap-south-1 |
| 10 | + |
| 11 | +jobs: |
| 12 | + terraform: |
| 13 | + name: Provision Infrastructure |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + api_gateway_url: ${{ steps.tf_outputs.outputs.api_gateway_url }} |
| 17 | + s3_bucket_name: ${{ steps.tf_outputs.outputs.s3_bucket_name }} |
| 18 | + lambda_function_name: ${{ steps.tf_outputs.outputs.lambda_function_name }} |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Configure AWS Credentials |
| 25 | + uses: aws-actions/configure-aws-credentials@v4 |
| 26 | + with: |
| 27 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 28 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 29 | + aws-region: ${{ env.AWS_REGION }} |
| 30 | + |
| 31 | + - name: Setup Terraform |
| 32 | + uses: hashicorp/setup-terraform@v3 |
| 33 | + with: |
| 34 | + terraform_wrapper: false |
| 35 | + |
| 36 | + - name: Terraform Init |
| 37 | + working-directory: infrastructure |
| 38 | + run: terraform init |
| 39 | + |
| 40 | + - name: Terraform Apply |
| 41 | + working-directory: infrastructure |
| 42 | + run: | |
| 43 | + terraform apply -auto-approve \ |
| 44 | + -var="sender_email=${{ secrets.SENDER_EMAIL }}" \ |
| 45 | + -var="recipient_email=${{ secrets.RECIPIENT_EMAIL }}" |
| 46 | +
|
| 47 | + - name: Capture Terraform outputs |
| 48 | + id: tf_outputs |
| 49 | + working-directory: infrastructure |
| 50 | + run: | |
| 51 | + echo "api_gateway_url=$(terraform output -raw api_gateway_url)" >> $GITHUB_OUTPUT |
| 52 | + echo "s3_bucket_name=$(terraform output -raw s3_bucket_name)" >> $GITHUB_OUTPUT |
| 53 | + echo "lambda_function_name=$(terraform output -raw lambda_function_name)" >> $GITHUB_OUTPUT |
| 54 | +
|
| 55 | + deploy-frontend: |
| 56 | + name: Build and Deploy Frontend |
| 57 | + runs-on: ubuntu-latest |
| 58 | + needs: terraform |
| 59 | + |
| 60 | + steps: |
| 61 | + - name: Checkout code |
| 62 | + uses: actions/checkout@v4 |
| 63 | + |
| 64 | + - name: Configure AWS Credentials |
| 65 | + uses: aws-actions/configure-aws-credentials@v4 |
| 66 | + with: |
| 67 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 68 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 69 | + aws-region: ${{ env.AWS_REGION }} |
| 70 | + |
| 71 | + - name: Setup Node.js |
| 72 | + uses: actions/setup-node@v4 |
| 73 | + with: |
| 74 | + node-version: '20' |
| 75 | + |
| 76 | + - name: Install dependencies |
| 77 | + working-directory: client |
| 78 | + run: npm ci |
| 79 | + |
| 80 | + - name: Build Next.js static export |
| 81 | + working-directory: client |
| 82 | + env: |
| 83 | + NEXT_PUBLIC_API_URL: ${{ needs.terraform.outputs.api_gateway_url }} |
| 84 | + run: npm run build |
| 85 | + |
| 86 | + - name: Sync to S3 |
| 87 | + run: | |
| 88 | + aws s3 sync client/out/ s3://${{ needs.terraform.outputs.s3_bucket_name }}/ --delete |
| 89 | +
|
| 90 | + deploy-lambda: |
| 91 | + name: Deploy Lambda Function |
| 92 | + runs-on: ubuntu-latest |
| 93 | + needs: terraform |
| 94 | + |
| 95 | + steps: |
| 96 | + - name: Checkout code |
| 97 | + uses: actions/checkout@v4 |
| 98 | + |
| 99 | + - name: Configure AWS Credentials |
| 100 | + uses: aws-actions/configure-aws-credentials@v4 |
| 101 | + with: |
| 102 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 103 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 104 | + aws-region: ${{ env.AWS_REGION }} |
| 105 | + |
| 106 | + - name: Package Lambda Function |
| 107 | + run: zip -j lambda.zip lambda/handler.py |
| 108 | + |
| 109 | + - name: Deploy Lambda Function |
| 110 | + run: | |
| 111 | + aws lambda update-function-code \ |
| 112 | + --function-name ${{ needs.terraform.outputs.lambda_function_name }} \ |
| 113 | + --zip-file fileb://lambda.zip |
0 commit comments