使用 Jenkins 编译.net 项目需要用到 windows 的 slave 节点,安装依赖的工具:如 MSBuild 和.NET Framework, winrar 等
MSBuild 可以直接安装和开发使用一致版本的 Visual Studio
,使用其自带的 MSBuild.exe,比如我这里安装了 Visual Studio 2019
对应的路径为 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\amd64\MSBuild.exe
把用到的工具的路径加入到 windows slave 配置环境变量 path 中
jenkins pipeline
pipeline { agent { label 'windows-slave' } parameters{ gitParameter branchFilter: '.*', tagFilter: '.*', defaultValue: 'dev', name: 'TAG', type: 'PT_BRANCH_TAG', selectedValue: 'TOP', sortMode: 'DESCENDING_SMART' choice(name: 'PLAYBOOK', choices: 'net\nnet-rollback', description: 'net 部署\nnet-rollback 回滚') choice(name: 'HOSTS', choices: 'test-pms-group', description: '选择发布的主机组:\ntest-pms-group 测试环境') } environment { GIT = 'http://example.com/mgpms/MG_HuiZhu.git' PROJECT = 'MG_HuiZhu' APP_PATH = 'D:\\data\\apps\\MG_HuiZhu\\' BUILDFILE = 'MG_HuiZhu2.0\\Huazhu.PMS.WebSite\\Huazhu.PMS.WebSite.csproj' SOLUTIONDIR = "${WORKSPACE}\\MG_HuiZhu2.0\\" PACKAGE = "${WORKSPACE}\\MG_HuiZhu2.0\\Huazhu.PMS.WebSite\\obj\\Release\\Package\\PackageTmp\\" CONFIGURATION = 'Release' PLAYBOOK = "$PLAYBOOK" // ansible的的的palybook MAIL_TO = "yunwei@hhotel.com" ZIP = "target" CHECK_TAG = "${TAG}" // 分支或tag } stages { stage ('克隆代码') { steps { deleteDir() // 清理工作目录 git credentialsId: '9c9b7b17-04d5-47de-9b8f-6207a78e6973', url: "${GIT}" bat 'git checkout %CHECK_TAG%' } } stage ('编译打包') { steps { script { bat 'echo %TIME%' bat 'MSBuild %BUILDFILE% /p:Configuration=%CONFIGURATION% /p:SolutionDir=%SOLUTIONDIR% /t:Rebuild /p:DeployOnBuild=true /p:AllowUntrustedCertificate=True' bat 'rd /s/q D:\\jenkins\\workspace\\test-MG_HuiZhu\\MG_HuiZhu2.0\\Huazhu.PMS.WebSite\\Scripts\\Plugin\\My97DatePicker\\' bat 'winrar a -ep1 -r target.zip %PACKAGE%' stash(name: "target", includes: "target.zip") } } } stage ('部署') { agent { label "master" } environment { TIME = sh(script: "date '+%Y%m%d%H%M%S'", returnStdout: true).trim() } steps { deleteDir() // 清理工作目录 unstash("target") ansiblePlaybook( playbook: "/data/ansible/playbook/${PLAYBOOK}.yml", extraVars: [ APP_PATH:"${APP_PATH}", HOSTS:"${HOSTS}", PROJECT:"${PROJECT}", WORKSPACE:"${WORKSPACE}", ZIP:"${ZIP}", TIME:"${TIME}" ] ) } } } }
playbook.yml
--- - hosts: "test-pms-all" serial: "50%" tasks: - name: create app dir win_command: CMD /C "md D:\data\apps\{{PROJECT}}" ignore_errors: True - name: create backup dir win_command: CMD /C "md D:\data\backup\{{TIME}}" ignore_errors: True - name: backup website win_copy: src: D:\data\apps\{{PROJECT}} dest: D:\data\backup\{{TIME}} remote_src: True ignore_errors: True - name: transfer artifacts to target hosts win_copy: src: "{{WORKSPACE}}/{{ZIP}}.zip" dest: D:\data\apps\{{PROJECT}}\ - name: unzip artifacts win_unzip: src: D:\data\apps\{{PROJECT}}\target.zip dest: D:\data\apps\{{PROJECT}} creates: no delete_archive: yes - name: restart website win_iis_website: name={{PROJECT}} state=restarted
ansible 使用 wrm 来对 windows 服务器进行操作。
要求:
.NET Framework 3.0 以上
Powershell 3.0 以上
开启 winrm
使用管理员权限打开 Powershell
查看版本
get-host
更改 powershell 策略为 remotesigned
set-executionpolicy remotesigned get-executionpolicy
查看 winrm service listener
https 方式的使用 5986,http 使用 5985
winrm enumerate winrm/config/listener
配置 winrm 服务
winrm quickconfig
配置 auth 为 true(默认为 false)
winrm set winrm/config/service/auth '@{Basic="true"}'
配置允许非加密方式
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
安装 ansible 依赖包 pywinrm 模块
pip install pywinrm
模块路径
常用模块
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于