Jenkins自动部署随笔

1、Jenkins环境安装

下载:官网自行下载

环境:Java11

启动命令:java -jar jenkins.war
权限赋予:chmod +x auth-build-by-pom.sh
后台启动命令:
nohup /home/devops/jenkins/jdk-11.0.20/bin/java -jar /home/devops/jenkins/jenkins.war --httpPort=443 > jenkins.log 2>&1 &
gitlab项目:

2、项目打包发布

  1. 环境
    a、maven
    b、play 1.5.3
    c、java8以上
---------------play启动项目pom.xml增加插件-----------------
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version> 
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <descriptors>
            <descriptor>assembly.xml</descriptor>
        </descriptors>
    </configuration>
</plugin>


-----------------根目录增加assembly.xml文件-----------------------
<assembly>
    <id>build-assembly</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${basedir}/app</directory>
            <outputDirectory>/app</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${basedir}/components</directory>
            <outputDirectory>/components</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${basedir}/conf</directory>
            <outputDirectory>/conf</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${basedir}/lib</directory>
            <outputDirectory>/lib</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${basedir}/assets</directory>
            <outputDirectory>/assets</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${basedir}/modules</directory>
            <outputDirectory>/modules</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

2)Jenkins全局配置:
		Tools
			JDK installations 
			Git installations 
			Maven installations 

---------------------------------------------------------
    3)Jenkins构建项目配置参数:
        play启动应用:
        1、git
        2、触发构建条件:Poll SCM (每分钟执行检查一遍代码是否有变动,有则重构建)
        * * * * *
        3、构建环境:
        Add timestamps to the Console Output
        Terminate a build if it's stuck
        With Ant-java8
        4、构建脚本
        /home/joke/Documents/auth-build-by-pom.sh "/home/joke/Documents/outs" "dev"
        /home/devops/jenkins/shell/play-build.sh "/home/devops/run-project/dev/mall-platform" "dev"
        (shell路径 项目部署路径 conf配置环境)

    4)jar包应用:
        1、git
        2、触发构建条件:Poll SCM (每分钟执行检查一遍代码是否有变动,有则重构建)
        * * * * *
        3、构建环境:
        Add timestamps to the Console Output
        Terminate a build if it's stuck
        With Ant-java8
        4、构建脚本
        mvn clean install
        mvn clean deploy
        (发布到在线maven仓库,供所有引用过的play应用重构建应用)
        5、引用项目冲构建
        Post-build Actions - Build other projects
        所有引用到这个jar包的应该都要执行一遍重构建

play应用打包脚本1:

#!/bin/bash
# Author: joke
# Date: 2023-11-01 09:56:33
# Last Modified by: joke
# Description: 通过 maven 管理打包并发布项目
# Example: /home/joke/Documents/play-build.sh "/home/out" "dev"
# 重置 BUILD_ID 环境变量,防止Jenkins build结束后kill掉衍生进程
export BUILD_ID=anyWordIsOk
# 定义部署目录变量
destination_directory="$1"

if [ ! -d "$destination_directory" ]; then
  mkdir -p "$destination_directory"
fi

mvn clean install -DskipTests -Dactive.resource="$2"

mvn package -DskipTests

project_path=$(pwd)

cd "$project_path"/target

assembly_zip=$(find . -name "*assembly*.zip" -print -quit)

if [ -n "$assembly_zip" ]; then
  mv -f "$assembly_zip" "$destination_directory"

  # 进入部署目录,停止服务
  cd "$destination_directory"
  cd "$destination_directory"/"$(find . -type d -name "*SNAPSHOT*" -print -quit)"
  play stop

  # 进入部署目录,删除旧版本
  cd "$destination_directory"
  rm -rf "$(find . -type d -name "*SNAPSHOT*" -print -quit)"

  # 解压新版本
  unzip -o "$assembly_zip"

  # 进入新版本目录,启动服务
  cd "$destination_directory"/"$(find . -type d -name "*SNAPSHOT*" -print -quit)"

  nohup play start > ./run.out 2>&1 &

  max_wait_time=30
  current_time=0

  while [ "$current_time" -lt "$max_wait_time" ]; do
    if [[ $(cat ./logs/system.out) == *"Server is up and running"* ]] && [ "$current_time" -ge 5 ]; then
      cat ./logs/system.out
      echo "服务已成功启动"
      break 
    fi
    sleep 1
    current_time=$((current_time + 1))
  done

  if [ "$current_time" -ge "$max_wait_time" ] || ! [[ $(cat ./logs/system.out) == *"Server is up and running"* ]]; then
    echo "启动日志:./logs/system.out"
    cat ./logs/system.out
    echo "运行日志:./run.out"
    cat ./run.out
    echo "服务启动失败或等待超时"
    exit 1
  fi
else
  echo "未找到带 'assembly' 字符的 ZIP 文件"
  exit 1
fi

jar模块打包脚本2:

#!/bin/bash
# Author: joke
# Date: 2023-11-01 09:56:33
# Last Modified by: joke
# Description: jar项目通过 maven 管理更新并发布项目
# Example: /home/joke/Documents/jar-build.sh
mvn clean install -DskipTests
mvn clean deploy

以上为Jenkins构建的大概思路流程,仅仅满足个人在业务中所设计到的需求,如果需要更加自定义的需求,需要多看Jenkins的配置选项,以及多了解容器化部署的过程,毕竟容器化才是市场主流的方向,如果有需求的话,再把容器化部署的过程简单梳理一下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joker.our

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值