pipeline {
agent any
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git 'https://gitee.com/yeyeyeyeto/SpringBootTest.git'
bat 'mvn clean package -DskipTests'
}
}
stage('SSH') {
steps {
script {
def remote = [:]
remote.name = 'test'
remote.host = '124.223.222.123'
remote.port = 2222
remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: '124.223.222.123', passwordVariable: 'password', usernameVariable: 'username')]) {
remote.user = "${username}"
remote.password = "${password}"
}
sshCommand remote: remote, command: " sshpass -p '目标机密码' ssh -p 22 root@10.0.4.9 'echo 'wudasdasdasdi' > test.txt' "
sshPut remote: remote, from: './target/springboottest1-0.0.1-SNAPSHOT.jar', into: '/opt/project/test/'
sshCommand remote: remote, command: " sshpass -p '目标机密码' scp -o 'StrictHostKeyChecking no' -P 22 '/opt/project/test/springboottest1-0.0.1-SNAPSHOT.jar' root@10.0.4.9:/opt/project/test/springboottest1-0.0.1-SNAPSHOT.jar"
}
}
}
}
}
二、文件夹目录复制
pipeline {
agent any
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git 'https://gitee.com/yeyeyeyeto/SpringBootTest.git'
bat 'mvn clean package -DskipTests'
}
}
stage('SSH') {
steps {
script {
def remote = [:]
remote.name = 'test'
remote.host = '124.223.222.123'
remote.port = 2222
remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: '124.223.222.123', passwordVariable: 'password', usernameVariable: 'username')]) {
remote.user = "${username}"
remote.password = "${password}"
}
sshCommand remote: remote, command: " sshpass -p '目标机密码' ssh -p 22 root@10.0.4.9 'echo 'wudasdasdasdi' > test.txt' "
sshPut remote: remote, from: './target', into: '/opt/project/test/'
sshCommand remote: remote, command: " sshpass -p '目标机密码' scp -o 'StrictHostKeyChecking no' -P 22 -r '/opt/project/test/target' root@10.0.4.9:/opt/project/test"
}
}
}
}
}
三、使用加密信息,防止密码泄露
pipeline {
agent any
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git 'https://gitee.com/yeyeyeyeto/SpringBootTest.git'
bat 'mvn clean package -DskipTests'
}
}
stage('SSH') {
steps {
script {
def remote = [:]
remote.name = 'test'
remote.host = '124.223.222.123'
remote.port = 2222
remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: '124.223.222.123', passwordVariable: 'password', usernameVariable: 'username')]) {
remote.user = "${username}"
remote.password = "${password}"
}
sshCommand remote: remote, command: " sshpass -p 'A!199662' ssh -p 22 root@10.0.4.9 'echo 'wudasdasdasdi' > test.txt' "
sshPut remote: remote, from: './target', into: '/opt/project/test/'
withCredentials([usernamePassword(credentialsId: '1.117.77.230', passwordVariable: 'password', usernameVariable: 'username')]) {
sshCommand remote: remote, command: " sshpass -p '${password}' scp -o 'StrictHostKeyChecking no' -P 22 -r '/opt/project/test/target' root@10.0.4.9:/opt/project/test"
}
}
}
}
}
}