Stable deployment of Java projects

Deploy using shell script

The window system has a bat file to execute the script, and the shell can execute the script under linux

shell statement

First, let’s briefly record the meaning of the shell statement:

  1. $# represents the number of parameters;
  2. -lt (less than) less than -gt (greater than) greater than -eq (equal) equal -ne (not equal) not equal -ge (greater than or equal) greater than or equal -le (less than or equal) less than or equal to ;
  3. echo string output, quotation marks can be omitted;
  4. $0 is the name of the shell script itself, $1 is the first parameter of the script, 2 is the second parameter of the script, 2 is the second parameter of the script,2 The script passes in the second parameter, $ is the current process ID number of the script running, $! is the last process ID number running in the background;
  5. set -e can avoid the problem of continuing execution after the operation fails. Once the code that appears after "set -e" has a non-zero return value, the entire script will exit immediately. Set +e will be encountered after this sentence. If the return value is non-zero, it will continue to execute. When set -e encounters a non-zero return value after this sentence, it will exit directly;
  6. exit 0 - is a command that exits normally, exit 1 - is a command that exits abnormally, echo $? gets the result of the last command execution, if it is 0, it is executed normally, if it is not 0, there is an exception in the execution;
  7. {} curly brackets, the user executes the command, the last command in {} must use a semicolon, there is a space before the first command and the left bracket, and the commands are separated by;
  8. () Parentheses, a collection of commands. The commands in parentheses will be executed in the subshell. Command replacement $(cmd) is equivalent to cmd. Execute the cmd in parentheses and initialize the data array = (abcd)
  9. (()) Double parentheses, commands for integer operations, cannot operate on decimals (floating point numbers) or strings
  10. function(){} - represents a function
  11. if [-f /-z]; then execution content fi -f is true if it exists and is an ordinary file; -z is true if the string length is zero; -n is true if it determines a non-empty string
  12. $(cd dirname $0;pwd) displays the directory where the executed script is located, displayed as an absolute path; pwd displays the directory where the currently typed command is located, displayed as an absolute path; Note: 1. When comparing in the shell, a result
    of
    0 represents true , 1 represents false.
    2. Comparators such as -eq and -ne can only be used for numerical comparison, and any characters will be converted into numbers first and then compared.

Docker common commands

docker  info/version #docker环境信息
docker ps # 查看运行的容器信息
docker ps -a # 所有容器信息
docker search xx #查找镜像
docker pull xx #拉取镜像
docker rmi -f 镜像ID/name #删除本地镜像
docker logs -f 容器ID #跟踪实时日志
docker top 容器ID # 查看容器中进程信息
docker inspect 容器ID/name #获取容器/镜像的元数据 -f :指定返回值的模板文件-s :显示总的文件大小--type :为指定类型返回JSON
docker inspect --format '{
    
    {.LogPath}}' 容器ID #查看某容器的日志地址
docker build #构建镜像
docker run #启动镜像
docker system prune #清理磁盘,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)
docker system prune -a #命令清理得更加彻底,可以将没有容器使用Docker镜像都删掉

Simple shell deployment script implementation

# 编译打包过程
set -e
cd /xx/xx # 进入项目目录
git checkout xxx #切换分支
git pull #拉取最新代码
cd /xx/xx/xx #进入项目具体模块
mvn clean install -Dmaven.test.skip=true #使用maven 打包
propid=`ps -aux|grep xx.jar |grep -v grep| awk ' {print $2}
#ps -ef/aux |grep java就是通过管道的方式,将ps命令查询出来的进程信息内容筛选出与jar进程相关的数据
#-v 参数,作用是反转查找,即过滤出除了grep的其他数据
#  awk ' {
    
    print $2} awk对每行处理 print打印对应第二行的数据 也就是 xx.jar的pid
if[-n "${proid}"]; then
	echo "存在进程: ${propid}"
	kill -9 ${
    
    proid}
	echo "杀死进程:${propid}"
eles
	echo "进程不存在"
fi
#生成docker镜像
set -e
cd /xxx/xxx # 找到DockerFile所存在的目录
docker build -t x source/xx/
# -f :指定要使用的Dockerfile路径 -t name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签
# 后面部分为Dockerfile路径
set +e
#关闭名为x的容器
docker kill x
#删除停止的容器
docker rm x
set -e
docker run --name x -d -it --net="host" \
	 -v /home/xx.properties:/opt/xx/application-admin.properties \
	 -v /opt/files:/opt/files \
	 -v /opt/logs:/opt/logs \
	 -v /home:/home \
	 --security-opt label:disable \
	  x
# -v 给容器挂载存储卷,挂载到容器的某个目录
# -d 指定容器运行于前台还是后台,默认为 false
# -i 打开 STDIN,用于控制台交互
# -t 分配 tty 设备,该可以支持终端登录,默认为 false
# -name 指定容器名字,后续可以通过名字进行容器管理,links 特性需要使用名字
# –net=“hose” 容器使用主机网络
#  --security-opt label:disable 关闭容器的标签限制

Guess you like

Origin blog.csdn.net/qq_36488864/article/details/129746316