jenkins 编译 vue 项目、远程发布项目 shell 脚本

原创
2019/01/05 19:48
阅读数 335

第五版命令

#!/bin/bash
set -e

# start: config...
printf "\n\n"
echo "==================start: 项目信息========================="
# 版本
version="V5.0.1"
# shellscriptName 脚本名称
shellscriptName="buildPro.sh"
# 说明
description="VUE项目 通用部署 shellscript 脚本: ${shellscriptName}(${version})"
# 项目文件压缩包名 默认 dist.tar.gz
resourceName="dist.tar.gz"
# 部署脚本地址
deployProPath="/home/proconfig/common/${shellscriptName}"
# 需要远程上传的服务器
# serviceNameLst="root@192.168.2.150 root@172.30.34.210"
serviceNameLst=$1
# workSpacePath 工作区间地址
workSpacePath="${WORKSPACE}"
# workSpaceName 项目名
workSpaceName="${JOB_BASE_NAME}"
# 项目 git url 地址
getUrl="${GIT_URL}"
# GIT_BRANCH 当前所在分之
gitBranch="${GIT_BRANCH}"
# BUILD_TAG 编译标签
buildTag="${BUILD_TAG}"
# 项目部署地址
projectPath="/home/project/${workSpaceName}/${gitBranch}"
# 临时文件存放地址 从远程拉取的项目文件存放的地方
transferPath="/home/project/transfer/${workSpaceName}/${gitBranch}"
# 备份 项目文件存放地址
backPath="/home/back/${JOB_BASE_NAME}/${gitBranch}"
# 备份 项目文件压缩包名
filename="${backPath}/${JOB_NAME}_${BUILD_ID}.tar.gz"

# end: config...

nodeV=$(node -v)
npmV=$(npm -v)

echo """
        ########################################################
        # Name             : buildPro.sh
        # Version          : $version
        # Author           : Mad Dragon
        # email            : 395548460@qq.com
        # Create Date      : 2019-11-26
        # Function         : Auto publish script
        # Description      : $description
        # deployProPath    : $deployProPath
        ########################################################  
"""

echo """
        ########################################################
        # 变量配置:                                           
        #   项目    当前所在分之(gitBranch)         : $gitBranch
        #   项目    打包压缩包名(resourceName)      : $resourceName
        #   项目    部署服务器地址(serviceNameLst)  : $serviceNameLst
        #   项目    工作空间(workSpacePath)         : $workSpacePath
        #   项目    部署地址(projectPath)           : $projectPath
        #   项目    缓存地址(transferPath)          : $transferPath
        #   项目    备份文件地址(filename)           : $filename
        #   项目    git url 地址(getUrl)              : $getUrl
        #
        # 系统环境:
        #   node 版本       : $nodeV
        #   npm 版本        : $npmV
        ########################################################
"""

echo "==================end: 项目信息========================="

# master\dev 分支进行构建  其他分支过滤
if [[ "$gitBranch" == "origin/master" ]] || [[ "$gitBranch" == "origin/dev" ]] ; then

# 根据git分支 修改编译后文件件提交到的分支
if [[ "$gitBranch" == "origin/master" ]] ; then
    deploymentBranch="dist"
elif [[ "$gitBranch" == "origin/dev" ]] ; then
    deploymentBranch="dist-test"
else
    deploymentBranch="dist-test"
fi


# start: function...........
#--------------------start: 编译问题解决方案 -----------------------------------

# 解决 要输入密码的问题
# git config --global user.name "wsl"
# git config --global user.email "395548460@qq.com"
# git config --global credential.helper store

# 解决 缓存问题 "----清除缓存  依赖安装有问题需要重新安装依赖"
#npm cache clean --force
#npm audit fix
#npm audit

# npm 问题
#npm config set registry https://repo.i.sixi.com/repository/npm/
#npm cache clean --force
#npm update


#--------------------end: 编译问题解决方案 -----------------------------------
printf "\n\n"

