-
Notifications
You must be signed in to change notification settings - Fork 50
94 lines (85 loc) · 2.89 KB
/
production.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
name: Build, Test, and Deploy for Production
on:
push:
branches:
- main
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 production_app
test:
runs-on: ubuntu-latest
needs: build
env:
TEST_USERNAME: postgres
TEST_PASSWORD: password
TEST_DB_NAME: db_name
TEST_DB_HOST: localhost
TEST_DB_PORT: 5432
TEST_DB_CONNECTION: pgsql
TEST_TIMEZONE: Africa/Lagos
TEST_SSLMODE: disable
TEST_MIGRATE: true
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: ${{ env.TEST_USERNAME }}
POSTGRES_PASSWORD: ${{ env.TEST_PASSWORD }}
POSTGRES_DB: ${{ env.TEST_DB_NAME }}
ports:
- 5432:5432
steps:
- name: Checkout Code
uses: actions/checkout@v4
- 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_production_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/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