Skip to content

Commit f3762a9

Browse files
ftp deploy added
1 parent bfc80b3 commit f3762a9

7 files changed

Lines changed: 637 additions & 23 deletions

File tree

.env.dev

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
BUILD_HASH=XXXXXXXXXXXXXXXXX
22
SERVER_IP=0.0.0.0
3-
SERVER_PORT=5466
3+
SERVER_PORT=5466
4+
FTP_USERNAME=
5+
FTP_PASSWORD=
6+
FTP_HOST=
7+
FTP_PORT=

hook.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
const dotenv = require('dotenv')
2-
dotenv.config()
3-
const fastify = require('fastify')({ logger: true })
1+
42
const { exec } = require("child_process")
53
const gatsbyProcesses = require("./hook_commands/gatsby")
4+
const gatsbyFtpProcesses = require("./hook_commands/gatsby_ftp")
5+
const FtpDeploy = require("ftp-deploy");
6+
const ftpDeploy = new FtpDeploy();
7+
const dotenv = require('dotenv')
8+
dotenv.config()
69

710
let isPublishing = false
811
let results = ''
12+
13+
const ftpConfig = {
14+
user: process.env.FTP_USERNAME,
15+
password: process.env.FTP_PASSWORD,
16+
host: process.env.FTP_HOST,
17+
port: parseInt(process.env.FTP_PORT),
18+
include: ['*', '**/*'],
19+
localRoot: __dirname + "/hook_commands",
20+
remoteRoot: "/backup/",
21+
deleteRemote: false,
22+
forcePasv: true,
23+
sftp: false
24+
}
25+
926
let processes = []
1027

1128
const writeResult = (value) => {
@@ -25,6 +42,17 @@ function ejectRecursive(index) {
2542

2643
writeResult(`=====> command is started [ ${processes[index].command} ] : ${new Date()} : <=====`)
2744

45+
//ftp commond check
46+
if (processes[index].ftp) {
47+
ftpDeploy
48+
.deploy(ftpConfig)
49+
.then(res => {
50+
writeResult(`=====> FTP Upload Finished <=====`)
51+
ejectRecursive(index + 1)
52+
})
53+
return
54+
}
55+
2856
exec(processes[index].command, processes[index].option, (error, stdout, stderr) => {
2957
if (error) {
3058
writeResult(`=====> error command: [ ${processes[index].command} ] : ${new Date()} : <=====`)
@@ -56,7 +84,7 @@ function ejectRecursive(index) {
5684
})
5785
}
5886

59-
fastify.post('/build/:hash', async (request, reply) => {
87+
module.exports.startBuild = (request, reply) => {
6088
const receivedHash = request.params.hash
6189

6290
if (isPublishing)
@@ -67,26 +95,12 @@ fastify.post('/build/:hash', async (request, reply) => {
6795
}
6896

6997
results = ''
70-
processes = gatsbyProcesses;
98+
processes = gatsbyFtpProcesses;
7199
writeResult(`Process is starting... : ${new Date()} :`)
72100
ejectRecursive(0)
73101
isPublishing = true
74102

75103
return response(true, "build is started")
76-
})
77-
78-
fastify.get('/build_result', async () => {
79-
return results
80-
})
81-
82-
const start = async () => {
83-
try {
84-
await fastify.listen(process.env.SERVER_PORT, process.env.SERVER_IP)
85-
fastify.log.info(`server listening on ${fastify.server.address().port}`)
86-
} catch (err) {
87-
fastify.log.error(err)
88-
process.exit(1)
89-
}
90104
}
91105

92-
start()
106+
module.exports.results = () => results

hook_commands/gatsby.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ const copyProcess = {
1515
option: projectOptions.public
1616
}
1717

18-
module.exports = [{
18+
module.exports = [
19+
{
20+
command: "git reset --hard",
21+
option: projectOptions.main
22+
},
23+
{
24+
command: "git clean -df",
25+
option: projectOptions.main
26+
},
27+
{
1928
command: "git pull",
2029
option: projectOptions.main
2130
},

hook_commands/gatsby_ftp.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const projectOptions = {
2+
main: {
3+
cwd: '/root/gatsby/'
4+
}
5+
}
6+
7+
module.exports = [
8+
{
9+
command: "git reset --hard",
10+
option: projectOptions.main
11+
},
12+
{
13+
command: "git clean -df",
14+
option: projectOptions.main
15+
},
16+
{
17+
command: "git pull",
18+
option: projectOptions.main
19+
},
20+
{
21+
command: "npm install",
22+
option: projectOptions.main
23+
},
24+
{
25+
command: "npm run build",
26+
option: projectOptions.main
27+
},
28+
{
29+
command: "FTP Upload", // optional
30+
ftp: true // required
31+
}
32+
]

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const hook = require('./hook.js');
2+
const fastify = require('fastify')({ logger: true })
3+
const dotenv = require('dotenv')
4+
dotenv.config()
5+
6+
fastify.post('/build/:hash', async (request, reply) => {
7+
return hook.startBuild(request, reply)
8+
})
9+
10+
fastify.get('/build_result', async () => {
11+
return hook.results()
12+
})
13+
14+
const start = async () => {
15+
try {
16+
await fastify.listen(process.env.SERVER_PORT, process.env.SERVER_IP)
17+
fastify.log.info(`server listening on ${fastify.server.address().port}`)
18+
} catch (err) {
19+
fastify.log.error(err)
20+
process.exit(1)
21+
}
22+
}
23+
24+
start()

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"child_process": "^1.0.2",
1313
"dotenv": "^8.2.0",
14-
"fastify": "^3.3.0"
14+
"fastify": "^3.3.0",
15+
"ftp-deploy": "^2.4.1"
1516
}
1617
}

0 commit comments

Comments
 (0)