echo "==================start: 项目编译========================="

    printf "\n"

    # 跟新依赖
    echo "      [info]: 1. 更新依赖 ..."
    printf "\n"
    npm install
    printf "\n"
    echo "      [info]: 成功 ..." 
    printf "\n"


    echo "      [info]: 2. build项目 ..."
    printf "\n"
    npm run build
    printf "\n"
    echo "      [info]: 成功 ..." 
    printf "\n"

echo "==================end: 项目编译========================="

printf "\n\n"

echo "==================start: 项目打包========================="

    echo "      [info]: 1. 打包项目 文件名:[ $resourceName ]..."
    cd dist
    tar -zcf $resourceName *
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 2. 创建目录    projectPath:[ $projectPath ] ..."
    mkdir -p $projectPath
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 3. 清理目录  [ $projectPath ] /* ..." 
    rm -rf "$projectPath/$resourceName"
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 4. 移动压缩包文件  [ $projectPath/$resourceName ] 到 [ $projectPath ] ..." 
    mv $resourceName $projectPath/
    echo "      [info]: 成功 ..." 
    printf "\n"

echo "==================end: 项目打包========================="

printf "\n\n"

echo "==================start: 同步镜像到 GIT 分支 [ $deploymentBranch ] ========================="

    echo "      [info]: 1. 创建目录    transferPath:[ $transferPath ] ..."
    mkdir -p $transferPath
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 2. 清理目录  [ $transferPath ] /* ..." 
    rm -rf $transferPath/*
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 3. 清理目录  [ $transferPath ] .[!.]* ..." 
    rm -rf $transferPath/.[!.]*
    echo "      [info]: 成功 ..." 
    printf "\n"
	
    echo "      [info]: 4. 克隆分支 [ $deploymentBranch ]  指令:[ git clone  -b $deploymentBranch --single-branch --depth 10 $GIT_URL $transferPath ] ..."
    git clone  -b $deploymentBranch --single-branch --depth 10 $getUrl $transferPath
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 5. 查看git变更状态 status ..." 
    git status
    echo "      [info]: 成功 ..." 
    printf "\n"
    
    echo "      [info]: 6. 清理目录  [ $transferPath ] /* ..." 
    rm -rf $transferPath/*
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 7. 移动压缩包文件  [ $projectPath/$resourceName ] 到 [ $transferPath ] ..." 
    mv "$projectPath/$resourceName" "$transferPath/"
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 8. 切换目录    [ $transferPath ] ..." 
    cd $transferPath/
    echo "      [info]: 成功 ..." 
    printf "\n"
    
    echo "      [info]: 9. 解压压缩包文件  [ $resourceName ] ..." 
    tar -zxf $resourceName
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 10. 备份压缩包文件  [ $resourceName ] 到 [ $projectPath/$resourceName  ] ..."
    mv "$transferPath/$resourceName" "$projectPath/$resourceName"
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 11. 添加build.log 文件 ..." 
    echo "$description 自动构建 - $deploymentBranch - $buildTag" >> build.log
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 12. git add --all ..." 
    git add --all
    echo "      [info]: 成功 ..." 
    printf "\n"
    
    echo "      [info]: 13. git status ..." 
	git status
    echo "      [info]: 成功 ..." 
    printf "\n"

    echo "      [info]: 14. git commit ..." 
    git commit -m "$description 自动构建 - $deploymentBranch - $buildTag "
    echo "      [info]: 成功 ..." 
    printf "\n"


    echo "      [info]: 15. git push origin $deploymentBranch ..." 
    git push origin $deploymentBranch
    echo "      [info]: 成功 ..." 
    printf "\n"

echo "==================end: 同步镜像到GIT $deploymentBranch 分支========================="

printf "\n\n"

echo "==================start:远程项目部署========================="

for serviceName in $serviceNameLst;do

    echo "=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* [ serviceName:   $serviceName ] *=*=*==*=*=*=*=*==*=*=*=*=*=*==*=*=*=*=*=*==*=*="

ssh -t $serviceName<<EOF

printf "\n"
echo "      [info]: 1. 创建目录    transferPath:[ $transferPath ] ..."
mkdir -p $transferPath
echo "      [info]: 成功 ..." 
printf "\n"

echo "      [info]: 2. 创建目录    projectPath:[ $projectPath ] ..."
mkdir -p $projectPath
echo "      [info]: 成功 ..." 
printf "\n"

echo "      [info]: 3. 创建目录    backPath:[ $backPath ] ..."
mkdir -p $backPath
echo "      [info]: 成功 ..." 
printf "\n"

EOF

    echo "      [info]: 4. 上传压缩包文件   [ $projectPath/$resourceName ] 到 [ $serviceName:$transferPath] ..."
    scp -r "$projectPath/$resourceName" "$serviceName:$transferPath"
    echo "      [info]: 成功 ..." 
    printf "\n"

ssh -t $serviceName<<EOF

echo "      [info]: 5. 清理目录  [ $projectPath ] ..." 
rm -rf $projectPath/* 
echo "      [info]: 成功 ..." 
printf "\n"

echo "      [info]: 6. 移动压缩包文件  [ $transferPath/$resourceName ] 到 [ $projectPath ] ..." 
mv "$transferPath/$resourceName" "$projectPath/"
echo "      [info]: 成功 ..." 
printf "\n"

echo "      [info]: 7. 切换目录    [ $projectPath ] ..." 
cd $projectPath/
echo "      [info]: 成功 ..." 
printf "\n"

echo "      [info]: 8. 解压压缩包文件  [ $resourceName ] ..." 
tar -zxf $resourceName
echo "      [info]: 成功 ..." 
printf "\n"

echo "      [info]: 9. 备份压缩包文件  [ $resourceName ] 到 [ $filename  ] ..."
mv "$projectPath/$resourceName" $filename
echo "      [info]: 成功 ..." 
printf "\n"

EOF

done

    printf "\n"
echo "==================end:远程项目部署========================="

else

    printf "\n"
    echo "      [debug]: ${gitBranch} 非构建分支 ..."

fi

printf "\n\n"
echo "撒花结束 *★,°*:.☆( ̄▽ ̄)/$:*.°★* 。"


# ----------------------------------------------


第四版命令

#!/bin/bash
set -e

# start: config...
echo "==================start: 项目信息========================="
# 说明
description="通用项目部署 shellscript 脚本"
# shellscriptName
shellscriptName="buildPro.sh"
# 项目 git url 地址
getUrl="${GIT_URL}"

# 需要远程上传的服务器
# serviceNameLst="root@192.168.2.150 root@172.30.34.210"
serviceNameLst=$1

echo "serviceNameLst $serviceNameLst"
# workSpacePath 工作区间地址
workSpacePath="${WORKSPACE}"
gitBranch="${GIT_BRANCH}"
# BUILD_TAG 编译标签
buildTag="${BUILD_TAG}"
# workSpaceName 项目名
# 一般为项目名: 驼峰写法 customer-center-app ==》customerCenterApp
workSpaceName="${JOB_BASE_NAME}"
deployProHome="home"
deployProConfig="proconfig"
deployProCommon="common"
deployProPath="/$deployProHome/$deployProConfig/$deployProCommon"
deployProName="deployPro.sh"
# 项目文件压缩包名 默认 dist.tar.gz
resourceName="dist.tar.gz"
# 项目部署地址
projectPath="/home/project/${workSpaceName}"
# 临时文件存放地址 从远程拉取的项目文件存放的地方
transferPath="/home/project/transfer/${workSpaceName}"
# 备份 项目文件压缩包名
filename="${JOB_NAME}_${BUILD_ID}.tar.gz"
# 备份 项目文件存放地址
backPath="/home/back/${JOB_BASE_NAME}"
# end: config...

nodeV=$(node -v)
npmV=$(npm -v)
echo '''
	########################################################
	# $Name             : buildPro.sh	          #                       
	# $Version          : v4.1.1               #                   
	# $Function         : release/on-line for jboss Script # 
	# $Author           : Mad Dragon					   #	
	# $Create Date      : 2019-07-16                       #
	# $email            : 395548460@qq.com                 #
	# $Description      : 通用VUE项目部署 shellscript 脚本 #
	########################################################
'''

echo """
	########################################################
	# 变量配置:                                           #
	#   gitBranch  : $gitBranch                            #
	#   getUrl  : $getUrl                                  #
	#   serviceNameLst  : $serviceNameLst                  #
	#   workSpacePath   : $workSpacePath                   #
	#   workSpaceName   : $workSpaceName                   #
	#   deployProHome   : $deployProHome                   #
	#   deployProConfig : $deployProConfig                 #
	#   deployProCommon : $deployProCommon                 #
	#   deployProPath   : $deployProPath                   #
	#   deployProName   : $deployProName                   #
	#   resourceName    : $resourceName                    #
	#   projectPath     : $projectPath                     #
	#   transferPath    : $transferPath                    #
	#   filename        : $filename                        #
	#   backPath        : $backPath                        #
	#                                                      #
	# 系统环境:                                           #
	#   node 版本       : $nodeV                           #
	#   npm 版本        : $npmV                            #
	########################################################
"""

if [[ "$gitBranch" == "origin/master" ]] || [[ "$gitBranch" == "origin/dev" ]] ; then

deploymentBranch="dist-test"
if [[ "$gitBranch" == "origin/master" ]] ; then
deploymentBranch="dist"
fi

# start: function...........
printf "\n\n"

# 解决 要输入密码的问题
# git config --global user.name "wsl"
# git config --global user.email "395548460@qq.com"
# git config --global credential.helper store

printf "\n\n"


echo "==================start: 项目编译========================="
printf "\n\n"
#echo "----清除缓存  依赖安装有问题需要重新安装依赖"
#npm cache clean --force
#npm audit fix
#npm audit

echo "001、更新依赖 install         ====》"

npm install

#printf "\n\n"
echo "002、编译 build               ====》"
npm run build

#printf "\n\n"
echo "003、压缩文件 $resourceName   ====》"
cd dist
tar -zcf $resourceName *

#printf "\n\n"
mkdir -p $projectPath
echo "004、创建 $projectPath        ====》"

#printf "\n\n"
rm -rf "$projectPath/$resourceName"
mv $resourceName $projectPath/
echo "005、移动文件 $projectPath/$resourceName         ====》"

printf "\n\n"
#echo "006、目录结构                 ====》"
#ls $projectPath
echo "==================start: 同步镜像到GIT $deploymentBranch 分支 ========================="
    printf "\n\n"
    mkdir -p $transferPath
    echo "004、创建 $transferPath        ====》"

    #ls $transferPath -a

    #printf "\n\n"
    rm -rf $transferPath/*

    #ls $transferPath -a

    rm -rf $transferPath/.[!.]*

    #ls $transferPath -a
	
    echo "克隆 项目 $deploymentBranch 文件夹 git clone  -b $deploymentBranch --single-branch --depth 3 $GIT_URL $transferPath"
    git clone  -b $deploymentBranch --single-branch --depth 3 $getUrl $transferPath
    git status

    rm -rf $transferPath/*

    #printf "\n\n"
    mv "$projectPath/$resourceName" "$transferPath/"
    echo "      [debug]:移动 压缩包文件 $projectPath/$resourceName 到 $transferPath 成功" 

    #printf "\n\n"
    cd $transferPath/
    #pwd

    tar -zxf $resourceName
    echo "      [debug]:将 $transferPath/$resourceName 解压 $transferPath 成功" 

    mv "$transferPath/$resourceName" "$projectPath/$resourceName"

    #printf "\n\n"
    echo 'git push 操作'
    echo "$buildTag $description $shellscriptName 自动上线-$deploymentBranch" >> build.log

    git add --all
	git status
    git commit -m "$buildTag $description $shellscriptName 自动上线-$deploymentBranch"
    git push origin $deploymentBranch
    echo '*/*/**/'

	#printf "\n\n"
