一键拉取代码打包部署springboot项目
1.环境部署
安装maven
# 下载maven
wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
# 解压
tar -zxf apache-maven-3.6.3-bin.tar.gz -C /usr/local
# 修改配置
vim /usr/local/apache-maven-3.6.3/conf/settings.xml
<!-- 在mirrors中配置一个阿里的mirror -->
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<!-- 配置阿里仓库 -->
<profiles>
<profile>
<id>ali</id>
<repositories>
<repository>
<id>nexus></id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- 激活阿里仓库配置 -->
<activeProfiles>
<activeProfile>ali</activeProfile>
</activeProfiles>
将maven 加入环境变量中
vim /etc/profile
#添加的内容 ,路径记得修改为自己解压的路径位置
export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=${MAVEN_HOME}/bin:$PATH
# 刷新系统环境变量生效
source /etc/profile
# 查看Maven版本
mvn -v
# 如果查看版本出现错误,没有错误的不需要执行
-bash: /usr/local/maven/bin/mvn: Permission denied
# 文件没有权限,需要设置文件权限
chmod a+x /usr/local/apache-maven-3.6.3/bin/mvn
安装Git
# 安装git,然后一直回车就行
yum install git
# 初始化ssh公钥
ssh-keygen -t rsa -C "邮箱地址"
# 查看公钥,复制放到git上配置
cd /root/.ssh
cat id_rsa.pub
编写shell脚本
vim package.sh
#!/bin/sh
#项目的git地址
git_path=""
#本地代码位置代码路径
code_path=""
#项目名称
projectName=""
# jar包地址
jar_path=""
# 当前日期
currentdate=$(date +%Y年%m月%d日)
env="master"
#如果项目文件夹不存在说明没有下载过改项目代码,需要检出该项目
if [ ! -d ${code_path}/${projectName} ];
then
echo ${projectName}"不存在,检出该项目"
cd $code_path
git clone ${git_path}
else
echo ${projectName} "项目存在,更新代码"
cd ${code_path}/${projectName}
git reset --hard
git pull
fi
#删除项目
echo "删除项目下target"
rm -rf "${code_path}/${projectName}/${projectName}/target"
#maven 再打项目包
echo "maven打包${projectName}"
cd ${code_path}/${projectName}
mvn install -Dmaven.test.skip=true -Denv=$env -U
#如果打包出错直接退出
if [ $? -ne 0 ]; then
exit 1
fi
#关闭jar服务
echo "关闭jar服务"
#查询jar包服务进程ID,并杀掉该进程
pidList=$(ps aux | grep $projectName | grep -v grep | awk '{print $2}')
for pid in $pidList
do
kill -9 $pid
echo 'kill pid '$pid
done
echo "删除${projectName}.jar和${projectName}.log文件"
cd ${code_path}
rm -rf "${projectName}.jar"
rm -rf "${projectName}.log"
echo "复制打包的jar包"
if [ ! -d ${jar_path} ];then
mkdir -p ${jar_path}
else
if [ ! -f ${jar_path}/${projectName}.jar ];then
echo "文件不存在"
else
mv ${jar_path}/${projectName}.jar ${jar_path}/${projectName}.jar.${currentdate}
fi
fi
cp -rf ${code_path}/${projectName}/target/${projectName}.jar ${jar_path}/
执行脚本文件
sh package.sh