本文主要是介绍jenkins+gitlab+ansible-tower实现发布,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前提准备:
gitlab中上传相应的jenkinsfile文件和源码。
安装和破解ansible-tower。
安装jenkins。
大致流程:从gitlab中拉取文件,存放到windows机器上,使用nuget等进行打包到windows中,使用sshPublisher语句传输到远程ansible-tower的机器上,打包传输完成后,使用ansible-tower进行发布。
机器分布:
jenkins:192.168.50.155 gitlab:192.168.50.156 ansible-tower:192.168.50.157
后端服务器:192.168.50.154 192.168.50.155
1.jenkins中的配置
第一步:配置gitlab的用户名和密码
第二步:新建项目item
第三步:得到的界面
2.配置Nodes.配置window机器将项目打包
配置好后,还需要在节点windows服务器中执行相关的命令,才能建立连接。参考
https://blog.csdn.net/weixin_41238626/article/details/137828742
3.配置远端ansible-tower服务器相关
参考 https://blog.csdn.net/qq_25646191/article/details/109103252
需要安装Publish over SSH插件。输入密码后可以点击右下角进行测试 test configuration
4 在打包机器上安装dotnet版本
需要安装dotnet环境
https://download.visualstudio.microsoft.com/download/pr 需要配置环境变量
安装 nuget 需要配置环境变量
安装vs https://c2rsetup.officeapps.live.com/ 工作负载选择ASP.NET和Web开发、.NET桌面开发
参考:https://www.cnblogs.com/sgxw/p/15607574.html
proget 8624端口部署–》安装
参考: https://www.cnblogs.com/zerodai/p/10796535.html
5 测试查看是否能够打包已经是否能够传输到ansible-tower中
Jenkinsfile文件内容
pipeline{parameters {string defaultValue: '', description: 'tag', name: 'Tag', trim: falsechoice choices: ['class-assistant-linux-pr', 'class-assistant-linux'], description: '', name: 'TARGET_HOST'choice choices: ['Release', 'Publish', 'Alpha', 'Debug'], description: '', name: 'Configuration'
}agent nonestages{stage("build"){agent {label "win-110-88"}steps("first steps"){bat 'nuget.exe restore .\\src\\ClassAssistant.sln -Source http://192.168.60.216:8624/nuget/Proget/'bat 'dotnet publish .\\src\\ClassAssistant.Apis\\ClassAssistant.Apis.csproj --force -o ./publish/publish --configuration %Configuration%'powershell 'Get-ChildItem -Path $ENV:WORKSPACE/publish/publish/* -Include appsettings.*,log4net.config -Recurse | Remove-Item'bat '''cd %WORKSPACE%\\publish\\publish\\"C:\\Program Files\\7-Zip\\7z.exe" a ..\\latest.zip .'''archiveArtifacts '/publish/*.zip'}}stage("sendfile and ansible job"){agent {label "win-110-88"}steps("sendfile steps"){sshPublisher(publishers: [sshPublisherDesc(configName: 'ansible-50.157', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/${TARGET_HOST}/', remoteDirectorySDF: false, removePrefix: '/publish', sourceFiles: '/publish/*.zip')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])}}}
}
6 ansible-tower中中进行相关配置配置发布
参考:https://blog.csdn.net/qq_48736646/article/details/137777740
ansible剧本内容
class-test.yaml
---
- name: iis pull latest.zip and updatehosts: class-assistant-linuxgather_facts: falsemax_fail_percentage: 100serial: 2vars:PATH: class-assistant-linux#group "group:" 重启group需要加: program program重启单个项目直接写项目名称AppPool: core-class-assistant-linuxvars_files:- ./extend_vars.yamlroles:- update
extend_vars.yaml
iis_path:class-assistant-linux:192.168.50.154:- "/opt/class-assistant-linux"192.168.50.155:- "/opt/class-assistant-linux"
hosts:
[class-assistant-linux]
192.168.50.154
192.168.50.155update/tasks/main.yml
---
- name: url get fileshell: scp -o StrictHostKeyChecking=no root@192.168.50.157:/data/win_iis/{ PATH }/latest.zip /tmp/{{ PATH }}_latest.ziptags: update
- name: unzip fileshell: sudo unzip -q -o /tmp/{{ PATH }}_latest.zip -d {{ item }}with_items: "{{ iis_path[PATH][inventory_hostname] }}"tags: update
- name: restart webapppoolshell: sudo supervisorctl restart {{AppPool}}- name: Remove a file, if presentshell: rm -f /tmp/{{ PATH }}_latest.ziptags: update
7 结果
这篇关于jenkins+gitlab+ansible-tower实现发布的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!