-
Notifications
You must be signed in to change notification settings - Fork 0
145 lines (123 loc) · 5.35 KB
/
deploy.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
name: Deploying to Production (Google Cloud Platform VM)
on:
push:
branches:
- master
pull_request:
branches:
- development
types:
- closed
jobs:
deploy:
runs-on: ubuntu-latest
if: github.event_name == 'push' || (github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'master')
environment: 'production'
env:
APP_URL: ${{ vars.APP_URL }}
APP_TITLE: ${{ vars.APP_TITLE }}
APP_AUTHOR: ${{ vars.APP_AUTHOR }}
APP_DESCRIPTION: ${{ vars.APP_DESCRIPTION }}
APP_METADATA_KEYWORDS: ${{ vars.APP_METADATA_KEYWORDS }}
APP_METADATA_OPENGRAPH_SITENAME: ${{ vars.APP_METADATA_OPENGRAPH_SITENAME }}
steps:
- name: Checkout source code
uses: actions/checkout@v3
- name: Print environment variables (Debugging)
run: |
echo "APP_URL: $APP_URL"
echo "APP_TITLE: $APP_TITLE"
echo "APP_AUTHOR: $APP_AUTHOR"
echo "APP_DESCRIPTION: $APP_DESCRIPTION"
echo "APP_METADATA_KEYWORDS: $APP_METADATA_KEYWORDS"
echo "APP_METADATA_OPENGRAPH_SITENAME: $APP_METADATA_OPENGRAPH_SITENAME"
- name: Cache npm and Next.js build cache
uses: actions/cache@v4
with:
path: |
~/.npm
${{ github.workspace }}/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
- name: Install project production dependencies
run: npm ci
- name: Build the project for production environment
run: |
echo "APP_URL=$APP_URL" > .env
echo "APP_TITLE=$APP_TITLE" >> .env
echo "APP_AUTHOR=$APP_AUTHOR" >> .env
echo "APP_DESCRIPTION=$APP_DESCRIPTION" >> .env
echo "APP_METADATA_KEYWORDS=$APP_METADATA_KEYWORDS" >> .env
echo "APP_METADATA_OPENGRAPH_SITENAME=$APP_METADATA_OPENGRAPH_SITENAME" >> .env
npm run build
- name: Configuring Directory Permissions of the server
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
VM_USERNAME: ${{ secrets.VM_USERNAME }}
VM_IP: ${{ secrets.VM_IP }}
run: |
echo "$SSH_PRIVATE_KEY" > key.pem
chmod 600 key.pem
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP "sudo mkdir -p /var/www/website-portfolio/.next"
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP "sudo chown -R $VM_USERNAME:$VM_USERNAME /var/www/website-portfolio"
- name: Sync files to server and Configure nginx
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
VM_USERNAME: ${{ secrets.VM_USERNAME }}
VM_IP: ${{ secrets.VM_IP }}
run: |
echo "$SSH_PRIVATE_KEY" > key.pem
chmod 600 key.pem
rsync -avz -e "ssh -i key.pem -o StrictHostKeyChecking=no" --delete .env .next node_modules public package.json package-lock.json $VM_USERNAME@$VM_IP:/var/www/website-portfolio/
rsync -avz -e "ssh -i key.pem -o StrictHostKeyChecking=no" deploy/nginx.conf $VM_USERNAME@$VM_IP:/tmp/nginx.conf
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP << 'EOF'
sudo mv /tmp/nginx.conf /etc/nginx/sites-available/website-portfolio
sudo ln -sf /etc/nginx/sites-available/website-portfolio /etc/nginx/sites-enabled/
sudo nginx -t
sudo nginx -s reload
EOF
# - name: Configure environment variables on the server
# run: |
# echo "$SSH_PRIVATE_KEY" > key.pem
# chmod 600 key.pem
# ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP << EOF
# echo "APP_URL=$APP_URL" > /var/www/website-portfolio/.env
# echo "APP_TITLE=$APP_TITLE" >> /var/www/website-portfolio/.env
# echo "APP_AUTHOR=$APP_AUTHOR" >> /var/www/website-portfolio/.env
# echo "APP_DESCRIPTION=$APP_DESCRIPTION" >> /var/www/website-portfolio/.env
# echo "APP_METADATA_KEYWORDS=$APP_METADATA_KEYWORDS" >> /var/www/website-portfolio/.env
# echo "APP_METADATA_OPENGRAPH_SITENAME=$APP_METADATA_OPENGRAPH_SITENAME" >> /var/www/website-portfolio/.env
# EOF
- name: Configure and Start application
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
VM_USERNAME: ${{ secrets.VM_USERNAME }}
VM_IP: ${{ secrets.VM_IP }}
run: |
echo "$SSH_PRIVATE_KEY" > key.pem
chmod 600 key.pem
ssh -i key.pem -o StrictHostKeyChecking=no $VM_USERNAME@$VM_IP << EOF
cd /var/www/website-portfolio
# Ensure PM2 is installed
if ! command -v pm2 &> /dev/null
then
echo "PM2 not found, installing..."
sudo npm install -g pm2
else
echo "PM2 is already installed, continue;"
fi
# Delete existing PM2 process if it exists
if pm2 list | grep -q "website-portfolio"
then
echo "Deleting existing PM2 process..."
pm2 delete website-portfolio
else
echo "PM2 process not found, continue;"
fi
# Start the Next.js application with PM2
pm2 start npm --name "website-portfolio" -- start || true
# Save the PM2 process list
pm2 save
EOF
shell: bash