Jenkins 是什么?
Jenkins是一款开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部署软件。
Jenkins 支持各种运行方式,可通过系统包、Docker 或者通过一个独立的 Java 程序。
Jenkins下载部署
①下载地址:https://jenkins.io/zh/download/
②上传war包到服务器
③复制到Tomcat的webapps目录下
cp /usr/local/jenkins.war /usr/local/apache-tomcat-8.5.50/webapps/
④启动Tomcat
输入访问地址:
http://192.168.67.128:8080/jenkins/
服务器查看密码命令:
cat /root/.jenkins/secrets/initialAdminPassword
按照提示输入初始密码:
选择安装推荐的插件(插件下载过程有点久):
创建用户:
jenkins全局配置:
新建项目前确保Ansible、Git等插件已安装好:
源码管理选项卡设置:
构建选项卡设置:
项目脚本:
hosts:
192.168.67.129 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123456
[mybatis_plus]
192.168.67.129
deploy.yml:
---
- hosts: mybatis_plus
tasks:
# - file: path=/tmp/mytemp state=directory mode=0755
# 存放脚本
- name: create script directory
file:
path: /script
state: directory
mode: 0755
# 存放jar包
- name: create /app/test
file:
path: /app/test
state: directory
mode: 0755
# 存放日志
- name: create /tmp/startlog directory
file:
path: /tmp/startlog
state: directory
mode: 0755
- name: copy readme
copy:
src: /script/readme
dest: /script/readme
- name: copy start.sh
copy:
src: /script/start.sh
dest: /script/start.sh
mode: u+x
- name: copy kill.sh
copy:
src: /script/kill.sh
dest: /script/kill.sh
mode: u+x
- name: copy mybatis-plus.jar
copy:
src: /root/.jenkins/workspace/Eportal/target/mybatis_plus.jar
dest: /app/test/mybatis_plus.jar
mode : 0755
- name: view readme
shell: cat /script/readme
- name : "stop the application if running"
shell : /script/kill.sh
- name: run start.sh
shell: nohup /script/start.sh &
脚本kill.sh:
PID=$(ps -ef |grep /app/test | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
PID=$(ps -ef |grep /app/ | grep -v grep | awk '{ print $2 }')
fi
if [ -z "$PID" ]
then
echo Application is already stopped
else
echo kill -9 $PID
kill -9 $PID
echo Application is stopped
fi
start.sh脚本:
/usr/local/jdk1.8.0_151/bin/java -jar /app/test/mybatis_plus.jar > /app/test/log.log
查看日志文件:
tail -100f log.log
服务确实起来: