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)

发布了378 篇原创文章 · 获赞 1419 · 访问量 632万+

猜你喜欢

转载自blog.csdn.net/catoop/article/details/100511027