Merge pull request #265 from wildan3105/style/update-home-caption #34
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 | |
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: "" | |
TOKEN: "" | |
ENV: "" | |
with: | |
host: ${{ secrets.VPS_HOST }} | |
username: ${{ secrets.VPS_USERNAME }} | |
password: ${{ secrets.VPS_PASSWORD }} | |
port: 22 | |
envs: ACTIVE_ENV,TARGET_ENV,TARGET_DIR,TARGET_PORT, TOKEN, ENV | |
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 ${{ env.PROJECT_REPO }} $TARGET_DIR | |
echo "Repository cloned" | |
echo "Writing to environment variables..." | |
export ENV="${{ secrets.ENV }}" | |
export TOKEN="${{ secrets.TOKEN }}" | |
echo "Environment variables written" | |
echo "Install and build application..." | |
cd $TARGET_DIR | |
npm install | |
echo "Dependencies installed and app built" | |
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..." | |
PORT=$TARGET_PORT pm2 start npm --name "$TARGET_ENV" -- run start || { 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:$TARGET_PORT;|" "${{ env.NGINX_CONF }}" | |
sudo nginx -s reload | |
echo "Nginx updated" |