u
This commit is contained in:
parent
816f2e3e82
commit
4abcbf62a2
32
README.md
32
README.md
@ -1,17 +1,17 @@
|
|||||||
|
|
||||||
## 参数
|
## 参数
|
||||||
* -t, --type: web(默认) 或 git
|
|
||||||
* 如果是 git
|
- -t, --type: web(默认) 或 git
|
||||||
* -r, --repo: 远程仓库,例如 'https://github.com/ORG/ORG.github.io'
|
- 如果是 git
|
||||||
* -b, --branch: 分支,默认为 main
|
- -r, --repo: 远程仓库,例如 'https://github.com/ORG/ORG.github.io'
|
||||||
* -n, --gitname
|
- -b, --branch: 分支,默认为 main
|
||||||
* -e, --gitemail
|
- -n, --gitname
|
||||||
* 如果是 web
|
- -e, --gitemail
|
||||||
* -H, --host: 远程主机
|
- 如果是 web
|
||||||
* -P, --port: 远程端口,默认为 22
|
- -H, --host: 远程主机
|
||||||
* -D, --dir: 远程路径,例如 /home/USER/FOLDER
|
- -P, --port: 远程端口,默认为 22
|
||||||
* -d, --dist: 远程文件夹,例如 www。默认为 dist。
|
- -D, --dir: 远程路径,例如 /home/USER/FOLDER
|
||||||
* -l, --local: 本地路径,例如 ../../project/public。默认为 ./dist
|
- -d, --dist: 远程文件夹,例如 www。默认为 dist。
|
||||||
* -u, --user: 用户名
|
- -l, --local: 本地路径,例如 ../../project/public。默认为 ./dist
|
||||||
* -p, --password: 密码
|
- -u, --user: 用户名
|
||||||
* -k, --key: 密钥文件
|
- -p, --password: 密码
|
||||||
|
- -k, --privateKey: 密钥文件
|
||||||
|
200
deploy.js
200
deploy.js
@ -2,25 +2,76 @@ const fs = require('fs')
|
|||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
/** ******************* 读取命令行以及配置文件里的参数 ******************** **/
|
/** ******************* 读取命令行以及配置文件里的参数 ******************** **/
|
||||||
const commander = require('commander')
|
|
||||||
const deepmerge = require('deepmerge')
|
|
||||||
|
|
||||||
// 默认参数
|
|
||||||
const wo = (global.wo = {
|
const wo = (global.wo = {
|
||||||
envar: {
|
envar: require('basend-envar').merge_envar({
|
||||||
|
rawEnvar: {
|
||||||
|
Commander_Option_List: [
|
||||||
|
// 命令行里可以接受的参数。将传给 commander。每个参数的定义格式是 [参数名,参数键,描述],后两者用于传给 commander,取值后覆盖掉同名变量。
|
||||||
|
[
|
||||||
|
'fromPath',
|
||||||
|
'-f, --fromPath <fromPath>',
|
||||||
|
'local distribution path to copy from.'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'gotoTarget',
|
||||||
|
'-g, --gotoTarget <gotoTarget>',
|
||||||
|
'connection section name.'
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
'targetType',
|
||||||
|
'-t, --targetType <targetType>',
|
||||||
|
'target type, git or ssh.'
|
||||||
|
],
|
||||||
|
|
||||||
|
[
|
||||||
|
'host',
|
||||||
|
'-H, --host <host>',
|
||||||
|
'Host IP or domain name of the target server.'
|
||||||
|
],
|
||||||
|
['port', '-P, --port <port>', 'Ssh port number of the target server.'],
|
||||||
|
[
|
||||||
|
'targetPath',
|
||||||
|
'-d, --targetPath <targetPath>',
|
||||||
|
'Destination path to deploy on the target.'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'targetDir',
|
||||||
|
'-D, --targetDir <targetDir>',
|
||||||
|
'Destination folder to deploy on the target.'
|
||||||
|
],
|
||||||
|
|
||||||
|
['repo', '-r, --repo <repo>', 'git repo address.'],
|
||||||
|
['branch', '-b, --branch <branch>', 'git repo branch.'],
|
||||||
|
['gitname', '-n, --gitname <gitname>', 'git user name.'],
|
||||||
|
['gitemail', '-m, --gitemail <gitemail>', 'git user email.'],
|
||||||
|
|
||||||
|
['user', '-u, --user <user>', 'User id to login the target server.'],
|
||||||
|
[
|
||||||
|
'privateKey',
|
||||||
|
'-k, --privateKey <privateKey>',
|
||||||
|
'User private key file to login the target server.'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'password',
|
||||||
|
'-p, --password <password>',
|
||||||
|
'User password to login the target server. You may have to enclose it in "".'
|
||||||
|
]
|
||||||
|
],
|
||||||
|
// 最基础的必须的默认配置,如果用户什么也没有提供
|
||||||
deploy: {
|
deploy: {
|
||||||
fromPath: './webroot',
|
fromPath: './webroot',
|
||||||
gotoTarget: 'github',
|
gotoTarget: 'github',
|
||||||
|
|
||||||
vultr: {
|
server: {
|
||||||
targetType: 'ssh',
|
targetType: 'ssh',
|
||||||
host: undefined,
|
host: undefined,
|
||||||
port: 22,
|
port: 22,
|
||||||
targetPath: undefined,
|
targetPath: undefined, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
|
||||||
targetDir: 'webroot',
|
targetDir: 'webroot', // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。
|
||||||
user: undefined,
|
user: undefined,
|
||||||
password: undefined,
|
password: undefined,
|
||||||
key: `${process.env.HOME}/.ssh/id_rsa`
|
privateKey: `${process.env.HOME}/.ssh/id_rsa`
|
||||||
},
|
},
|
||||||
github: {
|
github: {
|
||||||
targetType: 'git',
|
targetType: 'git',
|
||||||
@ -30,96 +81,29 @@ const wo = (global.wo = {
|
|||||||
gitemail: undefined,
|
gitemail: undefined,
|
||||||
user: undefined,
|
user: undefined,
|
||||||
password: undefined,
|
password: undefined,
|
||||||
key: `${process.env.HOME}/.ssh/id_rsa`
|
privateKey: `${process.env.HOME}/.ssh/id_rsa`
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
envarFiles: ['./envar-deploy.js', './envar-deploy-secret.js']
|
||||||
})
|
})
|
||||||
|
})
|
||||||
// 读取配置文件
|
const envarDeploy = wo.envar.deploy
|
||||||
try {
|
delete wo.envar.deploy
|
||||||
let configFile
|
// 用 commander 采集到的配置 替换 文件中采集到的配置
|
||||||
if (
|
envarDeploy.fromPath = wo.envar.fromPath || envarDeploy.fromPath
|
||||||
fs.existsSync(
|
envarDeploy.gotoTarget = wo.envar.gotoTarget || envarDeploy.gotoTarget
|
||||||
(configFile = path.join(process.cwd(), 'envar-base-deploy.js'))
|
// 使用用户指定的连接
|
||||||
)
|
const connection = envarDeploy[envarDeploy.gotoTarget]
|
||||||
) {
|
Object.assign(connection, wo.envar) // 用 commander 采集到的配置 替换 文件中采集到的配置
|
||||||
wo.envar = deepmerge(wo.envar, require(configFile))
|
connection.tryKeyboard = true
|
||||||
console.info(`- ${configFile} loaded`)
|
connection.onKeyboardInteractive = (
|
||||||
}
|
name,
|
||||||
if (
|
instructions,
|
||||||
fs.existsSync(
|
lang,
|
||||||
(configFile = path.join(process.cwd(), 'envar-base-secret.js'))
|
prompts,
|
||||||
)
|
finish
|
||||||
) {
|
) => {
|
||||||
wo.envar = deepmerge(wo.envar, require(configFile))
|
|
||||||
console.info(`- ${configFile} loaded`)
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('- Loading config files failed: ' + err.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 读取命令行参数
|
|
||||||
commander
|
|
||||||
.version('1.0', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
|
||||||
.option('-f, --fromPath <fromPath>', `local distribution path to copy from.`)
|
|
||||||
.option('-g, --gotoTarget <gotoTarget>', `connection section in config.`)
|
|
||||||
|
|
||||||
.option('-t, --targetType <targetType>', `target type, git or ssh.`)
|
|
||||||
|
|
||||||
.option('-H, --host <host>', `Host IP or domain name of the target server.`)
|
|
||||||
.option('-P, --port <port>', `Ssh port number of the target server.`)
|
|
||||||
.option(
|
|
||||||
'-d, --targetPath <targetPath>',
|
|
||||||
`Destination path to deploy on the target.`
|
|
||||||
)
|
|
||||||
.option(
|
|
||||||
'-D, --targetDir <targetDir>',
|
|
||||||
`Destination folder to deploy on the target.`
|
|
||||||
)
|
|
||||||
|
|
||||||
.option('-r, --repo <repo>', `git repo address.`)
|
|
||||||
.option('-b, --branch <branch>', `git repo branch.`)
|
|
||||||
.option('-n, --gitname <gitname>', `git user name.`)
|
|
||||||
.option('-m, --gitemail <gitemail>', `git user email.`)
|
|
||||||
|
|
||||||
.option('-u, --user <user>', `User id to login the target server.`)
|
|
||||||
.option(
|
|
||||||
'-k, --key <key>',
|
|
||||||
`User private key file to login the target server.`
|
|
||||||
)
|
|
||||||
.option(
|
|
||||||
'-p, --password <password>',
|
|
||||||
`User password to login the target server. You may have to enclose it in "".`
|
|
||||||
)
|
|
||||||
.parse(process.argv)
|
|
||||||
|
|
||||||
wo.envar.deploy.fromPath = commander.fromPath || wo.envar.deploy.fromPath
|
|
||||||
wo.envar.deploy.connection =
|
|
||||||
wo.envar.deploy[commander.gotoTarget || wo.envar.deploy.gotoTarget] // 使用用户指定的连接
|
|
||||||
|
|
||||||
// 可以用命令行参数覆盖掉配置文件
|
|
||||||
const connection = {
|
|
||||||
targetType: commander.targetType || wo.envar.deploy.connection.targetType,
|
|
||||||
|
|
||||||
// for ssh
|
|
||||||
host: commander.host || wo.envar.deploy.connection.host,
|
|
||||||
port: commander.port || wo.envar.deploy.connection.port,
|
|
||||||
targetPath: commander.targetPath || wo.envar.deploy.connection.targetPath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
|
|
||||||
targetDir: commander.targetDir || wo.envar.deploy.connection.targetDir, // 新系统将发布在这个文件夹里。建议为dist,和npm run build产生的目录一致,这样既可以远程自动部署,也可以直接登录服务器手动部署。
|
|
||||||
// for git
|
|
||||||
repo: commander.repo || wo.envar.deploy.connection.repo,
|
|
||||||
branch: commander.branch || wo.envar.deploy.connection.branch,
|
|
||||||
gitname: commander.gitname || wo.envar.deploy.connection.gitname,
|
|
||||||
gitemail: commander.gitemail || wo.envar.deploy.connection.gitemail,
|
|
||||||
// common
|
|
||||||
username: commander.user || wo.envar.deploy.connection.user,
|
|
||||||
privateKey: fs.existsSync(commander.key || wo.envar.deploy.connection.key)
|
|
||||||
? commander.key || wo.envar.deploy.connection.key
|
|
||||||
: undefined,
|
|
||||||
password: commander.password || wo.envar.deploy.connection.password,
|
|
||||||
tryKeyboard: true,
|
|
||||||
onKeyboardInteractive: (name, instructions, lang, prompts, finish) => {
|
|
||||||
// 不起作用
|
// 不起作用
|
||||||
if (
|
if (
|
||||||
prompts.length > 0 &&
|
prompts.length > 0 &&
|
||||||
@ -127,12 +111,10 @@ const connection = {
|
|||||||
) {
|
) {
|
||||||
finish([password])
|
finish([password])
|
||||||
}
|
}
|
||||||
},
|
|
||||||
url: wo.envar.deploy.connection.url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
` deploy from ${wo.envar.deploy.fromPath} to ${JSON.stringify(connection)}`
|
`*** Deploy from ${envarDeploy.fromPath} to ${JSON.stringify(connection)} ***`
|
||||||
)
|
)
|
||||||
|
|
||||||
if (connection.targetType === 'ssh') {
|
if (connection.targetType === 'ssh') {
|
||||||
@ -184,7 +166,7 @@ function deployToSsh (connection) {
|
|||||||
await ssh.execCommand(`mkdir ${connection.targetDir}`, {
|
await ssh.execCommand(`mkdir ${connection.targetDir}`, {
|
||||||
cwd: connection.targetPath
|
cwd: connection.targetPath
|
||||||
})
|
})
|
||||||
const toCreate = necessaryPath(path.join('./', wo.envar.deploy.fromPath))
|
const toCreate = necessaryPath(path.join('./', envarDeploy.fromPath))
|
||||||
for (const name of toCreate) {
|
for (const name of toCreate) {
|
||||||
console.log(`[ mkdir ${connection.targetDir}/${name.join('/')} ... ]`)
|
console.log(`[ mkdir ${connection.targetDir}/${name.join('/')} ... ]`)
|
||||||
await ssh.execCommand(
|
await ssh.execCommand(
|
||||||
@ -198,7 +180,7 @@ function deployToSsh (connection) {
|
|||||||
`[ Upload to ${connection.targetPath}/${connection.targetDir} ... ]`
|
`[ Upload to ${connection.targetPath}/${connection.targetDir} ... ]`
|
||||||
)
|
)
|
||||||
await ssh.putDirectory(
|
await ssh.putDirectory(
|
||||||
path.join('./', wo.envar.deploy.fromPath),
|
path.join('./', envarDeploy.fromPath),
|
||||||
`${connection.targetPath}/${connection.targetDir}`,
|
`${connection.targetPath}/${connection.targetDir}`,
|
||||||
{
|
{
|
||||||
concurrency: 10,
|
concurrency: 10,
|
||||||
@ -219,12 +201,12 @@ function deployToSsh (connection) {
|
|||||||
ssh.dispose()
|
ssh.dispose()
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(
|
console.error(
|
||||||
`🤷♀️🤷♀️🤷♀️ Failed deploy ${wo.envar.deploy.fromPath} to ${connection.targetPath}/${connection.targetDir} 🤷♀️🤷♀️🤷♀️`
|
`🤷♀️🤷♀️🤷♀️ Failed deploy ${envarDeploy.fromPath} to ${connection.targetPath}/${connection.targetDir} 🤷♀️🤷♀️🤷♀️`
|
||||||
)
|
)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
} else {
|
} else {
|
||||||
console.info(
|
console.info(
|
||||||
`😊😊😊 Successfully deployed [${wo.envar.deploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 😊😊😊`
|
`😊😊😊 Successfully deployed [${envarDeploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 😊😊😊`
|
||||||
)
|
)
|
||||||
if (connection.url) {
|
if (connection.url) {
|
||||||
console.info(`😊😊😊 ${connection.url} 😊😊😊`)
|
console.info(`😊😊😊 ${connection.url} 😊😊😊`)
|
||||||
@ -236,7 +218,7 @@ function deployToSsh (connection) {
|
|||||||
console.error(err)
|
console.error(err)
|
||||||
ssh.dispose()
|
ssh.dispose()
|
||||||
console.error(
|
console.error(
|
||||||
`🤷♀️🤷♀️🤷♀️ Failed deploy [${wo.envar.deploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 🤷♀️🤷♀️🤷♀️`
|
`🤷♀️🤷♀️🤷♀️ Failed deploy [${envarDeploy.fromPath}] to [${connection.targetPath}/${connection.targetDir}] 🤷♀️🤷♀️🤷♀️`
|
||||||
)
|
)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
@ -307,7 +289,7 @@ function deployToGit (connection) {
|
|||||||
function exec () {
|
function exec () {
|
||||||
const targetDir = ''
|
const targetDir = ''
|
||||||
const deployDir = pathFn.join(targetDir, '.deploy_git')
|
const deployDir = pathFn.join(targetDir, '.deploy_git')
|
||||||
const fromDir = wo.envar.deploy.fromPath
|
const fromDir = envarDeploy.fromPath
|
||||||
let extendDirs = connection.extend_dirs
|
let extendDirs = connection.extend_dirs
|
||||||
const ignoreHidden = connection.ignore_hidden
|
const ignoreHidden = connection.ignore_hidden
|
||||||
const ignorePattern = connection.ignore_pattern
|
const ignorePattern = connection.ignore_pattern
|
||||||
@ -340,8 +322,8 @@ function deployToGit (connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setup () {
|
function setup () {
|
||||||
const userName = wo.envar.deploy.gitname || ''
|
const userName = connection.gitname || ''
|
||||||
const userEmail = wo.envar.deploy.gitemail || ''
|
const userEmail = connection.gitemail || ''
|
||||||
|
|
||||||
// Create a placeholder for the first commit
|
// Create a placeholder for the first commit
|
||||||
return fs
|
return fs
|
||||||
@ -375,7 +357,7 @@ function deployToGit (connection) {
|
|||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.info(
|
console.info(
|
||||||
`😊😊😊 Successfully deployed [${wo.envar.deploy.fromPath}] to [${connection.repo}#${connection.branch}] 😊😊😊`
|
`😊😊😊 Successfully deployed [${envarDeploy.fromPath}] to [${connection.repo}#${connection.branch}] 😊😊😊`
|
||||||
)
|
)
|
||||||
if (connection.url) {
|
if (connection.url) {
|
||||||
console.info(`😊😊😊 ${connection.url} 😊😊😊`)
|
console.info(`😊😊😊 ${connection.url} 😊😊😊`)
|
||||||
@ -383,7 +365,7 @@ function deployToGit (connection) {
|
|||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(
|
console.error(
|
||||||
`🤷♀️🤷♀️🤷♀️ Failed deploy [${wo.envar.deploy.fromPath}] to [${connection.repo}#${connection.branch}] 🤷♀️🤷♀️🤷♀️`
|
`🤷♀️🤷♀️🤷♀️ Failed deploy [${envarDeploy.fromPath}] to [${connection.repo}#${connection.branch}] 🤷♀️🤷♀️🤷♀️`
|
||||||
)
|
)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
@ -13,10 +13,9 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"basend-envar": "git+https://git.faronear.org/npm/basend-envar",
|
||||||
"bluebird": "^3.5.5",
|
"bluebird": "^3.5.5",
|
||||||
"chalk": "^2.4.2",
|
"chalk": "^2.4.2",
|
||||||
"commander": "^3.0.1",
|
|
||||||
"deepmerge": "^4.0.0",
|
|
||||||
"hexo-fs": "^3.1.0",
|
"hexo-fs": "^3.1.0",
|
||||||
"hexo-util": "^2.5.0",
|
"hexo-util": "^2.5.0",
|
||||||
"moment": "^2.24.0",
|
"moment": "^2.24.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user