需求
在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 示例(片段)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48// ...............节省篇幅,此处省略代码若干,如下是代码片段............... 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)
最后
以上就是粗犷身影最近收集整理的关于Jenkins 插件之 SSH Pipeline Steps的全部内容,更多相关Jenkins内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复