gitlab中自动将maven项目部署到windows服务器

xiaoxiao2021-02-27  450

gitlab实现自动部署,代码根目录中需要添加.gitlab-ci.yml以及ansible文件夹。ansible文件夹的目录结构如下:

.gitlab-ci.ymlansible group_vars windows.ymldeployauto.yamlhosts

实现中遇到的问题

(1)Windows机子配置1

注:参考文章里面“(4)设置Windows远端管理(WS-Management,WinRM)”中的命令是在cmd中执行的,不是在PowerShell中执行

(2)Windows中删除进程

找见端口号为8087的进程的PID:

netstat -ano | findstr 8087

强制删除这个进程:

taskkill /f /pid xxx

这两个操作合并到一起:

for /f tokens^=5 %i in ('netstat -ano ^| findstr 8087') do /f /pid %i

注: (1)上面这句话仅限在cmd中执行,如果在bat文件中,语法格式要修改 (2)因为findstr 8087会有两条记录,但是都是同一个PID,杀掉第一个记录对应的PID之后,再杀第二个记录对应的PID时就会报“错误,没有找到进程xxx”,所以在deployauto.yaml杀掉进程这一步要添加:

ignore_errors: true

(3)这句命令在deployauto.yaml中格式如下

ignore_errors: true name: 杀掉industryReport进程 raw: "CMD /C \"for /F \"\"tokens=5\"\" %i in ('netstat -ano ^| findstr 8087') do taskkill /f /pid %i\""

(3)解压bz2文件

在gitlab-runner服务器上执行ansible,解压Windows服务器上的bz2文件:

ansible -i ansible/hosts windows -m win_unzip -a "src=C:\industryReport\industryReport.bz2 dest=C:\industryReport\ recurse=yes rm=true"

上面的语句会报错,前提是需要在Windows服务器上面安装pscx2

(4)在Windows中启动industryReport

在启动服务的时候遇到了两个问题: (1)用win_commnd启动服务:

ansible -i ansible/hosts windows -m win_command -a "CMD /C 'java -jar c:\industryReport\industryReport.jar'"

报错,提示JVM内存不够,原来是Windows的BUG3。Windows当时配置:Server 2008 R2 SP1,PowerShell 3.0. 最后Windows Management Framework升级到4.0,问题解决。 (2)但是启动服务的ansible命令没有返回值 解决方式:以windows服务的形式启动服务4

(5)yaml文件语法检查5

各文件代码展示

.gitlab-ci.yml文件代码如下:

stages: - build - deploy build project: stage: build script: - mvn clean test allow_failure: true #deploy dev project: # stage: deploy # script: # - mvn clean package -Pdev -Dmaven.test.skip=true # - cd ${CI_PROJECT_DIR} # - tar -acf ansible/roles/uat2/files/QuestionnaireApi.bz2 target # - ansible-playbook -i ${CI_PROJECT_DIR}/ansible/hosts ${CI_PROJECT_DIR}/ansible/site-uat2.yml # allow_failure: false # tags: # - java # - mvn # when: on_success deploy uat project: stage: deploy script: - mvn clean package -Puat -Dmaven.test.skip=true - cd ${CI_PROJECT_DIR}/target/ - tar -acf ../ansible/industryReport.bz2 * - cd ../ - ansible-playbook -i ${CI_PROJECT_DIR}/ansible/hosts ${CI_PROJECT_DIR}/ansible/deployauto.yaml --extra-vars="WORK_DIR="${CI_PROJECT_DIR} allow_failure: false when: on_success only: - /^Release.*$/ tags: - java - mvn

windows.yml的代码如下6:

ansible_ssh_user: xxx ansible_ssh_pass: xxxx ansible_ssh_port: 5985 ansible_connection: winrm ansible_winrm_server_cert_validation: ignore

deployauto.yaml代码如下:

--- - hosts: windows name: 部署industryReport应用 tasks: - ignore_errors: true name: 杀掉industryReport进程 raw: "CMD /C \"for /F \"\"tokens=5\"\" %i in ('netstat -ano ^| findstr 8087') do taskkill /f /pid %i\"" - name: 删除历史war包 win_file: "path=\"C:/industryReport/*\" state=absent" - name: 上传industryReport.bz2文件 win_copy: "src={{WORK_DIR}}/ansible/industryReport.bz2 dest=\"C:/industryReport/\"" - name: 解压industryReport.bz2文件 win_unzip: dest: "C:\\industryReport\\" recurse: true rm: true src: "C:\\industryReport\\industryReport.bz2" - name: 启动industryReport服务 register: startindustryReport win_service: name: industryReport start_mode: auto state: started - debug: var=startindustryReport vars: - WORK_DIR: "${CI_PROJECT_DIR}"

hosts代码如下:

[windows] xxx.xxx.xxx.xxx
http://www.cnblogs.com/kingleft/p/6391652.html ↩http://docs.ansible.com/ansible/win_unzip_module.html ↩http://docs.ansible.com/ansible/intro_windows.html#windows-system-prep ↩http://www.open-open.com/lib/view/open1415888822383.html http://yajsw.sourceforge.net/ http://yajsw.sourceforge.net/#mozTocId212903 https://www.youtube.com/watch?v=gX9tGInrIvg ↩http://www.yamllint.com/ ↩https://docs.ansible.com/ansible/intro_windows.html#inventory ↩
转载请注明原文地址: https://www.6miu.com/read-912.html

最新回复(0)