远程脚本
由于使用的expect,是不支持shell的位置变量语法的,需使用下面这种方式
scp.sh
cat expect
#!/usr/bin/expect
set timeout -1
set file_name [lindex $argv 0]
set dest_name [lindex $argv 1]
set DATE [exec date +%Y%m%d]
spawn scp $file_name root@asdsadasdasdas:$dest_name
expect {
"(yes/no)?" {
send "yes\n"
expect "*assword:" { send "asdasd@@\n"}
}
"*assword:" {
send "asdasd@@\n"
}
}
expect "100%"
expect eof
交互式远程执行
expect <<EOF
spawn ssh root@192.168.10.121
expect "password" {send "password\r"}
expect "#" {send "touch ${GitlabProjoctName}\r"}
expect "#" {send "exit\r"}
EOF
Jenkinsfile
stage('Push HuNan'){
agent {
label 'jenkins-zong'
}
steps{
sh '''
set -x
DATE=`date +%Y%m%d`
echo "Push Image to hunan"
# 创建对端目录
expect <<EOF
spawn ssh root@192.168.1.1
expect "password" {send "password\r"}
expect "#" {send "mkdir -p /data/buildImage/jar_tmp/${GitlabProjoctName}\r"}
expect "#" {send "exit\r"}
EOF
# 传包
/data/hn/expect /data/hn/jar/$DATE/${GitlabProjoctName}/${GitlabProjoctName}-1.0.0.jar /data/buildImage/jar_tmp/${GitlabProjoctName}
'''
sh """
expect <<EOF
set timeout -1
spawn ssh root@192.168.1.1
expect "password" {send "password\r"}
expect "#" {send "cd /data/buildImage/\r"}
expect "#" {send "docker build -t 192.168.123.11:9000/g01/${GitlabProjoctName}:${INPUT_PARAMS.releaseTag} -f /data/buildImage/Dockerfile/Dockerfile.${GitlabProjoctName} jar_tmp/${GitlabProjoctName}\r"}
expect "#" {send "docker push 192.168.123.11:9000/g01/${GitlabProjoctName}:${INPUT_PARAMS.releaseTag}\r"}
expect "#" {send "exit\r"}
EOF
"""
}
}