JENKINS通过跳板机上传文件

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"
                    }
                    

                }
            }
        }
    }
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