宝塔webhook自动部署前端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env bash

#加入所需要的 shell
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/www/server/nvm/versions/node/v12.18.3/bin:~/bin
export PATH

#项目路径
gitPath="/admin"
#git 远程仓库地址
gitSSH=""
#构建完成需要部署到的目录
buildPath="/your_target_direct"
#需要部署的分支
gitBranch="master"
#是否更新标签
isChanged=0


#创建代码仓库
checkGit() {
if [ -d "$gitPath" ];then
cd $gitPath
#判断是否存在git目录
if [ ! -d ".git" ]; then
echo "在该目录下克隆 git"
git clone -b $gitBranch $gitSSH gittemp
mv gittemp/* gittemp/.[^.]* .
rm -fr gittemp
isChanged=1
fi
else
echo "该项目路径不存在"
echo "End"
exit 1
fi
}

#拉取代码
pullCode() {
cd $gitPath
echo "正在拉取远程代码"
#判断是否在所部署分支
nowBranch=`git branch | grep "*" | awk '{print $2}'`
if [ $nowBranch != $gitBranch ];then
git checkout $gitBranch
fi

git fetch
res=`git rev-parse HEAD @{u} | uniq | wc -l`
if [ $res = 2 ] || [ $isChanged = 1 ]; then
git pull origin $gitBranch
echo "本地代码已更新"
else
echo "本地代码已是最新代码,无需更新。"
exit 1
fi
}

# if [ -f "$buildScript" ]; then
# chmod +x $buildScript
# ./$buildScript
# else
# echo '不存在执行脚本';
# fi
# echo "脚本执行结束"


buildReact() {
which yarn
if [ $? != 0 ];then
echo "yarn 不存在 "
exit 1
fi

cd $gitPath

echo "更新依赖中..."
a=`yarn install`
if [ $? = 0 ];then
echo "依赖更新完成"
else
echo "更新依赖失败"
echo $a
fi

echo "开始构建项目..."

build=`yarn build-production`
if [ $? = 0 ]; then
chown -R 751dpark:751dpark $gitPath/dist
echo "项目构建完成..."
else
echo "项目构建失败 "
echo $build
fi
}

#复制生成的文件到
cpFile() {
#TODO删除当前目录下的所有文件
if test -n "$buildPath";then
#
rm -fr $buildPath/*
cp -r -p $gitPath/dist/* $buildPath/
#ln -s $gitPath/dist/* $buildPath/
else
echo "需要部署的目录为空"
exit 1
fi
}

#
main() {
checkGit
pullCode
buildReact
cpFile
}

main 2>&1
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~