echo "==================end: 同步镜像到GIT $deploymentBranch 分支========================="
printf "\n\n"
echo "==================start: 远程上传文件========================="
#echo "007、start:远程上传文件...                        ====》"
printf "\n\n"

for serviceName in $serviceNameLst
do
	echo "=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*==*=*=*==*=*=*=*=*==*=*=*=*=*=*==*=*=*=*=*=*==*=*="
    echo "serviceName: $serviceName "
ssh -t $serviceName<<EOF
#printf "\n\n"
#echo "[debug]:查看ip ====>"
#ip add

printf "\n\n"
mkdir -p $transferPath
echo "      [debug]:创建 transferPath:$transferPath"

mkdir -p $deployProPath
echo "      [debug]:创建 deployProPath:$deployProPath"

mkdir -p $projectPath
echo "      [debug]:创建 projectPath:$projectPath"

mkdir -p $backPath
echo "      [debug]:创建 backPath:$backPath"
printf "\n\n"
EOF
    
    scp -r "$projectPath/$resourceName" "$serviceName:$transferPath"
    echo "      [debug]:上传 $projectPath/$resourceName 到 $serviceName:$transferPath  成功!"
done
echo "==================end: 远程上传文件========================="

printf "\n\n"
echo "==================start:远程项目部署========================="
#echo "008、start:远程项目部署...                        ====》"

