Jenkins 插件之 SSH Pipeline Steps

需求
在Jenkins中通过执行Jenkinsfile脚本,实现自动在多台机器上进行docker部署服务。

方法
方法大概可以分为如下几种:
一、直接跨机器执行SSH脚本
1.1 使用 Ansible 实现自动跨机器执行脚本
1.2 使用 Jenkins 插件 SSH Pipeline Steps 可以跨机器执行脚本

二、把每台目标机器都配置为 Jenkins Slave ,通过 Slave 执行脚本
1.1 动态控制 pipeline 的 node 或 agent

三、基于容器管理和编排的
docker swarm
k8s

步骤
本文是对 Jenkins 插件 SSH Pipeline Steps 的方法进行简单说明,抛砖引玉给个示例和思路。详情请查看插件官网的更多方法。
1、Jenkins 在插件管理中搜索并安装插件 SSH Pipeline Steps
2、如下 Pipeline 示例(片段

// ...............节省篇幅,此处省略代码若干,如下是代码片段...............

          stage('部署镜像'){
            ansiColor('xterm') {                 
              docker.withRegistry(REGISTRY_URL, REGISTRY_CREDENTIALS_ID){
                def imgName = "${REGISTRY_DOMAIN}/${DOCKER_NAMESPACE}/${project_name}:${tagName}";
                
                for (item in ipList.tokenize(',')){                
                  def sshServer = getServer(item)
                  
                  // 更新或下载镜像
                  sshCommand remote: sshServer, command: "docker pull ${imgName}"
                    
                  try{
                    // 停止容器
                    sshCommand remote: sshServer, command: "docker stop ${project_name}"
                    // 删除容器
                    sshCommand remote: sshServer, command: "docker rm -f ${project_name}"
                  }catch(ex){}
                  
                  // 启动容器
                  sshCommand remote: sshServer, command: "docker run -d --name ${project_name} -e TZ=Asia/Shanghai ${imgName}"
                  
                  // 清理none镜像
                  def clearNoneSSH = "n=`docker images | grep  '<none>' | wc -l`; if [ \$n -gt 0 ]; then docker rmi `docker images | grep  '<none>' | awk '{print \$3}'`; fi"
                  sshCommand remote: sshServer, command: "${clearNoneSSH}"
                }
              }
            }

// ...............节省篇幅,此处省略代码若干...............

// 插件 https://github.com/jenkinsci/ssh-steps-plugin#pipeline-steps
# 声明一个方法,其中 withCredentials 是 Jenkins 凭据管理中添加过的凭据
def getServer(ip){
    def remote = [:]
    remote.name = "server-${ip}"
    remote.host = ip
    remote.port = 22
    remote.allowAnyHosts = true
    withCredentials([usernamePassword(credentialsId: 'ServiceServer', passwordVariable: 'password', usernameVariable: 'userName')]) {
    //withCredentials([sshUserPrivateKey(credentialsId: 'ServiceServer', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
        remote.user = "${userName}"
        remote.password = "${password}"
    }
    return remote
}

本文所有目标机器的账号密码都一样,如果你机器账号密码差异不同,你需要更新详细的处理。
本文的主要目的是告诉大家 Jenkins 有这么一个插件存在,并且可以用来干什么。

(END)

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

catoop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值