jenkins 通过 linux expect 实现自动部署

文件拷贝/dir_cp.sh

!/usr/bin/expect

set timeout 30
set host [lindex $argv 2]
set username [lindex $argv 1]
set passwd [lindex $argv 4]
set file [lindex $argv 0]
set targertDir [lindex $argv 3]
spawn scp -r $file $username@$host:$targertDir
expect "*password:"
send "$passwd\r"
expect eof

jar包执行/exec_jar.sh

!/usr/bin/expect

log_file /tmp/expect.log
set timeout 30
set username [lindex $argv 0]
set host [lindex $argv 1]
set passwd [lindex $argv 2]
set jarPath [lindex $argv 3]
spawn ssh $username@$host
expect "*password:"
send "$passwd\r"
expect "#"
send "cd $jarPath\r"
expect "#"
send "sh restart.sh\r"
expect "#"
send "exit\r"
expect eof

git拉取代码/git_pull.sh

!/usr/bin/expect

set timeout 30
set username [lindex $argv 2]
set passwd [lindex $argv 3]
set targertDir [lindex $argv 0]
set branch [lindex $argv 1]
cd $targertDir
spawn git pull origin $branch
expect "Username"
send "$username\r"
expect "Password"
send "$passwd\r"
expect eof

暂停查看jar是否已启动/pause.sh

!/bin/bash

port=$1
host=$2
code=000
while [ ${code} != '200' ]
do
code=`curl --connect-timeout 1 -m 2 -s -w "%{http_code}" "$host:$port/healthCheck" -o /dev/null`
echo ${code}
sleep 1s
done

发表评论

电子邮件地址不会被公开。