环境准备:
10.23.160.50 gitlab 安装完成
10.23.160.70 jenkins 安装完成 jdk1.8.0
10.23.160.71 部署服务器
1.创建自由风格的项目
2.添加凭证和所要拉去的代码的git地址
添加凭证
3.构建项目测试拉取结果
5.创建流水项目进行自动化封装和推送到远程docker仓库
docker 封装镜像和推送到个人仓库,在这里是用的腾讯云私有仓库,也可以自己搭建harbor
pipeline { agent any stages { stage('pull code') { steps { git credentialsId: 'a7bca987-7181-4a80-8702-ceab5afda6ac', url: 'git@10.23.160.50:xrkmonitor/test.git' } } stage('docker build images') { steps { sh 'docker build --tag=nginx:v3 . ' sh 'docker tag nginx:v3 ccr.ccs.tencentyun.com/shiq/test:v3' sh 'docker tag nginx:v3 ccr.ccs.tencentyun.com/shiq/test:latest' } } stage('docker push images') { steps { sh 'sudo docker login --username=username --password=password ccr.ccs.tencentyun.com' sh 'docker push ccr.ccs.tencentyun.com/shiq/test:v3' } } } }
测试结果
6.进行远程项目部署
需要进行插件的安装:
Publish Over SSH 远程连接
SSH Pipeline Steps 远程连接
Deploy to container tomcat传输插件
SSH Pipeline Steps 常用参数可写成变量使用
远程连接---声明式测试
脚本远程连接 node { def remote = [:] remote.name = 'root' remote.host = '10.23.160.71' remote.user = 'root' remote.password = '123123' remote.allowAnyHosts = true stage('Remote SSH') { sshCommand remote: remote, command: "ls -lrt" sshCommand remote: remote, command: "for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done" } } 声明远程连接 pipeline { agent any stages { stage('SSH---') { steps { script { def remote = [:] remote.name = 'root' remote.host = '10.23.160.71' remote.user = 'root' remote.password = '123123' remote.allowAnyHosts = true sshCommand remote: remote, command: "ls -lrt" sshCommand remote: remote, command: "for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done" } } } } }
Publish Over SSH
需要在系统配置中 先添加需要远程的用户
找到流水线语法
sshPublisher(publishers: [sshPublisherDesc(configName: 'centos7-test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'pwd', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
pipeline { agent any stages { stage('Hello') { steps { sshPublisher(publishers: [sshPublisherDesc(configName: 'centos7-test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'pwd', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }
后面就是进行部署脚本编写了
pipeline { agent any stages { stage('SSH---') { steps { script { def remote = [:] remote.name = 'root' remote.host = '10.23.160.71' remote.user = 'root' remote.password = '123123' remote.allowAnyHosts = true sshCommand remote: remote, command: "docker login --username=username --password=password ccr.ccs.tencentyun.com" sshCommand remote: remote, command: "docker pull ccr.ccs.tencentyun.com/shiq/test:v2" sshCommand remote: remote, command: "docker run -d -p 80:80 --name test_web ccr.ccs.tencentyun.com/shiq/test:v2" } } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!