gitlab中自動將maven專案部署到windows伺服器
阿新 • • 發佈:2019-01-05
gitlab實現自動部署,程式碼根目錄中需要新增.gitlab-ci.yml以及ansible資料夾。ansible資料夾的目錄結構如下:
- .gitlab-ci.yml
- ansible
- group_vars
- windows.yml
- deployauto.yaml
- hosts
- group_vars
實現中遇到的問題
(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