-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from hngprojects/main
Update staging with commits from main
- Loading branch information
Showing
12 changed files
with
215 additions
and
47 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
name: Build, Test, and Deploy for Staging | ||
|
||
on: | ||
push: | ||
branches: | ||
- staging | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v4 | ||
- name: Set Golang | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: "1.22.1" | ||
- name: Build the application | ||
run: go build -o staging_app | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
needs: build | ||
env: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: password | ||
POSTGRES_DB: db_name | ||
services: | ||
postgres: | ||
image: postgres:latest | ||
env: | ||
POSTGRES_USER: ${{ env.POSTGRES_USER }} | ||
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }} | ||
POSTGRES_DB: ${{ env.POSTGRES_DB }} | ||
ports: | ||
- 5432:5432 | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v4 | ||
- name: Create the app config file | ||
run: cp app-sample.env app.env | ||
- name: Run The Project | ||
run: nohup go run main.go > /dev/null 2>&1 & | ||
- name: Wait for application to start | ||
run: sleep 30s | ||
- name: Test for reachability | ||
run: curl http://localhost:8019 | ||
- name: Run All Tests | ||
run: go test ./... -timeout 99999s | ||
|
||
deploy: | ||
runs-on: ubuntu-latest | ||
needs: test | ||
env: | ||
SSH_USERNAME: ${{ secrets.SSH_USERNAME }} | ||
SSH_HOST: ${{ secrets.SSH_HOST }} | ||
SSH_PORT: ${{ secrets.SSH_PORT }} | ||
SSH_PASSWORD: ${{ secrets.SSH_PASSWORD }} | ||
PROCESS_NAME: run_staging_app | ||
|
||
steps: | ||
- name: SSH into server and deploy | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ env.SSH_HOST }} | ||
username: ${{ env.SSH_USERNAME }} | ||
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 main || { 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 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module.exports = { | ||
apps: [ | ||
{ | ||
name: "run_production_app", | ||
script: "/home/vicradon/deployments/production/production_app", | ||
env: { | ||
SERVER_PORT: 9000, | ||
DB_NAME: "production_db", | ||
USERNAME: "production_user", | ||
APP_NAME: "production", | ||
APP_URL: "http://localhost:9000", | ||
}, | ||
}, | ||
{ | ||
name: "run_staging_app", | ||
script: "/home/vicradon/deployments/staging/staging_app", | ||
env: { | ||
SERVER_PORT: 8000, | ||
DB_NAME: "staging_db", | ||
USERNAME: "staging_user", | ||
APP_NAME: "staging", | ||
APP_URL: "http://localhost:8000", | ||
}, | ||
}, | ||
{ | ||
name: "run_development_app", | ||
script: "/home/vicradon/deployments/development/development_app", | ||
env: { | ||
SERVER_PORT: 7000, | ||
DB_NAME: "development_db", | ||
USERNAME: "development_user", | ||
APP_NAME: "development", | ||
APP_URL: "http://localhost:7000", | ||
}, | ||
}, | ||
], | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
~/deployments/development/development_app |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
~/deployments/production/production_app |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
~/deployments/staging/staging_app |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
#! /bin/bash | ||
WEB_ROOT="/var/www/production" | ||
CONFIG_FILE="/etc/nginx/conf.d/production.conf" | ||
DOMAIN_OR_IP="91.229.239.238" | ||
#!/bin/bash | ||
|
||
sudo mkdir -p $WEB_ROOT | ||
# Define environment configurations | ||
declare -A environments | ||
environments=( | ||
["development"]="7000 deployment.api-golang.boilerplate.hng.tech" | ||
["staging"]="8000 staging.api-golang.boilerplate.hng.tech" | ||
["production"]="9000 api-golang.boilerplate.hng.tech" | ||
) | ||
|
||
# General setup | ||
sudo apt update | ||
sudo apt install -y nginx | ||
|
||
# Configuring Nginx Reverse Proxy for the Production Server | ||
content='server { | ||
listen 80; | ||
server_name 91.229.239.238; | ||
location / { | ||
proxy_pass http://127.0.0.1:8019; | ||
proxy_set_header Host $host; | ||
proxy_set_header X-Real-IP $remote_addr; | ||
} | ||
}' | ||
# Create directories and configure Nginx for each environment | ||
for env in "${!environments[@]}"; do | ||
# Split the value into port and domain using parameter expansion | ||
port="${environments[$env]%% *}" | ||
domain="${environments[$env]#* }" | ||
web_root="~/deployments/$env" | ||
config_file="/etc/nginx/conf.d/$env.conf" | ||
|
||
# Create web root directory | ||
sudo mkdir -p $web_root | ||
|
||
echo "$content" | sudo tee $CONFIG_FILE > /dev/null | ||
sudo chmod 664 $CONFIG_FILE | ||
# Nginx configuration content | ||
content="server { | ||
listen 80; | ||
server_name $domain; | ||
location / { | ||
proxy_pass http://127.0.0.1:$port; | ||
proxy_set_header Host \$host; | ||
proxy_set_header X-Real-IP \$remote_addr; | ||
} | ||
}" | ||
|
||
# Write Nginx configuration file | ||
echo "$content" | sudo tee $config_file > /dev/null | ||
sudo chmod 664 $config_file | ||
done | ||
|
||
# Delete Default Nginx Webpage | ||
sudo rm /etc/nginx/sites-available/default && sudo rm /etc/nginx/sites-enabled/default | ||
|
||
# Validate the Configuration | ||
sudo nginx -t | ||
sudo systemctl restart nginx | ||
echo "PRODUCTION SERVER URL: http://$DOMAIN_OR_IP" | ||
|
||
# Output | ||
echo "Nginx setup for the applications to reverse proxy requests to them" |
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