preface
During front-end project deployment, after nginx configuration is complete, you only need to upload the packaged files to the specified directory on the server.
Generally, the following methods are used:
- Command line tools such as xshell are uploaded
- Upload visual tools such as FTP or SFTP
- For simple front-end projects, xshell and FTP are cumbersome in frequent deployment. However, automatic deployment services such as Jenkins require software installation in advance and familiarity with the configuration process. Therefore, we hope to use the local Node service to realize the upload of the front-end packaged files, which does not require the server to install additional procedures, but also helps us to achieve rapid upload and deployment, and helps us to understand node deeply.
purpose
Reduce the manual process of compiling and packaging frequently during the development and debugging of Web projects, and then deploy them to the server using FTP tools to improve work efficiency.
1. Import dependent modules
- npm install inquirer ssh2-sftp-client
- touch ssh.js helper.js
- Add a script to package.json, make sure NPM I reinstalls the dependencies once you’re done, and make sure the new script works!
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"."deploy": "bash deploy.sh"
},
Copy the code
- touch deploy.sh
npm run build
echo "Packed and ready"
node ./ssh.js
cd -
Copy the code
2. Connect to the remote server and upload the file
const Client = require('ssh2-sftp-client')
const sftp = new Client()
const helper = require ('./helper')
const config = [
{
name: 'a'// project/server name SSH: {host:'192.168.0.105',
port: 22,
username: 'root',
password: 'root',
},
romotePath: '/var/www/dist'// Remote addresslocalPath:'./dist'}, {name:'b',
ssh: {
host: '192.168.0.110',
port: 22,
username: 'root',
password: 'root',
},
romotePath: '/var/www/dist'.localPath:'./dist',
}
]
async function main() {const SELECT_CONFIG = (await helper(config)).value // Configuration information of the selected deployment project console.log()'You chose to deploy' + SELECT_CONFIG.name)
sftp
.connect(SELECT_CONFIG.ssh)
.then(() => {
console.log('- Connection successful, upload in.. ')
return sftp.uploadDir(SELECT_CONFIG.localPath, SELECT_CONFIG.romotePath)
})
.then(data => {
console.log(data,'Upload complete, clear cache in time' )
})
.catch(err => {
console.log(err,'Wrong! Look what's going on! '()}). The finally () = > {SFTP. End disconnected () / /})} the main ()Copy the code
3. Build and publish
- Execute NPM run deploy to package and proactively upload to your target server
- Git Bash Use arrow keys cannot be selected for Windows
winpty npm.cmd run deploy
Copy the code
Github address of the project. Welcome star, message and issue. I hope this article will be helpful to you and I wish you a happy work and life!