Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated GitHub workflows for development, production, and staging environments to use a script for deployment and added pull request triggers. #69

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- dev
pull_request:
branches:
- dev

jobs:
build:
Expand Down Expand Up @@ -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
Expand All @@ -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 }}
37 changes: 9 additions & 28 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
Expand Down Expand Up @@ -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
Expand All @@ -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 }}
37 changes: 9 additions & 28 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- staging
pull_request:
branches:
- staging

jobs:
build:
Expand Down Expand Up @@ -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
Expand All @@ -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 }}
70 changes: 70 additions & 0 deletions scripts/deploy_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

set -e

if [ $# -lt 2 ]; then
echo "Usage: $0 <development|staging|production> <github.repository> [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
Loading