0% found this document useful (0 votes)
4 views

origin finl

This document outlines a GitHub Actions workflow for deploying a backend application to a server upon a push to the main branch. It includes steps for checking out the repository, managing the PM2 process, cloning the repository, installing dependencies, and verifying the backend is running correctly. The deployment process ensures that any existing application directory is removed before deploying the new version and starts the application using PM2 for process management.

Uploaded by

shivamsharmash03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

origin finl

This document outlines a GitHub Actions workflow for deploying a backend application to a server upon a push to the main branch. It includes steps for checking out the repository, managing the PM2 process, cloning the repository, installing dependencies, and verifying the backend is running correctly. The deployment process ensures that any existing application directory is removed before deploying the new version and starts the application using PM2 for process management.

Uploaded by

shivamsharmash03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

name: Deploy Backend

on:
push:
branches:
- main # Trigger deployment on push to the main branch

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: SSH to server and deploy


uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |

set -e # Exit on any error

# Check PM2 status and stop the process if it's online


if command -v pm2 &> /dev/null; then
if pm2 list | grep -q 'origin-api'; then
pm2 stop origin-api # Stop the PM2 process if it's running
fi
else
echo "PM2 not found. Installing..."
npm install -g pm2
fi

# Ensure the directory does not exist or remove it


if [ -d "/var/www/origin-api.intelchain.org" ]; then
echo "Removing existing directory..."
rm -rf /var/www/origin-api.intelchain.org
fi

# Clone the repository


echo "Cloning the repository..."
git clone -b main [email protected]:intelchain-itc/origin-backend.git
/var/www/origin-api.intelchain.org/origin-backend

# Move the contents of origin-backend to the parent directory


echo "Moving repository contents..."
mv /var/www/origin-api.intelchain.org/origin-backend/* /var/www/origin-
api.intelchain.org/
mv /var/www/origin-api.intelchain.org/origin-backend/.[^.]*
/var/www/origin-api.intelchain.org/ || true # Move hidden files (e.g., .git, .env)

# Remove the now-empty origin-backend directory


echo "Removing origin-backend directory..."
rm -rf /var/www/origin-api.intelchain.org/origin-backend

# Navigate to the backend directory


cd /var/www/origin-api.intelchain.org
if ! command -v corepack &> /dev/null; then
echo "Installing Corepack..."
npm install -g corepack
fi

echo "Enabling Corepack..."


corepack enable

# Prepare Yarn version


echo "Preparing Yarn..."
corepack prepare [email protected] --activate

# Install dependencies
echo "Installing dependencies..."
yarn install

# Verify that src/index.js exists


if [ ! -f "src/index.js" ]; then
echo "Error: src/index.js not found in the directory."
exit 1
fi

# Start the backend temporarily to check the port


echo "Starting the backend temporarily..."
node src/index.js &
TEMP_PID=$!

# Wait for the server to start


sleep 5

# Check if the backend is running on port 5000


if nc -zv localhost 5000 2>/dev/null; then
echo "Backend is running on port 5000."

# Stop the temporary process


kill $TEMP_PID

# Start the backend with PM2


echo "Starting the backend with PM2..."
pm2 start src/index.js --name origin-api
pm2 save
else
echo "Backend is not running on port 5000. Exiting."
kill $TEMP_PID || true
exit 1

fi

echo "Deployment completed successfully!"

You might also like