1.配置多jdk环境
2.配置多maven环境
3.配置maven项目
3.1 配置部署主机
[root@jenkins war_update]# cat hosts_test
[webservice]
192.168.1.191
[root@jenkins war_update]# cat hosts_prod
[webservice]
192.168.1.43
192.168.1.254
3.2 配置git commit id
3.3 配置git仓库
3.4 配置build参数
3.5 构建完毕后调用ansible自动化工具部署
#选择角色
[root@jenkins code_cicd]# cat war_update.yaml
- hosts: webservice
serial: 1
roles:
- war_update
#任务步骤
[root@jenkins code_cicd]# cat war_update.yaml
- hosts: webservice
serial: 1
roles:
- war_update
[root@jenkins code_cicd]# cat war_update/tasks/main.yaml
- name: 0. Get System Time
shell:
cmd: "echo $(date +%F_%H_%M)"
register: Date
delegate_to: 127.0.0.1
- name: 1. Get Git_Commit_Id
shell:
cmd: "echo ${git_commit_id}|cut -c 1-8"
register: Git_Commit_Id
delegate_to: 127.0.0.1
- name: 2. Get Workspace Ansible Path
shell:
cmd: "echo ${WORKSPACE}"
register: Workspace_Path
delegate_to: 127.0.0.1
- name: 3. Get War Package Path
shell:
cmd: find "{{ Workspace_Path.stdout }}"/target -name "*.war"
delegate_to: 127.0.0.1
register: War_Path
- name: 4. Create Work Directory
file:
path: "/data/maven_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}"
state: directory
- name: 5. Scp War Package To Remote Host
unarchive:
src: "{{ War_Path.stdout }}"
dest: "/data/maven_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}"
- name: 6. Remove The Service
replace:
regexp: "server {{ ansible_default_ipv4.address }}"
path: /etc/nginx/conf.d/maven_slb.conf
replace: "#server {{ ansible_default_ipv4.address }}"
delegate_to: 192.168.1.191
notify: Reload_Nginx
- meta: flush_handlers
- name: 7. Stop Tomcat Service
systemd:
name: tomcat
state: stopped
- name: 8. Check Tomcat Port
wait_for:
port: "{{ Tomcat_Port }}"
state: stopped
- name: 9. Unlink Path
file:
path: /data/ROOT
state: absent
- name: 10. Create Links Path
file:
src: "/data/maven_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}"
dest: "/data/ROOT"
state: link
- name: 11. Start Tomcat Service
systemd:
name: tomcat
state: started
- name: 12. Check Tomcat Port
wait_for:
port: "{{ Tomcat_Port }}"
state: started
- name: 13. Create The Service
replace:
regexp: "#server {{ ansible_default_ipv4.address }}"
path: /etc/nginx/conf.d/maven_slb.conf
replace: "server {{ ansible_default_ipv4.address }}"
delegate_to: 192.168.1.191
notify: Reload_Nginx
- meta: flush_handlers
[root@jenkins code_cicd]# cat war_update/vars/main.yaml
Tomcat_Port: 8080
标签:java,name,stdout,maven,Path,jenkins,war
From: https://www.cnblogs.com/yangtao416/p/16895250.html