shell项目上线代码

2022-11-03 19:50:28 浏览数 (1)

echo "新建文件夹/opt/app/code"

mkdir -p /opt/app/code

echo "进入/opt/app/code"

cd /opt/app/code

#将git用户名、密码、仓库地址替换成自己的

echo "git拉取代码"

git clone https://username:password@gitee.com/xxx/xxx.git

echo "进入项目文件夹"

cd xxx

#必须是不存在的分支

echo "切换到新的分支"

git checkout -b $1

echo "推送新的分支代码到远端仓库"

git push origin $1

echo "拉取远端仓库新的分支代码"

git pull origin $1

echo "将远端仓库分支与本地仓库分支关联"

git branch --set-upstream-to=origin/$1

echo "再次拉取远端仓库新的分支代码,验证分支是否关联上"

git pull origin $1

echo "pom.xml 替换"

find -name "pom.xml" | xargs perl -pi -e "s|demo|$1|g"

echo "application.yml 替换"

find /opt/app/code/xxx/src/main/resources/ -name "application.yml" | xargs perl -pi -e "s|8081|$3|g"

# find 路径 -name "文件名" | xargs perl -pi -e "s|被替换的字符串|替换后的字符串|g"

# ./ 相对位置

echo "application-druid.yml 替换"

find /opt/app/code/xxx/src/main/resources/ -name "application-druid.yml" | xargs perl -pi -e "s|localhost|$2|g"

find /opt/app/code/xxx/src/main/resources/ -name "application-druid.yml" | xargs perl -pi -e "s|demo|$1|g"

echo "maven打包"

mvn clean install

#cp前添加反斜线,可以直接覆盖同名文件,不会出现"cp: overwrite `xxx` ?"提示

echo "复制jar"

cp /opt/app/code/xxx/target/$1.jar /opt/app

echo "启动jar"

nohup java -jar /opt/app/$1.jar >/dev/null 2>&1 &

echo "将所有项目文件放入缓存中"

git add .

#将git电子邮箱、用户名替换成自己的

echo "提交代码到本地仓库"

git config user.email "xxx@qq.com"

git config user.name "username"

git commit -m "打包$4"

echo "拉取远端仓库代码"

git pull origin $1

echo "推送代码到远程仓库"

git push origin $1

echo "删除clone的代码"

rm -rf /opt/app/code/xxx

#xxx.cn 替换成自己的域名

#$指最后一行,i是指在读取文件此行前增加(include)记录,即倒数第二行添加内容

echo "添加nginx配置"

sed -i '$i server {' /usr/local/nginx/conf/nginx.conf

sed -i '$i listen 80;' /usr/local/nginx/conf/nginx.conf

sed -i '$i server_name '$1'.xxx.cn;' /usr/local/nginx/conf/nginx.conf

sed -i '$i location / {' /usr/local/nginx/conf/nginx.conf

sed -i '$i proxy_pass http://127.0.0.1:'$3'/;' /usr/local/nginx/conf/nginx.conf

sed -i '$i proxy_connect_timeout 600;' /usr/local/nginx/conf/nginx.conf

sed -i '$i proxy_read_timeout 600;' /usr/local/nginx/conf/nginx.conf

sed -i '$i }' /usr/local/nginx/conf/nginx.conf

0 人点赞