#先声明出一个函数,获取主机信息 def getHost(){ def remote = [:] remote.name = ‘test‘ remote.host = ‘1.1.1.1‘ remote.user = ‘root‘ remote.password = ‘xxxxx‘ remote.allowAnyHosts = true return remote } #定义流水线 pipeline { agent any stages { #获取远程主机信息 stage(‘get host‘) { steps { script { server = getHost() } } } #在远程主机上执行命令 stage(‘Remote SSH‘) { steps { sshCommand remote: server, command: "ls -lrt" sshCommand remote: server, command: "for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done" } } #拉取git代码 stage(‘checkout‘) { steps { checkout([$class: ‘GitSCM‘, branches: [[name: ‘*/master‘]], extensions: [], userRemoteConfigs: [[url: ‘https://gitee.com/dl88250/solo.git‘]]]) } } #进行docker build创建镜像 stage(‘docker build‘) { steps { script { sh ‘docker build -t solo:v1 ./‘ } } } #部署到远程服务器中 stage(‘deploy‘) { steps { sshCommand remote: server, command: "docker run -itd --name mysolo solo:v1" } } #测试 stage(‘test‘) { steps { echo "testing" } } } }
原文:https://www.cnblogs.com/fengzi7314/p/14779103.html