Skip to content

Commit

Permalink
Adding features in deploy-backend.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Frnn4268 committed May 30, 2024
1 parent a4ee47b commit 86a0fbf
Showing 1 changed file with 25 additions and 30 deletions.
55 changes: 25 additions & 30 deletions .github/workflows/deploy-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ on:
push:
branches:
- master # Use the branch you use for production

jobs:
host-1:
runs-on: chat-easypark-deploy
deploy-host-1:
runs-on: chat-easypark-deploy
steps:
- uses: actions/checkout@v3

# Setup Node.js with version 20

- name: Setup Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 20

# Install dependencies
node-version: '20'

- name: Install Dependencies
working-directory: server
run: |
run: |
npm install
echo MONGO_URL="mongodb+srv://Frnn:noobculo@mongocluster.o2eojyk.mongodb.net/snappy?retryWrites=true&w=majority" > .env
# Set up SSH key from GitHub Secrets
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
Expand All @@ -35,37 +32,35 @@ jobs:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_HOST: ${{ secrets.SSH_HOST1 }}

# Deploy to remote host via SSH
- name: Deploy to Remote Server
working-directory: server
run: |
echo "====== Deploy to production server ======"
echo "====== Sync local and remote directory ======"
rsync -azPq -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --delete ./build/ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST1 }}:/home/ubuntu/backend/ --rsync-path="sudo rsync"
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST1 }} 'pm2 delete backend && pm2 start index.js --name backend'
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST1 }} 'sudo mkdir -p /home/ubuntu/backend && sudo chown -R $USER:$USER /home/ubuntu/backend'
rsync -azPq -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --delete ./ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST1 }}:/home/ubuntu/backend/ --rsync-path="sudo rsync"
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST1 }} 'pm2 delete backend || true && pm2 start index.js --name backend'
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST1 }} 'sudo systemctl restart pm2'
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_HOST: ${{ secrets.SSH_HOST1 }}
host-2:
runs-on: chat-easypark-deploy

deploy-host-2:
runs-on: chat-easypark-deploy
steps:
- uses: actions/checkout@v3

# Setup Node.js with version 20

- name: Setup Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: 20

# Install dependencies
node-version: '20'

- name: Install Dependencies
working-directory: server
run: |
run: |
npm install
echo MONGO_URL="mongodb+srv://Frnn:noobculo@mongocluster.o2eojyk.mongodb.net/snappy?retryWrites=true&w=majority" > .env
# Set up SSH key from GitHub Secrets
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
Expand All @@ -76,15 +71,15 @@ jobs:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SSH_HOST: ${{ secrets.SSH_HOST2 }}

# Deploy to remote host via SSH
- name: Deploy to Remote Server
working-directory: server
run: |
echo "====== Deploy to production server ======"
echo "====== Sync local and remote directory ======"
rsync -azPq -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --delete ./build/ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST2 }}:/home/ubuntu/backend/ --rsync-path="sudo rsync"
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST2 }} 'pm2 delete backend && pm2 start index.js --name backend'
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST2 }} 'sudo mkdir -p /home/ubuntu/backend && sudo chown -R $USER:$USER /home/ubuntu/backend'
rsync -azPq -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --delete ./ ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST2 }}:/home/ubuntu/backend/ --rsync-path="sudo rsync"
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST2 }} 'pm2 delete backend || true && pm2 start index.js --name backend'
ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST2 }} 'sudo systemctl restart pm2'
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_HOST: ${{ secrets.SSH_HOST2 }}
SSH_HOST: ${{ secrets.SSH_HOST2 }}

0 comments on commit 86a0fbf

Please sign in to comment.