From 91c73b197e71ca70621ea61e345a53d8c731fb4b Mon Sep 17 00:00:00 2001 From: ekedonald Date: Sun, 21 Jul 2024 16:41:13 +0100 Subject: [PATCH 1/2] "Updated GitHub workflows for development, production, and staging environments to use a script for deployment and added pull request triggers." --- .github/workflows/development.yml | 39 +++++------------ .github/workflows/production.yml | 37 ++++------------ .github/workflows/staging.yml | 39 +++++------------ scripts/deploy_app.sh | 70 +++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 86 deletions(-) create mode 100644 scripts/deploy_app.sh diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 25658b14..0aeb7057 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -1,9 +1,12 @@ -name: Build, Test, and Deploy for Development +name: Build, Test, and Deploy for Production 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..ee3b6ef1 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,9 +1,12 @@ -name: Build, Test, and Deploy for Staging +name: Build, Test, and Deploy for Production 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 From 29cc52b06ad5a6967888fdd563b53a4011191cd4 Mon Sep 17 00:00:00 2001 From: ekedonald Date: Sun, 21 Jul 2024 17:20:15 +0100 Subject: [PATCH 2/2] "Updated workflow names in development.yml and staging.yml" --- .github/workflows/development.yml | 2 +- .github/workflows/staging.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index 0aeb7057..db3ad1d6 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -1,4 +1,4 @@ -name: Build, Test, and Deploy for Production +name: Build, Test, and Deploy for Development on: push: diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml index ee3b6ef1..e13eb1db 100644 --- a/.github/workflows/staging.yml +++ b/.github/workflows/staging.yml @@ -1,4 +1,4 @@ -name: Build, Test, and Deploy for Production +name: Build, Test, and Deploy for Staging on: push: