Jenkins CD VM部署实践 03 前后端项目的发布

网友投稿 695 2022-11-30

Jenkins CD VM部署实践 03 前后端项目的发布

Jenkins CD VM部署实践 03 前后端项目的发布

前端项目

复制静态文件到nginx站点目录,nginx -s reload

tar -czf anyops-devops-ui-1.1.1.tar.gz . --warning=no-file-changed### 进入Web服务器nginx的站点目录下[root@jenkins-agent ~]# wget ----html]# pwd/usr/local/nginx/html[root@jenkins-agent html]# lsindex.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任务,例如持续部署。

官网:Inventory)Ansible的配置文件,对主机进行分类,用来告诉ansible需要管理哪些主机。默认文件为: /etc/ansible/hosts

## 定义主机192.168.0.22devops.server.comjenkins.test.com## 定义主机组[webservers]test.server.comtest2.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 ping192.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 javaroot 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 anyopsroot 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/bashAPP_NAME=$1VERSION=$2PORT=$3start(){ 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 || 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 instart) 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 checkservice is running...[root@jenkins-agent ~]# ./test.sh anyops-devops-service 1.1.1 8091 stopservice is not running...[root@jenkins-agent ~]# ./test.sh anyops-devops-service 1.1.1 8091 startservice 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] echo192.168.100.2,192.168.100.3[Pipeline] sh+ echo 192.168.100.2192.168.100.2[Pipeline] sh+ echo 192.168.100.3192.168.100.3[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

剩下的就是

测试主机的连通性 清理和创建发布目录 复制app 复制脚本 启动服务 检查服务

如果需要回滚,也就是将旧版本重新发布一下就行了,不需要备份老的版本

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:kubectl get cs查看组件状态kube-scheduler和kube-controller-manager显示unhealthy
下一篇:SpringBoot之groups应对不同的Validation规则自定义方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~