1+ name : CI/CD Pipeline
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+ - develop
8+ pull_request :
9+ branches :
10+ - main
11+ - develop
12+
13+ permissions :
14+ id-token : write
15+ contents : read
16+
17+ env :
18+ AWS_REGION : us-east-1
19+ ECR_REGISTRY_ALIAS : ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.us-east-1.amazonaws.com
20+ IMAGE_NAME : nodejs-api
21+
22+ jobs :
23+ lint :
24+ name : Lint Code
25+ runs-on : ubuntu-22.04
26+ steps :
27+ - name : Checkout code
28+ uses : actions/checkout@b4ffde65f69735aad63487c1169896c199e89eaf
29+ with :
30+ fetch-depth : 0
31+
32+ - name : Setup Node.js
33+ uses : actions/setup-node@8f152de45cc393bb48ce5d6f0381a57a7ed4bf0a
34+ with :
35+ node-version : ' 20.10.0'
36+ cache : ' npm'
37+
38+ - name : Install dependencies
39+ run : npm ci
40+
41+ - name : Run ESLint
42+ run : npm run lint --if-present
43+
44+ - name : Run Prettier
45+ run : npm run format:check --if-present
46+
47+ test :
48+ name : Run Tests
49+ runs-on : ubuntu-22.04
50+ needs : lint
51+ steps :
52+ - name : Checkout code
53+ uses : actions/checkout@b4ffde65f69735aad63487c1169896c199e89eaf
54+
55+ - name : Setup Node.js
56+ uses : actions/setup-node@8f152de45cc393bb48ce5d6f0381a57a7ed4bf0a
57+ with :
58+ node-version : ' 20.10.0'
59+ cache : ' npm'
60+
61+ - name : Install dependencies
62+ run : npm ci
63+
64+ - name : Run unit tests
65+ run : npm test -- --coverage --passWithNoTests
66+
67+ - name : Upload coverage reports
68+ uses : actions/upload-artifact@26f96dfc697d77e81fd5907df203aa23a56210f8
69+ if : always()
70+ with :
71+ name : coverage-reports
72+ path : coverage/
73+ retention-days : 30
74+
75+ build :
76+ name : Build Application
77+ runs-on : ubuntu-22.04
78+ needs : test
79+ steps :
80+ - name : Checkout code
81+ uses : actions/checkout@b4ffde65f69735aad63487c1169896c199e89eaf
82+
83+ - name : Setup Node.js
84+ uses : actions/setup-node@8f152de45cc393bb48ce5d6f0381a57a7ed4bf0a
85+ with :
86+ node-version : ' 20.10.0'
87+ cache : ' npm'
88+
89+ - name : Install dependencies
90+ run : npm ci
91+
92+ - name : Build application
93+ run : npm run build --if-present
94+
95+ - name : Upload build artifacts
96+ uses : actions/upload-artifact@26f96dfc697d77e81fd5907df203aa23a56210f8
97+ with :
98+ name : build-artifacts
99+ path : |
100+ dist/
101+ node_modules/
102+ package.json
103+ package-lock.json
104+ retention-days : 1
105+
106+ docker-build-push :
107+ name : Build and Push Docker Image
108+ runs-on : ubuntu-22.04
109+ needs : build
110+ if : github.event_name == 'push'
111+ outputs :
112+ image-uri : ${{ steps.image.outputs.image-uri }}
113+ steps :
114+ - name : Checkout code
115+ uses : actions/checkout@b4ffde65f69735aad63487c1169896c199e89eaf
116+
117+ - name : Download build artifacts
118+ uses : actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935
119+ with :
120+ name : build-artifacts
121+
122+ - name : Configure AWS credentials
123+ uses : aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502
124+ with :
125+ role-to-assume : ${{ secrets.AWS_ROLE_TO_ASSUME }}
126+ aws-region : ${{ env.AWS_REGION }}
127+
128+ - name : Login to Amazon ECR
129+ id : login-ecr
130+ uses : aws-actions/amazon-ecr-login@062b18b96a7aabea0ba9c0d1b16689709525c965
131+
132+ - name : Build Docker image
133+ id : docker-build
134+ env :
135+ REGISTRY : ${{ steps.login-ecr.outputs.registry }}
136+ REPOSITORY : ${{ env.IMAGE_NAME }}
137+ IMAGE_TAG : ${{ github.sha }}
138+ run : |
139+ docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG -t $REGISTRY/$REPOSITORY:latest .
140+ echo "image-tag=$IMAGE_TAG" >> $GITHUB_OUTPUT
141+
142+ - name : Push Docker image to ECR
143+ env :
144+ REGISTRY : ${{ steps.login-ecr.outputs.registry }}
145+ REPOSITORY : ${{ env.IMAGE_NAME }}
146+ IMAGE_TAG : ${{ steps.docker-build.outputs.image-tag }}
147+ run : |
148+ docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG
149+ docker push $REGISTRY/$REPOSITORY:latest
150+ echo "image-uri=$REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
151+
152+ - name : Set image URI output
153+ id : image
154+ env :
155+ REGISTRY : ${{ steps.login-ecr.outputs.registry }}
156+ REPOSITORY : ${{ env.IMAGE_NAME }}
157+ IMAGE_TAG : ${{ steps.docker-build.outputs.image-tag }}
158+ run : |
159+ echo "image-uri=$REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
160+
161+ terraform-plan :
162+ name : Terraform Plan
163+ runs-on : ubuntu-22.04
164+ needs : docker-build-push
165+ if : github.event_name == 'pull_request'
166+ steps :
167+ - name : Checkout code
168+ uses : actions/checkout@b4ffde65f69735aad63487c1169896c199e89eaf
169+
170+ - name : Configure AWS credentials
171+ uses : aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502
172+ with :
173+ role-to-assume : ${{ secrets.AWS_ROLE_TO_ASSUME }}
174+ aws-region : ${{ env.AWS_REGION }}
175+
176+ - name : Setup Terraform
177+ uses : hashicorp/setup-terraform@a1502cd9aee432aca07f620e2dce6b0EDA834ffF
178+ with :
179+ terraform_version : 1.6.0
180+
181+ - name : Terraform Format Check
182+ run : terraform fmt -check -recursive
183+
184+ - name : Terraform Init
185+ working-directory : ./terraform
186+ run : terraform init
187+
188+ - name : Terraform Validate
189+ working-directory : ./terraform
190+ run : terraform validate
191+
192+ - name : Terraform Plan
193+ working-directory : ./terraform
194+ env :
195+ TF_VAR_image_uri : ${{ needs.docker-build-push.outputs.image-uri }}
196+ run : terraform plan -out=tfplan
197+
198+ - name : Upload Terraform Plan
199+ uses : actions/upload-artifact@26f96dfc697d77e81fd5907df203aa23a56210f8
200+ with :
201+ name : tfplan
202+ path : terraform/tfplan
203+ retention-days : 7
204+
205+ deploy :
206+ name : Deploy to AWS ECS
207+ runs-on : ubuntu-22.04
208+ needs : docker-build-push
209+ if : github.event_name == 'push' && github.ref == 'refs/heads/main'
210+ environment :
211+ name : production
212+ steps :
213+ - name : Checkout code
214+ uses : actions/checkout@b4ffde65f69735aad63487c1169896c199e89eaf
215+
216+ - name : Configure AWS credentials
217+ uses : aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502
0 commit comments