for serviceName in $serviceNameLst
do
echo "=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*==*=*=*==*=*=*=*=*==*=*=*=*=*=*==*=*=*=*=*=*==*=*="
    echo "serviceName: $serviceName "
ssh -t $serviceName<<EOF
#printf "\n\n"
#echo "[debug]:查看ip ====>"
#ip add

printf "\n\n"
rm -rf $projectPath/* 
echo "      [debug]:删除 $projectPath/* 旧文件 成功" 

#ls $projectPath

mv "$transferPath/$resourceName" "$projectPath/"
echo "      [debug]:移动 压缩包文件 $transferPath/$resourceName 到 $projectPath 成功" 

cd $projectPath/
#pwd

tar -zxf $resourceName
echo "      [debug]:将 $projectPath/$resourceName 解压 $projectPath 成功" 

mv "$projectPath/$resourceName" $backPath/$filename
echo "      [debug]:将文件 $projectPath/$resourceName 备份到 $backPath/$filename 成功"
printf "\n\n"
EOF

done

printf "\n\n"
echo "==================end:远程项目部署========================="
fi


printf "\n\n"
echo "撒花结束 *★,°*:.☆( ̄▽ ̄)/$:*.°★* 。"


# ----------------------------------------------



第三版命令(升级)

#!/bin/bash
echo '''
#######################################################
# $Name             : buildPro.sh	                                
# $Version          : v3.0                                 
# $Function         : release/on-line for jboss Script 
# $Author           : Mad Dragon
# $Create Date      : 2019-1-5 16:09:42
# $email            : 395548460@qq.com
# $Description      : 通用项目部署 shellscript 脚本
#######################################################
'''

set -e

# start: config...
echo "==================start: 项目信息========================="
# 需要远程上传的服务器
# serviceNameLst="root@192.168.2.150 root@172.30.34.210"
serviceNameLst=$1
echo "serviceNameLst $serviceNameLst"
# workSpacePath 工作区间地址
workSpacePath="${WORKSPACE}"
# workSpaceName 项目名
# 一般为项目名: 驼峰写法 customer-center-app ==》customerCenterApp
workSpaceName="${JOB_BASE_NAME}"
deployProHome="home"
deployProConfig="proconfig"
deployProCommon="common"
deployProPath="/$deployProHome/$deployProConfig/$deployProCommon"
deployProName="deployPro.sh"
# 项目文件压缩包名 默认 dist.tar.gz
resourceName="dist.tar.gz"
# 项目部署地址
projectPath="/home/project/${workSpaceName}"
# 临时文件存放地址 从远程拉取的项目文件存放的地方
transferPath="/home/project/transfer/${workSpaceName}"
# 备份 项目文件压缩包名
filename="${JOB_NAME}_${BUILD_ID}.tar.gz"
# 备份 项目文件存放地址
backPath="/home/back/${JOB_BASE_NAME}"
# end: config...

nodeV=$(node -v)
npmV=$(npm -v)

echo """
#######################################################
变量配置:

#   serviceNameLst  : $serviceNameLst
#   workSpacePath   : $workSpacePath
#   workSpaceName   : $workSpaceName
#   deployProHome   : $deployProHome
#   deployProConfig : $deployProConfig
#   deployProCommon : $deployProCommon
#   deployProPath   : $deployProPath
#   deployProName   : $deployProName
#   resourceName    : $resourceName
#   projectPath     : $projectPath
#   transferPath    : $transferPath
#   filename        : $filename
#   backPath        : $backPath
    
系统环境:

#   node 版本       : $nodeV
#   npm 版本        : $npmV
#######################################################
"""

# start: function...........
printf "\n\n"
printf "\n\n"
echo "==================start: 项目编译========================="
printf "\n\n"
echo "001、更新依赖 install         ====》"
npm install

printf "\n\n"
echo "002、编译 build               ====》"
npm run build

printf "\n\n"
echo "003、压缩文件 $resourceName   ====》"
cd dist
tar -zcf $resourceName *

printf "\n\n"
mkdir -p $projectPath
echo "004、创建 $projectPath        ====》"

printf "\n\n"
rm -rf $projectPath/$resourceName
mv $resourceName $projectPath/
echo "005、移动文件 $projectPath/$resourceName         ====》"

printf "\n\n"
echo "006、目录结构                 ====》"
ls $projectPath

printf "\n\n"
echo "==================start: 远程上传文件========================="
echo "007、start:远程上传文件...                        ====》"
printf "\n\n"
for serviceName in $serviceNameLst
do
    echo "serviceName: $serviceName "
ssh -t $serviceName<<EOF
echo "=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*==*=*=*==*=*=*=*=*==*=*=*=*=*=*==*=*=*=*=*=*==*=*="
printf "\n\n"
echo "[debug]:查看ip ====>"
ip add

printf "\n\n"
mkdir -p $transferPath
echo "      [debug]:创建 transferPath:$transferPath"

mkdir -p $deployProPath
echo "      [debug]:创建 deployProPath:$deployProPath"

mkdir -p $projectPath
echo "      [debug]:创建 projectPath:$projectPath"

mkdir -p $backPath
echo "      [debug]:创建 backPath:$backPath"
printf "\n\n"
EOF
    
    scp -r "$projectPath/$resourceName" "$serviceName:$transferPath"
    echo "          [debug]:上传 $projectPath/$resourceName 到 $serviceName:$transferPath  成功!"
    printf "\n\n"

done

printf "\n\n"
echo "end:远程上传文件..."

printf "\n\n"
echo "撒花结束 *★,°*:.☆( ̄▽ ̄)/$:*.°★* 。"

printf "\n\n"
echo "==================start:项目部署========================="

for serviceName in $serviceNameLst
do
    ssh -t $serviceName<<EOF
echo "=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*==*=*=*==*=*=*=*=*==*=*=*=*=*=*==*=*=*=*=*=*==*=*="
printf "\n\n"
echo "[debug]:查看ip ====>"
ip add

printf "\n\n"
rm -rf $projectPath/* 
echo "      [debug]:删除 $projectPath/* 旧文件 成功" 

printf "\n\n"
ls $projectPath

printf "\n\n"
mv "$transferPath/$resourceName" "$projectPath/"
echo "      [debug]:移动 压缩包文件 $transferPath/$resourceName 到 $projectPath 成功" 

printf "\n\n"
cd $projectPath/
pwd

printf "\n\n"

tar -zxf $resourceName
echo "      [debug]:将 $projectPath/$resourceName 解压 $projectPath 成功" 

printf "\n\n"

mv "$projectPath/$resourceName" $backPath/$filename
echo "      [debug]:将文件 $projectPath/$resourceName 备份到 $backPath/$filename 成功"
printf "\n\n"
EOF

done

# ----------------------------------------------


第二版命令(升级)

1、本机shell操作

#!/bin/bash
set -e

# author:Mad Dragon
# email:395548460@qq.com
# 通用项目部署 shellscript 脚本
echo "==================start build project========================="
# start: config...

# 需要远程上传的服务器
serviceNameLst="root@192.168.2.150 root@172.30.34.210"
echo "serviceNameLst $serviceNameLst"

# workSpacePath 工作区间地址
workSpacePath="${WORKSPACE}"

# workSpaceName 项目名
# 一般为项目名: 驼峰写法 customer-center-app ==》customerCenterApp
workSpaceName="${JOB_BASE_NAME}"
echo "workSpaceName $workSpaceName"

deployProHome="home"
echo "deployProHome $deployProHome"

deployProConfig="proconfig"
echo "deployProConfig $deployProConfig"

deployProCommon="common"
echo "deployProCommon $deployProCommon"

deployProPath="/$deployProHome/$deployProConfig/$deployProCommon"
echo "deployProPath $deployProPath"

deployProName="deployPro.sh"
echo "deployProName $deployProName"

# 项目文件压缩包名 默认 dist.tar.gz
resourceName="dist.tar.gz"
echo "resourceName $resourceName"

# 项目部署地址
projectPath="/home/project/${workSpaceName}"
echo "projectPath $projectPath"

# 临时文件存放地址 从远程拉取的项目文件存放的地方
transferPath="/home/project/transfer/${workSpaceName}"
echo "transferPath $transferPath"

# 备份 项目文件压缩包名
filename="${JOB_NAME}_${BUILD_ID}.tar.gz"
echo "filename $filename" 

# 备份 项目文件存放地址
backPath="/home/back/${JOB_BASE_NAME}"
echo "backPath $backPath" 

# end: config...


# start: function...........
echo "proPath: $projectPath"

echo "node 版本:"
node -v

echo "npm 版本:"
npm -v

printf "\n\n"
echo "更新依赖 install"
npm install

printf "\n\n"
echo "编译 build"
npm run build

printf "\n\n"
echo "压缩文件 $resourceName"
cd dist
tar -zcf $resourceName *

printf "\n\n"
mkdir -p $projectPath
echo "创建 $projectPath"

printf "\n\n"
rm -rf $projectPath/$resourceName
mv $resourceName $projectPath/
echo "移动文件 $projectPath/$resourceName"

printf "\n\n"
echo "目录结构"
ls $projectPath

printf "\n\n"
echo "start:远程上传文件..."

printf "\n\n"
for serviceName in $serviceNameLst
do
    echo "serviceName: $serviceName "
ssh -t $serviceName<<EOF
printf "\n\n"
echo "[debug]:查看ip"
ip add

printf "\n\n"
mkdir -p $transferPath
echo "[debug]:创建 transferPath:$transferPath"

mkdir -p $deployProPath
echo "[debug]:创建 deployProPath:$deployProPath"

mkdir -p $projectPath
echo "[debug]:创建 projectPath:$projectPath"

mkdir -p $backPath
echo "[debug]:创建 backPath:$backPath"
printf "\n\n"
EOF
    
    scp -r "$projectPath/$resourceName" "$serviceName:$transferPath"
    echo "[debug]:上传 $projectPath/$resourceName 到 $serviceName:$transferPath  成功!"
    printf "\n\n"

done

printf "\n\n"
echo "end:远程上传文件..."

printf "\n\n"
echo "撒花结束 *★,°*:.☆( ̄▽ ̄)/$:*.°★* 。"

printf "\n\n"
echo "==================deploy build success========================="

for serviceName in $serviceNameLst
do
    ssh -t $serviceName<<EOF
printf "\n\n"
echo "[debug]:查看ip"
ip add

printf "\n\n"
rm -rf $projectPath/* 
echo "[debug]:删除 $projectPath/* 旧文件 成功" 

printf "\n\n"
ls $projectPath

printf "\n\n"
mv "$transferPath/$resourceName" "$projectPath/"
echo "[debug]:移动 压缩包文件 $transferPath/$resourceName 到 $projectPath 成功" 

printf "\n\n"
cd $projectPath/
pwd

printf "\n\n"

tar -zxvf $resourceName
echo "[debug]:将 $projectPath/$resourceName 解压 $projectPath 成功" 

printf "\n\n"

mv "$projectPath/$resourceName" $backPath/$filename
echo "[debug]:将文件 $projectPath/$resourceName 备份到 $backPath/$filename 成功"
printf "\n\n"
EOF

done

# ----------------------------------------------


第一版命令

1、本机shell 操作

o "==================start build project========================="

# start: config...

# 需要远程上传的服务器
serviceName1="root@192.168.2.150"

# workSpaceName 工作区间名   
# 一般为项目名: 驼峰写法 customer-center-app ==》customerCenterApp
workSpaceName="${JOB_BASE_NAME}"

# 项目文件压缩包名 默认 dist.tar.gz
resourceName="dist.tar.gz"

# 项目部署地址
projectPath="/home/project/${workSpaceName}"

# 临时文件存放地址 从远程拉取的项目文件存放的地方
transferPath="/home/project/transfer/${workSpaceName}"

# end: config...

echo 'proPath:'
echo $projectPath

echo 'node 版本:'
node -v

echo 'npm 版本:'
npm -v

echo '更新依赖 install'
npm install

echo '编译 build'
npm run build

echo '压缩文件'
cd dist
tar -zcf $resourceName *

echo '移动文件 $projectPath/$resourceName'

echo '判断文件是否存在'
if test -e $projectPath
then
    echo '文件已存在! ${projectPath}'
else
    echo '文件不存在! ${projectPath}'
    mkdir $projectPath
fi

rm -rf $projectPath/$resourceName
mv $resourceName $projectPath/


echo '目录结构'
ls $projectPath

echo 'start:远程上传文件...'

echo '判断文件是否存在'
if ssh $serviceName1 test -e $transferPath
then

    echo '文件已存在! ${serviceName1}:${transferPath}'
else
    echo '文件不存在! ${serviceName1}:${transferPath}'
    ssh -t $serviceName1 mkdir $transferPath/
fi

scp -r $projectPath/$resourceName $serviceName1:$transferPath/
echo 'end:远程上传文件...'


echo '撒花结束 *★,°*:.☆( ̄▽ ̄)/$:*.°★* 。'
echo "==================deploy build success========================="

# ----------------------------------------------



2、免密登录远程服务器后操作

#!/bin/bash
echo "==================start deploy project========================="
echo '项目运行服务器。。。。'

# start: config...

# workSpaceName 工作区间名   
# 一般为项目名: 驼峰写法 customer-center-app ==》customerCenterApp
workSpaceName="${JOB_BASE_NAME}"

# 项目文件压缩包名 默认 dist.tar.gz
resourceName="dist.tar.gz"

# 远程项目存放地址
originPath="root@172.30.34.188:/home/project/${workSpaceName}"

# 临时文件存放地址 从远程拉取的项目文件存放的地方
transferPath="/home/project/transfer/${workSpaceName}"

# 项目部署地址
projectPath="/home/project/${JOB_BASE_NAME}"

# 备份 项目文件压缩包名
filename="${JOB_NAME}_${BUILD_ID}.tar.gz"

# 备份 项目文件存放地址
backPath="/home/back/${JOB_BASE_NAME}"

# end: config...

echo '查看ip'
ip add 

echo '进入项目目录 ${transferPath}' 

echo '判断文件是否存在'
if test -e $transferPath
then

    echo '文件已存在! ${transferPath}'
else
    echo '文件不存在! ${transferPath}'
    mkdir $transferPath
fi

cd $transferPath/ 

echo '远程下载文件开始 ${originPath}/${resourceName}'
scp -r $originPath/$resourceName $transferPath

echo '删除 ${JOB_BASE_NAME}旧文件' 

echo '判断文件是否存在'
if test -e $projectPath
then

    echo '文件已存在! ${projectPath}'
else
    echo '文件不存在! ${projectPath}'
    mkdir $projectPath
fi

rm -rf $projectPath/* 

echo '移动 压缩包文件 到 ${projectPath}' 
mv dist.tar.gz $projectPath/

echo '进入 ${JOB_BASE_NAME} 目录' 
cd $projectPath/

echo '解压 dist.tar.gz' 
tar -zxf $resourceName

echo '将文件备份到 ${backPath}/${filename}'

echo '判断文件是否存在'
if test -e $backPath
then

    echo '文件已存在! ${backPath}'
else
    echo '文件不存在! ${backPath}'
    mkdir $backPath
fi

mv dist.tar.gz $backPath/$filename

echo "==================deploy project success========================="
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部