Skip to content

Commit 56ce48a

Browse files
Add CI/CD workflow
1 parent 6c5dd84 commit 56ce48a

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

.github/workflows/ci-cd.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: CI/CD Node.js → AWS Lambda + API Gateway with Rollback
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
7+
jobs:
8+
build-and-test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v3
13+
14+
- name: Setup Node.js
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: '18'
18+
19+
- name: Install dependencies
20+
run: npm install
21+
22+
- name: Run tests
23+
run: npm test
24+
25+
deploy:
26+
needs: build-and-test
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Checkout code
30+
uses: actions/checkout@v3
31+
32+
- name: Setup Node.js
33+
uses: actions/setup-node@v3
34+
with:
35+
node-version: '18'
36+
37+
- name: Install dependencies
38+
run: npm install
39+
40+
- name: Zip project
41+
run: zip -r function.zip .
42+
43+
- name: Configure AWS credentials
44+
uses: aws-actions/configure-aws-credentials@v2
45+
with:
46+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
47+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
48+
aws-region: ${{ secrets.AWS_REGION }}
49+
50+
- name: Deploy Lambda with rollback
51+
id: deploy
52+
run: |
53+
# Get current Lambda version of prod alias
54+
PREV_VERSION=$(aws lambda get-alias --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} --name prod --query 'FunctionVersion' --output text)
55+
echo "Previous Lambda version: $PREV_VERSION"
56+
57+
# Update function code
58+
aws lambda update-function-code \
59+
--function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \
60+
--zip-file fileb://function.zip
61+
62+
# Publish a new version
63+
NEW_VERSION=$(aws lambda publish-version --function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} --query 'Version' --output text)
64+
echo "New Lambda version: $NEW_VERSION"
65+
66+
# Update alias to new version
67+
aws lambda update-alias \
68+
--function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \
69+
--name prod \
70+
--function-version $NEW_VERSION || \
71+
# If alias update fails, rollback to previous version
72+
aws lambda update-alias \
73+
--function-name ${{ secrets.LAMBDA_FUNCTION_NAME }} \
74+
--name prod \
75+
--function-version $PREV_VERSION
76+
77+
- name: Deploy API Gateway
78+
run: |
79+
aws apigateway create-deployment \
80+
--rest-api-id ${{ secrets.API_ID }} \
81+
--stage-name ${{ secrets.STAGE_NAME }} \
82+
--description "Deployed via GitHub Actions"

0 commit comments

Comments
 (0)