Skip to content

Commit 18ef64d

Browse files
committed
new workflow deploy.yml config
1 parent 4bebbd5 commit 18ef64d

5 files changed

Lines changed: 95 additions & 132 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,60 @@
1-
name: Next.js Deployment ke cPanel via SSH
1+
name: 🚀 Deploy Next.js to cPanel via SSH
22

33
on:
44
push:
55
branches:
6-
- main # Ganti jika branch utama Anda berbeda
6+
- main
77

88
jobs:
99
deploy:
1010
runs-on: ubuntu-latest
11+
1112
steps:
12-
- name: 1. Checkout Kode Sumber
13+
- name: 🧭 Checkout Repository
1314
uses: actions/checkout@v4
1415

15-
- name: 2. Setup Node.js Environment
16+
- name: 🟢 Setup Node.js
1617
uses: actions/setup-node@v4
1718
with:
18-
node-version: "22"
19-
cache: "npm"
19+
node-version: 22
2020

21-
- name: 3. Instal Dependencies dan Lakukan Build (Di Runner GitHub)
22-
run: |
23-
npm ci
24-
npm run build
25-
# Pengecekan: Pastikan folder utama standalone dan static ada
26-
echo "Verifikasi konten build Next.js (sebelum transfer):"
27-
ls -ld .next/standalone || true # Hanya cek direktori induk
28-
ls -R .next/static || true
21+
- name: 📦 Install Dependencies
22+
run: npm ci
2923

30-
- name: 4. Buat Deployment Tarball
31-
run: |
32-
set -e # Hentikan eksekusi jika perintah tar gagal
33-
echo "Membuat deployment tarball: deployment.tar.gz"
34-
# Membuat tarball berisi semua yang dibutuhkan untuk standalone deployment
35-
# --exclude='node_modules' (node_modules di root tidak perlu ditransfer)
36-
tar -zcf deployment.tar.gz \
37-
--exclude='node_modules' \
38-
.next/standalone \
39-
.next/static \
40-
package.json \
41-
app.js \
42-
public
43-
ls -l deployment.tar.gz # Verifikasi file dibuat dan ukurannya > 0
24+
- name: 🏗️ Build Next.js App
25+
run: npm run build
4426

45-
- name: 4b. Verifikasi Tarball Sebelum Transfer
27+
- name: 📁 Prepare Deployment Package
4628
run: |
47-
echo "Isi direktori kerja sebelum SCP:"
48-
ls -l deployment.tar.gz
29+
mkdir deploy
30+
cp -r .next public package.json app.js next.config.js deploy/
31+
# Tambahkan file lain yang dibutuhkan aplikasi di server
32+
ls -la deploy
4933
50-
- name: 5. Transfer File (Single Tarball) ke cPanel (via SCP)
34+
- name: 📤 Deploy to cPanel via SSH
5135
uses: appleboy/scp-action@v0.1.7
5236
with:
5337
host: ${{ secrets.SSH_HOST }}
5438
username: ${{ secrets.SSH_USER }}
5539
key: ${{ secrets.SSH_PRIVATE_KEY }}
5640
passphrase: ${{ secrets.SSH_PASSPHRASE }}
57-
port: 22
58-
rm: false
59-
debug: true
60-
# HANYA transfer satu file: tarball yang sudah kita buat
61-
source: deployment.tar.gz
41+
source: "deploy/*"
6242
target: ${{ secrets.DEPLOY_DIR }}
43+
strip_components: 1
44+
overwrite: true
6345

