Merge pull request #247 from wildan3105/dependabot/npm_and_yarn/multi… #15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to VPS | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out the code | |
uses: actions/checkout@v3 | |
- name: Verify SSH connection and deploy | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.VPS_HOST }} | |
username: ${{ secrets.VPS_USERNAME }} | |
password: ${{ secrets.VPS_PASSWORD }} | |
port: 22 | |
script: | | |
# Set paths for npm and node | |
export PATH=$PATH:/root/.nvm/versions/node/v18.10.0/bin | |
# Define directories | |
TARGET_DIR="/var/www/html" | |
TEMP_DIR="${TARGET_DIR}/tmp" | |
SYMLINK="${TARGET_DIR}/current" | |
# Prepare the temporary area | |
echo "Preparing the temporary deployment directory..." | |
sudo rm -rf $TEMP_DIR | |
sudo mkdir -p $TEMP_DIR | |
# Clone the repo into the temporary directory | |
echo "Cloning repository to the temporary directory..." | |
git clone [email protected]:wildan3105/github-langs.git $TEMP_DIR | |
# Ensure necessary files are present | |
if [ -f "$TEMP_DIR/src/index.js" ]; then | |
echo "Entry point found in the temporary directory" | |
else | |
echo "Error: Entry point not found in the temporary directory!" | |
exit 1 | |
fi | |
# Set environment variables | |
export ENV=${{ secrets.ENV }} | |
export TOKEN=${{ secrets.TOKEN }} | |
# Install dependencies | |
echo "Installing dependencies..." | |
cd $TEMP_DIR | |
npm install | |
# Gracefully stop the existing application if it's running on port 5000 | |
if lsof -i:5000; then | |
echo "Stopping existing application running on port 5000..." | |
PID=$(lsof -t -i:5000) | |
sudo kill -9 $PID || echo "Failed to kill process with PID $PID" | |
fi | |
# Start the application in the background using Node | |
echo "Starting the application..." | |
ENV=$ENV TOKEN=$TOKEN npm run start-prod | |
# Switch symlink atomically | |
echo "Updating symlink to point to the new temporary directory..." | |
sudo ln -sfn $TEMP_DIR $SYMLINK | |
# Verify that the symlink points to the latest deployment | |
echo "Deployment verification:" | |
ls -l $SYMLINK |