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 --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

image.png

 

 后端项目

  • 复制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 复制脚本 启动服务 检查服务

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值