77
88env :
99 AWS_REGION : ap-south-1
10+ FORCE_JAVASCRIPT_ACTIONS_TO_NODE24 : true
1011
1112jobs :
12- terraform :
13- name : Provision Infrastructure
13+ # Checks for changes in directories, always run hoga
14+ changes :
15+
16+ name : Detect changes
1417 runs-on : ubuntu-latest
18+
1519 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- cloudfront_distribution_id : ${{ steps.tf_outputs.outputs.cloudfront_distribution_id }}
20+ infrastructure : ${{ steps.filter.outputs.infrastructure }}
21+ lambda : ${{ steps.filter.outputs.infrastructure }}
22+ frontend : ${{ steps.filter.outputs.infrastructure }}
23+
24+ steps :
25+ - name : Checkout code
26+ uses : actions/checkout@v5
27+
28+ - name : Detect changed directories
29+ uses : dorny/paths-filter@v4
30+ id : filter
31+ with :
32+ filters : |
33+ infrastructure:
34+ - 'infrastructure/**'
35+ lambda:
36+ - 'lambda/**'
37+ frontend:
38+ - 'client/**'
39+
40+ terraform :
2041
42+ name : Provision Infrastructure
43+ runs-on : ubuntu-latest
44+ needs : changes
45+ if : needs.changes.outputs.infrastructure == 'true'
2146
2247 steps :
2348 - name : Checkout code
24- uses : actions/checkout@v4
49+ uses : actions/checkout@v5
2550
2651 - name : Configure AWS Credentials
2752 uses : aws-actions/configure-aws-credentials@v4
2853 with :
2954 aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
3055 aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
31- aws-region : ${{ env .AWS_REGION }}
56+ aws-region : ${{ secrets .AWS_REGION }}
3257
33- - name : Setup Terraform
58+ - name : Setup terraform
3459 uses : hashicorp/setup-terraform@v3
3560 with :
3661 terraform_wrapper : false
@@ -49,80 +74,76 @@ jobs:
4974 -var="cloudflare_api_token=${{ secrets.CLOUDFLARE_API_TOKEN }}" \
5075 -var="cloudflare_zone_id=${{ secrets.CLOUDFLARE_ZONE_ID }}"
5176
52- - name : Capture Terraform outputs
53- id : tf_outputs
54- working-directory : infrastructure
77+ deploy-lambda :
78+
79+ name : Deploy Lambda Function
80+ runs-on : ubuntu-latest
81+ needs : [changes, terraform]
82+ if : |
83+ always() && needs.changes.outputs.lambda == 'true' && (needs.terraform.result == 'success' || needs.terraform.result == 'skipped')
84+
85+ steps :
86+ - name : Checkout code
87+ uses : actions/checkout@v5
88+
89+ - name : Configure AWS Credentials
90+ uses : aws-actions/configure-aws-credentials@v4
91+ with :
92+ aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
93+ aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
94+ aws-region : ${{ secrets.AWS_REGION }}
95+
96+ - name : Package Lambda Function
5597 run : |
56- echo "api_gateway_url=$(terraform output -raw api_gateway_url)" >> $GITHUB_OUTPUT
57- echo "s3_bucket_name=$(terraform output -raw s3_bucket_name)" >> $GITHUB_OUTPUT
58- echo "lambda_function_name=$(terraform output -raw lambda_function_name)" >> $GITHUB_OUTPUT
59- echo "cloudfront_distribution_id=$(terraform output -raw cloudfront_distribution_id)" >> $GITHUB_OUTPUT
98+ pip install -r lambda/requirements.txt -t lambda/package/ --quiet
99+ cp lambda/handler.py lambda/package/
100+ cd lambda/package && zip -r ../../lambda.zip .
60101
102+ - name : Deploy Lambda Function
103+ run : |
104+ aws lambda update-function-code \
105+ --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \
106+ --zip-file fileb://lambda.zip
107+ aws lambda wait function-updated \
108+ --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }}
61109
62110 deploy-frontend :
111+
63112 name : Build and Deploy Frontend
64113 runs-on : ubuntu-latest
65- needs : terraform
114+ needs : [changes, terraform, deploy-lambda]
115+ if : |
116+ always() && needs.changes.outputs.frontend == 'true' && (needs.terraform.result == 'success' && needs.terraform.result == 'skipped') && (needs.deploy-lambda.result == 'success' || needs.deploy-lambda.result == 'skipped')
66117
67118 steps :
68119 - name : Checkout code
69- uses : actions/checkout@v4
120+ uses : actions/checkout@v5
70121
71122 - name : Configure AWS Credentials
72123 uses : aws-actions/configure-aws-credentials@v4
73124 with :
74125 aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
75126 aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
76- aws-region : ${{ env .AWS_REGION }}
127+ aws-region : ${{ secrets .AWS_REGION }}
77128
78129 - name : Setup Node.js
79130 uses : actions/setup-node@v4
80131 with :
81132 node-version : ' 20'
82133
83- - name : Install dependencies
134+ - name : Install Dependencies
84135 working-directory : client
85136 run : npm ci
86137
87138 - name : Build Next.js static export
88139 working-directory : client
89140 env :
90141 NEXT_PUBLIC_API_URL : ${{ secrets.NEXT_PUBLIC_API_URL }}
91- NEXT_PUBLIC_RECAPTCHA_SITE_KEY : ${{ secrets.NEXT_PUBLIC_RECAPTCHA_SITE_KEY }}
142+ NEXT_PUBLIC_RECAPTCHA_SITE_KET : ${{ secrets.NEXT_PUBLIC_RECAPTCHA_SITE_KET }}
92143 run : npm run build
93144
94145 - name : Sync to S3
95- run : aws s3 sync client/out/ s3://${{ needs.terraform.outputs.s3_bucket_name }}/ --delete
96-
97- - name : Invalidate CloudFront cache
98- run : aws cloudfront create-invalidation --distribution-id ${{ needs.terraform.outputs.cloudfront_distribution_id }} --paths "/*"
99-
100- deploy-lambda :
101- name : Deploy Lambda Function
102- runs-on : ubuntu-latest
103- needs : terraform
104-
105- steps :
106- - name : Checkout code
107- uses : actions/checkout@v4
146+ run : aws s3 sync client/out/ s3://${{ secrets.S3_BUCKET_NAME }}/ --delete
108147
109- - name : Configure AWS Credentials
110- uses : aws-actions/configure-aws-credentials@v4
111- with :
112- aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
113- aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
114- aws-region : ${{ env.AWS_REGION }}
115-
116- - name : Package Lambda Function
117- run : |
118- pip install -r lambda/requirements.txt -t lambda/package/ --quiet
119- cp lambda/handler.py lambda/package/
120- cd lambda/package && zip -r ../../lambda.zip .
121-
122- - name : Deploy Lambda Function
123- run : |
124- aws lambda update-function-code \
125- --function-name ${{ needs.terraform.outputs.lambda_function_name }} \
126- --zip-file fileb://lambda.zip
127- aws lambda wait function-updated \
128- --function-name ${{ needs.terraform.outputs.lambda_function_name }}
148+ - name : Invalidate Cloudfront Cache
149+ run : aws cloudfront create-invalidation -- disrtibution-id ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }} --paths "/*"
0 commit comments