Jenkins+ansible前后端构建项目
Jenkins服务器准备
## Jenkins服务器安装git,ansible工具
[root@jenkins ~]# yum -y install epel-release git ansible
##本地域名解析
[root@jenkins ~]# vim /etc/hosts

##ansible配置主机清单
[root@jenkins ~]# vim /etc/ansible/hosts

##准备ssh密钥对
[root@jenkins ~]# ssh-keygen 一路回车
##将产生的公钥发送给主机清单中的服务器
[root@jenkins ~]# ssh-copy-id tomcat
[root@jenkins ~]# ssh-copy-id nginx
##修改ansible配置文件
[root@jenkins ~]# vim /etc/ansible/ansible.cfg

配置maven,jdk,ansible,nodejs工具




Jenkins+gitlab配置
gitlab+Jenkins的配置略


gitlab仓库配置Jenkins公钥
[root@jenkins ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDK/eI5/n5tlhURj7Zhy/7NayeSzBiD4Ew+HRMIxZBBYP3XIS5ZjplJhhCqC8NFHy8DQWrskakB1t/A7CGj9QB27obe/joGRM5Hr0Dx01YNMqA3h0JOMFUqe/1ekbTyARs4x5Z8FTL22OFDLyIRc7y0oluUajz+EJp6btWYgIckhuQzSYEB7T/AncfkNmgd5XBcu7LCkVpEhatj6DqirNjvHg1K6fJH5lIkkiQ7lh2O2bkctliIRZYdmvrYef6BUXrCFialJU/xihS1/mAOqqMDAJIUqvD1Xq5x4mWjb3Nw4PI8xJgGaTBlPsHc6+MLn5kc4Bw+NRbQYlD5jA0ZogLD root@client

ansible前端项目打包
ansible ad-hoc执行打包
注意
采用ansible进行打包并发送到客户端的时候 不需要构建后的操作 ansible本身相当于在远程主机上执行操作
所以 在ansible命令执行之前,先要将Jenkins拉下的项目代码进行打包,然后使用ansible命令将打包后的项目代码发送到远程主机








ansible playbook打包
编写一个剧本
[root@jenkins playbook]# pwd
/etc/ansible/playbook
[root@jenkins playbook]# vim ruoyiweb.yml
---
- hosts: web
user: root
tasks:
- name: 复制打包的项目代码
unarchive:
src: /root/.jenkins/workspace/ruoyiweb/ruoyi-ui/dist.tar.gz
dest: /ruoyi/ruoyi-ui/
- name: 赋予项目代码权限
shell: chmod 755 /ruoyi/ruoyi-ui/ -R




ansible 后端项目打包
anansible ad-hoc模式执行打包







anansible playbook模式执行打包
编写一个playbook
[root@jenkins playbook]# pwd
/etc/ansible/playbook
[root@jenkins playbook]# vim ruoyijava.yml
---
- hosts: java
user: root
tasks:
- name: 将打包的后端代码发送的远程主机
copy:
src: /root/.jenkins/workspace/ruoyijava/ruoyi-admin/target/ruoyi-admin.jar
dest: /opt/java
- name: 执行脚本启动后端代码
shell: nohup /opt/java/java1.sh &

Jenkins配置ansible playbook




【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)