注:参考文章里面“(4)设置Windows远端管理(WS-Management,WinRM)”中的命令是在cmd中执行的,不是在PowerShell中执行
找见端口号为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\""在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
在启动服务的时候遇到了两个问题: (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
.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 - mvnwindows.yml的代码如下6:
ansible_ssh_user: xxx ansible_ssh_pass: xxxx ansible_ssh_port: 5985 ansible_connection: winrm ansible_winrm_server_cert_validation: ignoredeployauto.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