Last active
November 20, 2024 22:11
-
-
Save wildan3105/f9b874dc5c942f02c1a1dd5d1d85f012 to your computer and use it in GitHub Desktop.
Example of blue-green deployment script using GitHub action pipeline
This file contains hidden or 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: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
env: | |
PROJECT_REPO: [email protected]:<your-github-username>/<repo-name>.git # your git repository URL | |
BLUE_NAME: "blue" | |
GREEN_NAME: "green" | |
BLUE_PORT: 3000 # your blue environment port. ensure it's not used by any existing process | |
GREEN_PORT: 3001 # your green environment port. ensure it's not used by any existing process | |
NGINX_CONF: /etc/nginx/sites-available/<your-site> # your site's nginx configuration file | |
BLUE_DIR: /var/www/html/blue # your blue environment directory (customizable) | |
GREEN_DIR: /var/www/html/green # your green environment directory (customizable) | |
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 # list of environments used in this step | |
script: | | |
echo "Deployment using blue-green is started..." | |
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..." # your environment variables, if any | |
echo "ENV_KEY=${{ secrets.ENV_KEY }}" >> $TARGET_DIR/.env | |
echo "Environment variables written" | |
echo "Install and build application..." # your install and build app stage (if any) | |
cd $TARGET_DIR | |
npm install | |
npm run build | |
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..." | |
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:$TARGET_PORT;|" "${{ env.NGINX_CONF }}" | |
sudo nginx -s reload | |
echo "Nginx updated" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment