diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 25658b14..db3ad1d6 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -4,6 +4,9 @@ on: push: branches: - dev + pull_request: + branches: + - dev jobs: build: @@ -54,7 +57,11 @@ jobs: SSH_HOST: ${{ secrets.SSH_HOST }} SSH_PORT: ${{ secrets.SSH_PORT }} SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} - PROCESS_NAME: run_development_app + SERVER_PORT: 7000 + DB_NAME: "development_db" + USERNAME: "development_user" + APP_NAME: "development" + APP_URL: "http://localhost:7000" steps: - name: SSH into server and deploy @@ -65,30 +72,4 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - export APPROOT=~/deployments/development - export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin - export PATH=$PATH:/usr/local/go/bin - - mkdir -p $APPROOT - cd $APPROOT - - if [ -d "$APPROOT/.git" ]; then - # Navigate to the repository directory and pull changes - cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } - git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin dev || { echo "Failed to pull latest changes"; exit 1; } - else - git clone -b dev http://github.com/${{ github.repository }} . || { echo "Failed to clone repository"; exit 1; } - fi - - cp app-sample.env app.env - go build -o ~/deployments/development/development_app - - # Check if pm2 is already running - if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then - echo "Process ${{ env.PROCESS_NAME }} is running. Restarting..." - pm2 restart "${{ env.PROCESS_NAME }}" - else - echo "Process ${{ env.PROCESS_NAME }} is not running. Starting..." - pm2 start "${{ env.PROCESS_NAME }}".sh - fi + bash ~/scripts/deploy_app.sh development SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index 06325392..42447b24 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -4,6 +4,9 @@ on: push: branches: - main + pull_request: + branches: + - main jobs: build: @@ -54,7 +57,11 @@ jobs: SSH_HOST: ${{ secrets.SSH_HOST }} SSH_PORT: ${{ secrets.SSH_PORT }} SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} - PROCESS_NAME: run_production_app + SERVER_PORT: 9000 + DB_NAME: "production_db" + USERNAME: "production_user" + APP_NAME: "production" + APP_URL: "http://localhost:9000" steps: - name: SSH into server and deploy @@ -65,30 +72,4 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - export APPROOT=~/deployments/production - export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin - export PATH=$PATH:/usr/local/go/bin - - mkdir -p $APPROOT - cd $APPROOT - - if [ -d "$APPROOT/.git" ]; then - # Navigate to the repository directory and pull changes - cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } - git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin main || { echo "Failed to pull latest changes"; exit 1; } - else - git clone -b main http://github.com/${{ github.repository }} . || { echo "Failed to clone repository"; exit 1; } - fi - - cp app-sample.env app.env - go build -o ~/deployments/production/production_app - - # Check if pm2 is already running - if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then - echo "Process ${{ env.PROCESS_NAME }} is running. Restarting..." - pm2 restart "${{ env.PROCESS_NAME }}" - else - echo "Process ${{ env.PROCESS_NAME }} is not running. Starting..." - pm2 start "${{ env.PROCESS_NAME }}".sh - fi + bash ~/scripts/deploy_app.sh production SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index f4b49264..e13eb1db 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -4,6 +4,9 @@ on: push: branches: - staging + pull_request: + branches: + - staging jobs: build: @@ -54,7 +57,11 @@ jobs: SSH_HOST: ${{ secrets.SSH_HOST }} SSH_PORT: ${{ secrets.SSH_PORT }} SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} - PROCESS_NAME: run_staging_app + SERVER_PORT: 8000 + DB_NAME: "staging_db" + USERNAME: "staging_user" + APP_NAME: "staging" + APP_URL: "http://localhost:8000" steps: - name: SSH into server and deploy @@ -65,30 +72,4 @@ jobs: password: ${{ env.SSH_PASSWORD }} port: ${{ env.SSH_PORT }} script: | - export APPROOT=~/deployments/staging - export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin - export PATH=$PATH:/usr/local/go/bin - - mkdir -p $APPROOT - cd $APPROOT - - if [ -d "$APPROOT/.git" ]; then - # Navigate to the repository directory and pull changes - cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } - git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } - git pull origin staging || { echo "Failed to pull latest changes"; exit 1; } - else - git clone -b staging http://github.com/${{ github.repository }} . || { echo "Failed to clone repository"; exit 1; } - fi - - cp app-sample.env app.env - go build -o ~/deployments/staging/staging_app - - # Check if pm2 is already running - if pm2 list | grep -q "${{ env.PROCESS_NAME }}"; then - echo "Process ${{ env.PROCESS_NAME }} is running. Restarting..." - pm2 restart "${{ env.PROCESS_NAME }}" - else - echo "Process ${{ env.PROCESS_NAME }} is not running. Starting..." - pm2 start "${{ env.PROCESS_NAME }}".sh - fi + bash ~/scripts/deploy_app.sh staging SERVER_PORT=${{ env.SERVER_PORT }} DB_NAME=${{ env.DB_NAME }} USERNAME=${{ env.USERNAME }} APP_NAME=${{ env.APP_NAME }} APP_URL=${{ env.APP_URL }} diff --git a/scripts/deploy_app.sh b/scripts/deploy_app.sh new file mode 100644 index 00000000..06a8c9e6 --- /dev/null +++ b/scripts/deploy_app.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +set -e + +if [ $# -lt 2 ]; then + echo "Usage: $0 [env_vars...]" + exit 1 +fi + +ENV=$1 +GITHUB_REPOSITORY=$2 +shift 2 + +case $ENV in + development) + BRANCH="dev" + APPROOT=~/deployments/development + APP_NAME="development_app" + ;; + staging) + BRANCH="staging" + APPROOT=~/deployments/staging + APP_NAME="staging_app" + ;; + production) + BRANCH="main" + APPROOT=~/deployments/production + APP_NAME="production_app" + ;; + *) + echo "Invalid environment: $ENV" + exit 1 + ;; +esac + +export APPROOT +export PATH=$PATH:~/.nvm/versions/node/v20.15.1/bin +export PATH=$PATH:/usr/local/go/bin + +mkdir -p $APPROOT +cd $APPROOT + +if [ -d "$APPROOT/.git" ]; then + # Navigate to the repository directory and pull changes + cd $APPROOT || { echo "Failed to navigate to web root directory"; exit 1; } + git reset --hard HEAD || { echo "Failed to reset local changes"; exit 1; } + git pull origin $BRANCH || { echo "Failed to pull latest changes"; exit 1; } +else + git clone -b $BRANCH https://github.com/$GITHUB_REPOSITORY . || { echo "Failed to clone repository"; exit 1; } +fi + +# Replace environment variables in app.env +cp app-sample.env app.env +for VAR in "$@" +do + KEY=$(echo $VAR | cut -d '=' -f 1) + VALUE=$(echo $VAR | cut -d '=' -f 2) + sed -i "s|\${env.$KEY}|$VALUE|g" app.env +done + +go build -o $APPROOT/$APP_NAME + +# Start or restart the built binary with pm2 +if pm2 list | grep -qw "$APP_NAME"; then + echo "Process $APP_NAME is running. Restarting..." + pm2 restart $APPROOT/$APP_NAME +else + echo "Process $APP_NAME is not running. Starting..." + pm2 start $APPROOT/$APP_NAME --name $APP_NAME +fi \ No newline at end of file