ssh
, can be used to deploy stuffs or CI/CD.
A toolkit make it easy(with plain config) to manipulate(upload/download/exec command) server via ssh
, can be used to deploy stuffs or CI/CD.
A toolkit make it easy(with plain config) to manipulate(upload/download/exec command) server via
All actions are run in sequence, and you can set allow failure for specific action(wont stop the sequence even it failed).
Exmaple
const config = // ssh connection config ssh: host: 'my.server.com' username: 'fancy' // // use password if you prefer password // password: '123456' // or private ssh key file path(or key text content) // use ~ as user homedir privateKey: '~/.ssh/my-private-key' // set passphrase if private key is encrypted passphrase: '3344' // whether to show command execution logs log: true // commands sequence, will execute by its order cmds: // exec command type: 'cmd' // command arguments list args: 'mkdir' '-p' 'saiya/test' // command work directory on remote server cwd: '/home/user' type: 'cmd' args: 'pm2' 'stop' 'my-fancy-app' // if pm2 stop failed, still continue to run the following cmds allowFailure: true type: 'cmd' args: 'ls' 'saiya' '-l' cwd: '/home/user' // upload files type: 'upload' // files' glob pattern, could also be a file/dir path src: path // if upload multi files with glob pattern, srcPrefix is needed to determine to saved path on server // no need if `src` is certain a file/dir path srcPrefix: path // server directory path to save the files dest: '/home/kk/saiya' // download file, only support download a single file at a time type: 'download' // source file path on server src: '/home/kk/start.sh' // saved path in local dest: path // run commands // run local shell commands // find closest package.json file full path, return '' if not foundconsole// found closest .git dir full path from current work dir, return '' if not foundconsole // add git tag & puth to remote, use `v${package.version}` in package.json as tag name by default // sepecify the tag name
Install
yarn add deploy-toolkit
or
npm i deploy-toolkit -D
Usage
deploy
Deploy stuffs to remote server with simple json config, you can upload/download/execute-command on remote server.
// import the main function like this// // you can import the following types if you are using typescript// import { IDeployConfig } from 'deploy-toolkit' /** deploy confgi */ /** SSH Connection config *//** commands sequence */ /** download config */ /** custom command */ /** * custom script */
Example
const config: IDeployConfig = ssh: host: '10.213.85.1' username: 'deploy' password: 'passw0rp!' log: true cmds: type: 'cmd' args: 'mkdir' '-p' 'saiya/test' cwd: '~/Documents' type: 'download' src: '/home/deploy/start.sh' dest: path type: 'upload' src: '/home/user1/Documents/Hobby/project1/dist' dest: '/home/deploy/Documents/project1' type: 'script' script: ` cd ~ rm -rf Document/project1 UPLOAD /home/user1/Documents/Hobby/project1/dist > /home/deploy/Documents/project1 cd Document/project1 npm start DOWNLOAD /home/deploy/Documents/project1/logs/latest.log > /home/user1/Documents/Hobby/logs/latest.log echo "done" `
runShellCmd
run shell command on local machine, a promise wrapper of node child_process.spawn
, by default run the command in cwd process.cwd()
// return promise with execution result // check nodejs doc http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options for detail explains
findFileRecursive
find a file/dir recursively from specified dir to the root until found, return ''
if not found.
/** * find a file(dir) recursive( aka try to find package.json, node_modules, etc.) * @param fileName file name(s)(or dir name(s) if isDir is true), if an array, return the first matched one * @param dir the initial dir path to find, use `process.cwd()` by default * @param isDir whether to find a dir, default false */ // e.g. find babel config file path
addGitTag
add git tag and push it to remote, you can use it on postbuild
or postpublish
.
use v${package.version}
in package.json as tag name by default, return the tagName
after tag push