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文件内容

bash 复制代码
pipeline{
    parameters {
    string defaultValue: '', description: 'tag', name: 'Tag', trim: false
    choice choices: ['class-assistant-linux-pr', 'class-assistant-linux'], description: '', name: 'TARGET_HOST'
    choice choices: ['Release', 'Publish', 'Alpha', 'Debug'], description: '', name: 'Configuration'
}
    agent none

    stages{
        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剧本内容

bash 复制代码
class-test.yaml
---
- name: iis pull latest.zip and update
  hosts: class-assistant-linux
  gather_facts: false
  max_fail_percentage: 100
  serial: 2
  vars:
    PATH: class-assistant-linux

#group "group:" 重启group需要加:  program program重启单个项目直接写项目名称
    AppPool: core-class-assistant-linux
  vars_files:
    - ./extend_vars.yaml
  roles:
    - 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.155

update/tasks/main.yml
---
- name: url get file
  shell: scp -o StrictHostKeyChecking=no root@192.168.50.157:/data/win_iis/{ PATH }/latest.zip /tmp/{{ PATH }}_latest.zip
  tags: update
- name: unzip file

  shell: sudo unzip  -q -o /tmp/{{ PATH }}_latest.zip -d {{ item }}
  with_items: "{{ iis_path[PATH][inventory_hostname] }}"
  tags: update
- name: restart webapppool
  shell: sudo supervisorctl restart {{AppPool}}

- name: Remove a file, if present
  shell: rm -f /tmp/{{ PATH }}_latest.zip

  tags: update

7 结果

相关推荐
zxd02031116 小时前
EFK(Elasticsearch + Fluentd + Kibana) 日志收集系统
运维·docker·jenkins
终端行者17 小时前
Jenkins Pipeline 企业级用法 参数化构建+Ansible发布---上
docker·ansible·jenkins·cicd
认真的薛薛20 小时前
Linux运维:Jenkins部署
linux·运维·jenkins
终端行者20 小时前
jenkins Pipeline 企业级别的流水线构建 Docker 负责打包 最后上传到Nexus
ci/cd·docker·jenkins
淼淼爱喝水20 小时前
【Ansible 入门实战】三种变量详解
java·linux·数据库·ansible·playbook
木雷坞21 小时前
csdn-enterpriseGitLab Runner docker pull 慢:并行流水线镜像拉取排查
运维·docker·容器·gitlab
一只大袋鼠21 小时前
Git (三):Tag 标签管理、图形工具、IDEA 集成与 GitLab 私有化部署
开发语言·git·gitlab
认真的薛薛21 小时前
Linux运维:Jenkins+Argocd
linux·运维·jenkins
cgsthtm2 天前
openEuler release 24.03 (LTS-SP2) 安装 jenkins
jenkins·openeuler·firewall-cmd·jenkins.reps
IT界的老黄牛2 天前
Flink 重启变双开:一次部署引发的两个 CDC 任务并发消费
大数据·flink·jenkins