前端项目
- 复制静态文件到nginx站点目录,nginx -s reload
tar -czf anyops-devops-ui-1.1.1.tar.gz . --warning=no-file-changed
### 进入Web服务器nginx的站点目录下
[root@jenkins-agent ~]# wget --http-user=admin --http-passwd=admin123 http://139.198.170.122:8081/repository/anyops/com/anyops/anyops-devops-ui/1.1.1/anyops-devops-ui-1.1.1.tar.gz
#解压到这个目录下
[root@jenkins-agent html]# pwd
/usr/local/nginx/html
[root@jenkins-agent html]# ls
index.html static
## 触发nginx重载
[root@jenkins-agent html]# /usr/local/nginx/sbin/nginx -s reload
后端项目
- 复制jar包到目标目录, 使用nohup java -jar 启动服务。
- nohup java -jar app.jar >output 2>&1 &
Ansible工具应用
Ansible是一个IT自动化工具,简单易用。使用OpenSSH进行传输,可以配置系统,部署软件以及编排更高级的IT任务,例如持续部署。
- 官网:http://www.ansible.com/home
- 官网文档:http://docs.ansible.com/ansible/index.html
- Github地址:https://github.com/ansible
Ansible如何管理主机呢?清单文件(Host Inventory)Ansible的配置文件,对主机进行分类,用来告诉ansible需要管理哪些主机。默认文件为: /etc/ansible/hosts
## 定义主机
192.168.0.22
devops.server.com
jenkins.test.com
## 定义主机组
[webservers]
test.server.com
test2.server.com
命令与指令
检查主机连通性
[root@jenkins-agent ~]# #ssh-keygen
[root@jenkins-agent ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.10
[root@jenkins-agent ~]# ansible all -m ping
192.168.0.9 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.0.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.0.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
复制文件
[root@jenkins-agent ~]# ansible all -m copy -a "src=/root/anyops-devops-ui-1.1.1.tar.gz dest=/root"
yum安装软件
ansible webserver -m yum -a "name=lrzsz"
添加系统用户
ansible webserver -m user -a "name=zhangsan password=123456"
服务控制
ansible webserver -m service -a "name=sshd state=started"
java应用启动
[root@jenkins-agent ~]# nohup java -jar -Dserver.port=8091 anyops-devops-service-1.1.1.jar >anyops-devops-service.log 2>&1 &
[1] 27088
[root@jenkins-agent ~]# ps -ef | grep java
root 27088 22049 20 14:07 pts/2 00:00:06 java -jar -Dserver.port=8091 anyops-devops-service-1.1.1.jar
[root@jenkins-agent ~]# ps -ef | grep java | grep anyops
root 27088 22049 5 14:07 pts/2 00:00:06 java -jar -Dserver.port=8091 anyops-devops-service-1.1.1.jar
[root@jenkins-agent ~]# ps -ef | grep java | grep anyops | awk '{print $2}'
27088
发布脚本如下所示:
[root@jenkins-agent ~]# cat test.sh
#!/bin/bash
APP_NAME=$1
VERSION=$2
PORT=$3
start(){
proc_result=$(ps -ef | grep java | grep "${APP_NAME}" | grep -v grep || echo "false")
if [[ $proc_result == "false" ]];then
nohup java -jar -Dserver.port=8091 ${APP_NAME}-${VERSION}.jar >${APP_NAME}.log 2>&1 &
else
"echo skip start"
fi
}
stop(){
pid=$(ps -ef | grep java | grep "${APP_NAME}" | awk '{print $2}')
kill -15 $pid
}
check(){
proc_result=$(ps -ef | grep java | grep "${APP_NAME}" | grep -v grep || echo "false")
port_result=$(netstat -tpln | grep ${PORT} || echo "false")
url_result=$(curl -s http://localhost:${PORT} || echo "false")
if [[ $proc_result == "false" || $port_result == "false" || $url_result == "false" ]];then
echo "service is not running..."
else
echo "service is running..."
fi
}
case $4 in
start)
start
sleep 10s
check
;;
stop)
stop
sleep 10s
check
;;
restart)
stop
sleep 10s
start
sleep 10s
check
;;
check)
check
;;
*)
echo "error input,please sh $0 start|stop|check"
esac
[root@jenkins-agent ~]# ./test.sh anyops-devops-service 1.1.1 8091 check
service is running...
[root@jenkins-agent ~]# ./test.sh anyops-devops-service 1.1.1 8091 stop
service is not running...
[root@jenkins-agent ~]# ./test.sh anyops-devops-service 1.1.1 8091 start
service is running...
打印要发布的主机
stage("DeployHosts"){
steps {
script {
println(env.deployHosts)
for (host in "${env.deployHosts}".split(",")){
sh "echo ${host}"
}
}
}
[Pipeline] { (DeployHosts)
[Pipeline] script (hide)
[Pipeline] {
[Pipeline] echo
192.168.100.2,192.168.100.3
[Pipeline] sh
+ echo 192.168.100.2
192.168.100.2
[Pipeline] sh
+ echo 192.168.100.3
192.168.100.3
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
剩下的就是
测试主机的连通性 清理和创建发布目录 复制app 复制脚本 启动服务 检查服务
如果需要回滚,也就是将旧版本重新发布一下就行了,不需要备份老的版本