Skip to content

ci: fix sed

ci: fix sed #10

name: Deploy to VPS
on:
push:
branches:
- chore/blue-green-deployment
jobs:
deploy:
runs-on: ubuntu-latest
env:
PROJECT_REPO: [email protected]:wildan3105/github-langs.git
BLUE_NAME: "blue"
GREEN_NAME: "green"
BLUE_PORT: 3000
GREEN_PORT: 3001
NGINX_CONF: /etc/nginx/sites-available/gitstats.wildans.site
BLUE_DIR: /var/www/html/blue
GREEN_DIR: /var/www/html/green
steps:
- name: Check out the code
uses: actions/checkout@v3
- name: Deployment
uses: appleboy/[email protected]
env:
ACTIVE_ENV: ""
TARGET_ENV: ""
TARGET_DIR: ""
TARGET_PORT: ""
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USERNAME }}
password: ${{ secrets.VPS_PASSWORD }}
port: 22
envs: ACTIVE_ENV,TARGET_ENV,TARGET_DIR,TARGET_PORT
script: |
echo "Deployment using blue-green is started..."
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
echo "Determining the current active environment..."
if grep -q "proxy_pass http://localhost:${{ env.BLUE_PORT }};" "${{ env.NGINX_CONF }}"; then
export ACTIVE_ENV="${{ env.BLUE_NAME }}"
elif grep -q "proxy_pass http://localhost:${{ env.GREEN_PORT }};" "${{ env.NGINX_CONF }}"; then
export ACTIVE_ENV="${{ env.GREEN_NAME }}"
else
echo "error: could not determine active environment" >&2
exit 1
fi
echo "Active env is $ACTIVE_ENV"
echo "Set the target environment..."
if [ "$ACTIVE_ENV" == "blue" ]; then
export TARGET_ENV="${{ env.GREEN_NAME }}"
export TARGET_PORT="${{ env.GREEN_PORT }}"
export TARGET_DIR="${{ env.GREEN_DIR }}"
elif [ "$ACTIVE_ENV" == "green" ]; then
export TARGET_ENV="${{ env.BLUE_NAME }}"
export TARGET_PORT="${{ env.BLUE_PORT }}"
export TARGET_DIR="${{ env.BLUE_DIR }}"
else
echo "error: could not determine target environment" >&2
exit 1
fi
echo "The upcoming target environment is as follows:"
echo "Target env: $TARGET_ENV"
echo "Target port: $TARGET_PORT"
echo "Target dir: $TARGET_DIR"
echo "Cloning the repository..."
rm -rf $TARGET_DIR
mkdir -p $TARGET_DIR
git clone -b chore/blue-green-deployment ${{ env.PROJECT_REPO }} $TARGET_DIR
echo "Repository cloned"
echo "Writing to environment variables..."
echo "ENV=${{ secrets.ENV }} >> $TARGET_DIR/.env
echo "TOKEN=${{ secrets.TOKEN }}" >> $TARGET_DIR/.env
echo "PORT=$TARGET_PORT" >> $TARGET_DIR/.env
echo "Environment variables written"
echo "Install and build application..."
cd $TARGET_DIR
npm install
echo "Dependencies installed and app built"
echo "Reading from env variables..."
if [ -f ".env" ]; then
export $(grep -v '^#' .env | xargs)
else
echo "Error: .env file not found!"
exit 1
fi
echo "Start or restart application via PM2..."
if pm2 list | grep -q "$TARGET_ENV"; then
echo "App detected. Restarting the app..."
pm2 restart "$TARGET_ENV" || { echo "Failed to restart app"; exit 1; }
else
echo "App is not started yet. Starting the app..."
pm2 start npm --name "$TARGET_ENV" -- run start -- -p "$TARGET_PORT" || { echo "Failed to start app"; exit 1; }
fi
echo "Application started/restarted"
echo "Adding delay to ensure the app is ready..."
sleep 10s
echo "Delay finished"
echo "Update nginx port..."
sudo sed -i '' "s|proxy_pass http://localhost:[0-9]*;|proxy_pass http://localhost:$port;|" "${{ env.NGINX_CONF }}"
sudo nginx -s reload
echo "Nginx updated"