jenkins 常用shell

1、发布前端shell

ip="192.168.31.33"
port=22
# 前端打包后的包名
dist_name="dist"
# app的名字,app下有index.html
app_name="dist"
# app所在的目录
app_dir="/data/project/html/nmg-ibuilds-pc-visitor"
#备份个数
bak_count=3
# 备份目录
bak_dir="/data/project/html/nmg-ibuilds-pc-visitor"

npm install  --registry=https://registry.npmmirror.com
npm run build:prod

ssh -p $port $ip "if  [ ! -d $app_dir ]; then mkdir -p $app_dir; fi"
ssh -p $port $ip "if  [ ! -d $bak_dir ]; then mkdir -p $bak_dir; fi"
ssh -p $port $ip "if  [ -e $app_dir/$app_name ]; then mv $app_dir/$app_name ${bak_dir}/${app_name}-$(date +'%Y-%m-%d_%H:%M:%S'); fi"

scp -P $port -rp $dist_name $ip:$app_dir
if [ "$app_name" != "$dist_name" ]
then
    ssh -p $port $ip "mv $app_dir/$dist_name $app_dir/$app_name"
fi

# 删除多余的备份 ,tail -n +3表示第三行之后包含第三行
ssh -p $port $ip "cd $bak_dir && ls -lt | grep ${app_name}- | awk '{print \$NF}' | tail -n +${bak_count} |  tail +2  | xargs -I {} rm -rf  {}"

2、jar包跑本地supervisor

ip="172.22.215"
port=22
target_dir="./target"
jar_name="cscec1b-decoration.jar"
#supervisor中运行jar包的程序名
supervisor_name=${jar_name%.jar}
# jar包所在的目录
jar_dir="/data/project/rhdy/"
#备份个数
bak_count=3
# 备份目录
bak_dir="/data/project/rhdy/bak/"

mvn clean package  -Dmaven.test.skip=true  -P rw_dev -s /data/rw_dev/settings.xml

ssh -p $port $ip "if  [ ! -d $jar_dir ]; then mkdir -p $jar_dir; fi"
ssh -p $port $ip "if  [ ! -d $bak_dir ]; then mkdir -p $bak_dir; fi"
ssh -p $port $ip "if  [ -e $jar_dir/$jar_name ]; then mv $jar_dir/$jar_name $bak_dir/$jar_name-$(date +'%Y-%m-%d_%H:%M:%S'); fi"

scp -P $port -rp ${target_dir}/${jar_name} $ip:$jar_dir
ssh -p $port $ip "supervisorctl restart $supervisor_name"

# 删除多余的备份 ,tail -n +3表示第三行之后包含第三行
ssh -p $port $ip "cd $bak_dir && ls -lt | grep ${jar_name}- | awk '{print \$NF}' | tail -n +${bak_count} |  tail +2  | xargs -I {} rm -rf  {}"

3、jar包部署到k8s

Dockerfile

FROM openjdk:17
#容器中创建目录
RUN mkdir -p /app
#编译后的jar包copy到容器中创建到目录内
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
COPY ibuilds-consumer-adm-iot.jar /app
WORKDIR /app
#指定容器启动时要执行的命令
CMD java -jar   -Xms256m -Xmx512m  /app/ibuilds-consumer-adm-iot.jar

jenkins上shell

jar_name='ibuilds-consumer-adm-operate.jar'
host='192.168.31.10'
port='22'
service_name=${jar_name%.jar}
# 生成的dockerfile文件在service_dir
service_dir="/data/nmg-test/"${service_name}
target_dir="./ibuilds-consumer-adm-operate/target"
k8s_namespace=nmg-test
harbor_repository="192.168.31.15/nmg-test"
Dockerfile_model="/data/nmg-test/Dockerfile"
Dockerfile_modelService_name="ibuilds-consumer-adm-iot"

mvn clean package -Dmaven.test.skip=true -P nmg-test -s /data/nmg-test/settings.xml
mkdir -p $service_dir
cp $Dockerfile_model ${service_dir}/Dockerfile
sed -i s/${Dockerfile_modelService_name}/${service_name}/g ${service_dir}/Dockerfile
cp ${target_dir}/${jar_name} ${service_dir}
docker build -t  ${harbor_repository}/${service_name}:latest -f ${service_dir}/Dockerfile ${service_dir}

docker push ${harbor_repository}/${service_name}:latest
ssh -p $port $host "kubectl rollout restart deployment/${service_name} -n ${k8s_namespace}"

 

posted @ 2024-10-30 10:11  苦逼yw  阅读(7)  评论(0编辑  收藏  举报