64-
- name: 6. Eksekusi Perintah di cPanel (via SSH)
65-
uses: appleboy/ssh-action@v1.0.3
46+
- name: ⚙️ Restart Node.js App on cPanel
47+
uses: appleboy/ssh-action@v1.2.0
6648
with:
6749
host: ${{ secrets.SSH_HOST }}
6850
username: ${{ secrets.SSH_USER }}
6951
key: ${{ secrets.SSH_PRIVATE_KEY }}
7052
passphrase: ${{ secrets.SSH_PASSPHRASE }}
71-
port: 22
72-
debug: true
7353
script: |
74-
# 1. Setup Environment dan Pindah ke Direktori Aplikasi
75-
source ${{ secrets.NODE_ENV_PATH }}
76-
cd ${{ secrets.DEPLOY_DIR }}
77-
78-
# 2. EKSTRAKSI TARBALL
79-
echo "Mengekstrak deployment.tar.gz..."
80-
tar -xzf deployment.tar.gz
81-
rm deployment.tar.gz # Hapus file tarball setelah diekstrak
82-
83-
# 3. EKSTRAKSI DAN PEMINDAHAN KONTEN (Langkah Kritis)
84-
echo "Memindahkan konten standalone ke root aplikasi..."
85-
86-
# Pindahkan KONTEN dari .next/standalone (server files, node_modules internal) ke DEPLOY_DIR
87-
mv .next/standalone/* .
88-
89-
# Hapus folder sementara .next/standalone
90-
rm -rf .next/standalone
91-
92-
# 4. Instal Dependencies Produksi ke Path Khusus cPanel
93-
echo "Memastikan node_modules diarahkan ke path yang benar: ${{ secrets.NPM_PREFIX_DIR }}/lib/node_modules"
94-
npm config set prefix ${{ secrets.NPM_PREFIX_DIR }}
95-
npm install --production
96-
97-
# 5. Restart Aplikasi
98-
echo "Restart Aplikasi (Trigger cPanel App Manager)..."
99-
touch app.js
100-
101-
echo "Deployment Selesai!"
54+
echo "🔧 Activating Node.js environment..."
55+
${{ secrets.NODE_ENV_PATH }}
56+
echo "📦 Installing production dependencies..."
57+
npm ci --omit=dev
58+
echo "♻️ Restarting Node.js app..."
59+
touch tmp/restart.txt || true
60+
echo "✅ Deployment complete!"

app.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
// app.js
22

3-
const { createServer } = require("http");
4-
const { parse } = require("url");
3+
import { createServer } from "http";
4+
import { parse } from "url";
5+
import next from "next";
56

6-
const nextServer = require("./.next/standalone/server");
7+
const port = parseInt(process.env.PORT || "3000", 10);
8+
const dev = process.env.NODE_ENV !== "production";
79

8-
const port = process.env.PORT || 3000;
10+
const app = next({ dev });
11+
const handle = app.getRequestHandler();
912

10-
const handler = nextServer.default;
13+
app.prepare().then(() => {
14+
createServer((req, res) => {
15+
const parsedUrl = parse(req.url, true);
16+
handle(req, res, parsedUrl);
17+
}).listen(port);
1118

12-
createServer((req, res) => {
13-
const parsedUrl = parse(req.url, true);
14-
handler(req, res, parsedUrl);
15-
}).listen(port, (err) => {
16-
if (err) throw err;
17-
console.log(`> Next.js Standalone App is Ready on http://localhost:${port}`);
18-
console.log(`> Running in Production Mode.`);
19+
console.log(
20+
`✅ Server running on http://localhost:${port} in ${
21+
dev ? "development" : "production"
22+
} mode`
23+
);
1924
});

next.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { NextConfig } from "next";
22

33
const nextConfig: NextConfig = {
4-
output: "standalone",
4+
/* config options here */
55
reactCompiler: true,
66
};
77

package-lock.json

Lines changed: 46 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
"private": true,
66
"scripts": {
77
"dev": "next dev",
8-
"build": "NODE_ENV=production next build",
9-
"start": "NODE_ENV=production node .next/standalone/app.js",
10-
"lint": "biome check",
11-
"format": "biome format --write"
8+
"build": "next build",
9+
"start": "next start",
10+
"lint": "eslint"
1211
},
1312
"dependencies": {
1413
"react": "19.2.0",

0 commit comments

Comments
 (0)