There are a lot of great one-click deployment tools like Jenkins on the market, but the company’s front-end deployment is still in the slash-and-burn era (SFTP visual tool for uploading and deleting files).
Today I just have time to learn something about shell scripts and make a script for one-click deployment project.
Let’s cut to the chase
1. Upload files
UploadFile. Sh file
#! /bin/bash
ip="Server IP"
password="Server password"
local_dir="Absolute path to project directory" #eg: /Users/geekchen/Desktop/juejin/
remote_dir="The absolute path to upload to the server" #/web/juejin/
opt_type="dir" #dir or file Type of upload: entire directory or file
chmod +x command.sh # add command. Sh to command
./command.sh ${local_dir} root ${ip} ${remote_dir} ${password} ${opt_type} Note: This is the default root identity, if other identities, please modify the second parameter above
Copy the code
The command. Sh file
#! /usr/bin/expect
if {$argc < 2} {
send_user "usage: $argv0 src_file username host_ip dest_file password\n"
exit
}
set src_file [lindex $argv 0] The receiving parameters are the same as below
set username [lindex $argv 1]
set host_ip [lindex $argv 2]
set dest_file [lindex $argv 3]
set password [lindex $argv 4]
set opt_type [lindex $argv 5]
set timeout 60
if { "$opt_type"= ="dir" } {
spawn scp -r $src_file $username@$host_ip:$dest_file # use SCP protocol for upload
expect {
"Connection refused" exit
"continue connecting" {send "yes\r"; exp_continue}"password:" {send "$password\r"; exp_continue} eof } } elseif {"$opt_type"= ="file" } {
spawn scp $src_file $username@$host_ip:$dest_file # use SCP protocol for upload
expect {
"Connection refused" exit
"continue connecting" {send "yes\r"; exp_continue}"password:" {send "$password\r"; exp_continue} eof } }exit
Copy the code
How to eat: Ready to eat out of the box
Place the above two scripts in the same directory, open the terminal in the current directory, and enter
sh upload_file.sh
Copy the code
Wait until the upload is complete
2. Delete files
Delete_file. Sh file
#! /bin/bash
ip="Server IP"
password="Server password"
file_delete="Folder path on remote server to delete" #/web/juejin/
chmod +x command.sh # add command. Sh to command
./command.sh root ${ip} ${password} ${file_delete}Note: This is the default root identity, if other identities, please modify the first parameter above
if[$?-ne0];then
echo "./command.sh root ${ip} ${password} error!"
exit 1
fi
exit 0
Copy the code
The command. Sh file
#! /usr/bin/expect
if {$argc < 2} {
send_user "usage: $argv0 username host_ip password\n"
exit
}
set username [lindex $argv 0]
set host_ip [lindex $argv 1]
set password [lindex $argv 2]
set file_delete [lindex $argv 3]
set timeout 15
spawn ssh $username@$host_ip
expect {
"Connection refused" exit
"continue connecting" {send "yes\r"; exp_continue}"password:" {send "$password\r"; exp_continue} eof } expect"#"
send "rm -rf ${file_delete}\r"
expect eof
exit
Copy the code
How to eat: Ready to eat out of the box
Place the above two scripts in the same directory, open the terminal in the current directory, and enter
sh delete_file.sh
Copy the code
Wait until the deletion is complete
Note: With the fortress is not available.
It is the first time to write blog, welcome to study together, mention issue, merge new functions. 😝