Skip to content

Commit

Permalink
Update main.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhinavJFT authored Sep 12, 2024
1 parent 82c215a commit 7fe3a02
Showing 1 changed file with 102 additions and 7 deletions.
109 changes: 102 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,90 @@
# name: CI/CD Pipeline for DigitalOcean Droplet

# on:
# push:
# branches:
# - cicd

# jobs:
# redeploy:
# runs-on: ubuntu-latest

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

# # Step 2: Set up SSH for DigitalOcean access
# - name: Set up SSH
# uses: webfactory/[email protected]
# with:
# ssh-private-key: ${{ secrets.SSH_KEY }}

# # Step 3: Determine which servers had changes
# - name: Check changes in files
# id: check_files
# run: |
# CHATBOT_MAIN_CHANGED=false
# CHATBOT_PROCESS_CHANGED=false

# if git diff --name-only HEAD^ HEAD | grep -q 'chatbotmain/'; then
# CHATBOT_MAIN_CHANGED=true
# fi

# if git diff --name-only HEAD^ HEAD | grep -q 'chatbotprocess/'; then
# CHATBOT_PROCESS_CHANGED=true
# fi

# echo "::set-output name=main_changed::${CHATBOT_MAIN_CHANGED}"
# echo "::set-output name=process_changed::${CHATBOT_PROCESS_CHANGED}"

# # Step 4: Deploy to the affected servers (both chatbotmain and chatbotprocess if needed)
# - name: Deploy to DigitalOcean
# run: |
# ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.SSH_HOST }} << EOF
# # Check if chatbotmain needs to be redeployed
# if [ "${{ steps.check_files.outputs.main_changed }}" = "true" ]; then
# CONTAINER_ID=\$(sudo docker ps -q --filter "name=chatbotmain")
# if [ "\$CONTAINER_ID" ]; then
# echo "Stopping and removing container chatbotmain..."
# sudo docker stop \$CONTAINER_ID
# sudo docker rm \$CONTAINER_ID

# IMAGE_ID=\$(sudo docker images -q chatbotmain)
# if [ "\$IMAGE_ID" ]; then
# echo "Removing image chatbotmain..."
# sudo docker rmi -f \$IMAGE_ID
# fi
# fi

# echo "Rebuilding and running container chatbotmain..."
# sudo docker build -t chatbotmain .
# sudo docker run -d -p 8000:8000 chatbotmain
# fi

# # Check if chatbotprocess needs to be redeployed
# if [ "${{ steps.check_files.outputs.process_changed }}" = "true" ]; then
# CONTAINER_ID=\$(sudo docker ps -q --filter "name=chatbotprocess")
# if [ "\$CONTAINER_ID" ]; then
# echo "Stopping and removing container chatbotprocess..."
# sudo docker stop \$CONTAINER_ID
# sudo docker rm \$CONTAINER_ID

# IMAGE_ID=\$(sudo docker images -q chatbotprocess)
# if [ "\$IMAGE_ID" ]; then
# echo "Removing image chatbotprocess..."
# sudo docker rmi -f \$IMAGE_ID
# fi
# fi

# echo "Rebuilding and running container chatbotprocess..."
# sudo docker build -t chatbotprocess .
# sudo docker run -d -p 9000:9000 chatbotprocess
# fi
# EOF



name: CI/CD Pipeline for DigitalOcean Droplet

on:
Expand All @@ -8,7 +95,7 @@ on:
jobs:
redeploy:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the code
- name: Checkout code
Expand All @@ -27,23 +114,25 @@ jobs:
CHATBOT_MAIN_CHANGED=false
CHATBOT_PROCESS_CHANGED=false
if git diff --name-only HEAD^ HEAD | grep -q 'chatbotmain/'; then
# Compare changes using GitHub's event context for the previous commit
if git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q 'chatbotmain/'; then
CHATBOT_MAIN_CHANGED=true
fi
if git diff --name-only HEAD^ HEAD | grep -q 'chatbotprocess/'; then
if git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q 'chatbotprocess/'; then
CHATBOT_PROCESS_CHANGED=true
fi
echo "::set-output name=main_changed::${CHATBOT_MAIN_CHANGED}"
echo "::set-output name=process_changed::${CHATBOT_PROCESS_CHANGED}"
# Set the output values using environment variables
echo "main_changed=${CHATBOT_MAIN_CHANGED}" >> $GITHUB_ENV
echo "process_changed=${CHATBOT_PROCESS_CHANGED}" >> $GITHUB_ENV
# Step 4: Deploy to the affected servers (both chatbotmain and chatbotprocess if needed)
- name: Deploy to DigitalOcean
run: |
ssh -o StrictHostKeyChecking=no ubuntu@${{ secrets.SSH_HOST }} << EOF
# Check if chatbotmain needs to be redeployed
if [ "${{ steps.check_files.outputs.main_changed }}" = "true" ]; then
if [ "$main_changed" = "true" ]; then
CONTAINER_ID=\$(sudo docker ps -q --filter "name=chatbotmain")
if [ "\$CONTAINER_ID" ]; then
echo "Stopping and removing container chatbotmain..."
Expand All @@ -58,12 +147,13 @@ jobs:
fi
echo "Rebuilding and running container chatbotmain..."
cd /path/to/chatbotmain # Navigate to the correct directory if necessary
sudo docker build -t chatbotmain .
sudo docker run -d -p 8000:8000 chatbotmain
fi
# Check if chatbotprocess needs to be redeployed
if [ "${{ steps.check_files.outputs.process_changed }}" = "true" ]; then
if [ "$process_changed" = "true" ]; then
CONTAINER_ID=\$(sudo docker ps -q --filter "name=chatbotprocess")
if [ "\$CONTAINER_ID" ]; then
echo "Stopping and removing container chatbotprocess..."
Expand All @@ -78,7 +168,12 @@ jobs:
fi
echo "Rebuilding and running container chatbotprocess..."
cd /path/to/chatbotprocess # Navigate to the correct directory if necessary
sudo docker build -t chatbotprocess .
sudo docker run -d -p 9000:9000 chatbotprocess
fi
EOF
env:
main_changed: ${{ steps.check_files.outputs.main_changed }}
process_changed: ${{ steps.check_files.outputs.process_changed }}

0 comments on commit 7fe3a02

Please sign in to comment.