Skip to content

Commit

Permalink
update the workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mohit-nagaraj committed Aug 31, 2024
1 parent 014d81f commit b5f9978
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 84 deletions.
104 changes: 104 additions & 0 deletions .github/old-workflows-lambda/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Deploy to AWS Lambda

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the repository
- name: Checkout Code
uses: actions/checkout@v3

# Step 2: Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20' # Specify your Node.js version

# Step 3: Configure AWS Credentials
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

# Step 4: Cache Node.js dependencies for all folders
- name: Cache Node.js modules
uses: actions/cache@v4
with:
path: |
client/node_modules
server/node_modules
socket/node_modules
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-modules
# ============================
# Deploy Client (Vite Frontend)
# ============================

- name: Install Client Dependencies
working-directory: client
run: npm install

- name: Build Client
working-directory: client
run: npm run build

- name: Package Client for Lambda
working-directory: client
run: |
zip -r ../client.zip . -x "*.git*"
- name: Deploy Client to AWS Lambda
run: |
aws lambda update-function-code --function-name ClientLambda --zip-file fileb://client.zip
# ============================
# Deploy Server (Express.js Backend)
# ============================

- name: Install Server Dependencies
working-directory: server
run: npm install

- name: Package Server for Lambda
working-directory: server
run: |
zip -r ../server.zip . -x "*.git*"
- name: Deploy Server to AWS Lambda
run: |
aws lambda update-function-code --function-name ServerLambda --zip-file fileb://server.zip
# ============================
# Deploy Socket.IO Server
# ============================

- name: Install Socket Dependencies
working-directory: socket
run: npm install

- name: Package Socket.IO for Lambda
working-directory: socket
run: |
zip -r ../socket.zip . -x "*.git*"
- name: Deploy Socket.IO to AWS Lambda
run: |
aws lambda update-function-code --function-name SocketLambda --zip-file fileb://socket.zip
# ============================
# Cleanup Zip Files
# ============================

- name: Clean Up Zip Files
run: |
rm client.zip server.zip socket.zip
110 changes: 26 additions & 84 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,104 +1,46 @@
name: Deploy to AWS Lambda
name: Deploy to AWS

on:
push:
branches:
- main

jobs:
deploy:
build-and-deploy:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the repository
- name: Checkout Code
- name: Checkout code
uses: actions/checkout@v3

# Step 2: Set up Node.js environment
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18' # Specify your Node.js version

# Step 3: Configure AWS Credentials
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

# Step 4: Cache Node.js dependencies for all folders
- name: Cache Node.js modules
uses: actions/cache@v3
- name: Log in to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
path: |
client/node_modules
server/node_modules
socket/node_modules
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-modules
# ============================
# Deploy Client (Vite Frontend)
# ============================

- name: Install Client Dependencies
working-directory: client
run: npm install
region: ${{ secrets.AWS_REGION }}

- name: Build Client
working-directory: client
run: npm run build

- name: Package Client for Lambda
working-directory: client
run: |
zip -r ../client.zip . -x "*.git*"
- name: Deploy Client to AWS Lambda
- name: Build and push Docker image for socket
run: |
aws lambda update-function-code --function-name ClientLambda --zip-file fileb://client.zip
# ============================
# Deploy Server (Express.js Backend)
# ============================

- name: Install Server Dependencies
working-directory: server
run: npm install
docker buildx build --platform linux/amd64 -t ${{ secrets.ECR_SOCKET_REPO_URI }}:latest ./socket
docker push ${{ secrets.ECR_SOCKET_REPO_URI }}:latest
- name: Package Server for Lambda
working-directory: server
- name: Build and push Docker image for server
run: |
zip -r ../server.zip . -x "*.git*"
- name: Deploy Server to AWS Lambda
run: |
aws lambda update-function-code --function-name ServerLambda --zip-file fileb://server.zip
# ============================
# Deploy Socket.IO Server
# ============================

- name: Install Socket Dependencies
working-directory: socket
run: npm install

- name: Package Socket.IO for Lambda
working-directory: socket
run: |
zip -r ../socket.zip . -x "*.git*"
- name: Deploy Socket.IO to AWS Lambda
run: |
aws lambda update-function-code --function-name SocketLambda --zip-file fileb://socket.zip
# ============================
# Cleanup Zip Files
# ============================
docker buildx build --platform linux/amd64 -t ${{ secrets.ECR_SERVER_REPO_URI }}:latest ./server
docker push ${{ secrets.ECR_SERVER_REPO_URI }}:latest
- name: Clean Up Zip Files
- name: Deploy to EC2 Instance
run: |
rm client.zip server.zip socket.zip
ssh -o StrictHostKeyChecking=no -i ${{ secrets.EC2_SSH_KEY }} ubuntu@${{ secrets.EC2_INSTANCE_IP }} << 'EOF'
docker pull ${{ secrets.ECR_SOCKET_REPO_URI }}:latest
docker pull ${{ secrets.ECR_SERVER_REPO_URI }}:latest
docker stop socket-container || true
docker stop server-container || true
docker rm socket-container || true
docker rm server-container || true
docker run -d --name socket-container -p 3000:3000 ${{ secrets.ECR_SOCKET_REPO_URI }}:latest
docker run -d --name server-container -p 4000:4000 ${{ secrets.ECR_SERVER_REPO_URI }}:latest
EOF

0 comments on commit b5f9978

Please sign in to comment.