uni-app脚本及自动化构建部署
# 脚本打包
# win中使用
# 安装jq,zip命令
choco install jq
1
安装zip
只要加入这两个命令在Windows命令行下也就可以使用;下载地址 (opens new window)
直接将下载的zip.exe与unzip.exe保存在目标路径(自定义目录),然后将路径添加在环境变量path属性下。 WIN + R输入cmd 命令盘符中输入 zip ,如下所示,就说明环境配置成功了!
- 压缩命令:zip -q -r xxx.zip yyy (将yyy文件压缩成xxx.zip文件),
zip -rq xxx.zip yyy
- 解压缩命令:unzip xxx.zip (将xxx.zip解压缩)
# HbuildX cli设置全局
# 使用脚本
- cli项目中h5的build目录不一样了;
- 与mac脚本的不同之处,就是在win中mv移动目录时会有权限问题,就用cp 和rm命令替换;
#!/bin/bash
###
# @author: samy
# @email: yessz#foxmail.com
# @time: 2023-05-04 09:14:30
# @modAuthor: samy
# @modTime: 2023-05-16 08:44:41
# @desc: 确保hbuildx打开,而且还登录了
# Copyright © 2020~2023 DAM FE
###
if [ $# -eq 0 ]; then # 脚本必须传入参数。1是h5 ,2是app
echo "Please invoke this script with one or more command-line arguments."
exit $E_NO_ARGS
fi
echo "======构建类型==={$1}===="
# curDir=$WORKSPACE
curDir=$(
cd "$(dirname "$0")"
pwd
)
buildType=$1
datetime=$(date "+%Y%m%d%H%M%S")
proName=$(cat ./src/manifest.json | jq -r .proName) #proName跟HBuildx的项目名称一致
appid=$(cat ./src/manifest.json | jq -r .appid)
versionName=`cat ./src/manifest.json | jq -r .versionName`
# versionName=$(cat ./src/manifest.json | jq -r .versionName)
echo "---读取manifest.json中相关配置---"
echo "proName=$proName appid=$appid versionName=$versionName"
pkgNameApp=${appid}_${versionName}_${datetime}.wgt
pkgNameH5=h5_${versionName}_${datetime}
appBuildDir=$curDir/unpackage/release
# h5BuildDir=$curDir/unpackage/dist/build
h5BuildDir=$curDir/dist/build
mkdir -p $curDir/unpackage/realseDist
releaseDir=$curDir/unpackage/realseDist
replacePro() {
varKey=$2
varValue=$3
echo $varKey $varValue
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i "" "s/\(${varKey} =\).*/\1 ${varValue};/" $1 #注意等号前后有空格
else
sed -i "s/\(${varKey} =\).*/\1 ${varValue};/" $1
fi
}
echo "=====环境依赖初始化==node版本===源配置==isDev==="
# node -v
# npm config set registry https://registry.npm.taobao.org
replacePro ${curDir}/src/config.js isDev false
replacePro ${curDir}/config.js isDev false
echo "=====build==中====="
rm -rf $h5BuildDir
#h5类型构建,不能指定构建名称和路径,生成h5目录后,要脚本辅助处理
if [ $buildType = 1 ]; then
pkgName=$pkgNameH5
echo $pkgName
cli publish --platform h5 --project $proName --name $pkgName
# mv $h5BuildDir/h5 $h5BuildDir/$pkgName
cp -rf $h5BuildDir/h5 $h5BuildDir/$pkgName
cd $h5BuildDir
zip -rqm $pkgName.zip $pkgName
rm -rf $pkgName
mv $h5BuildDir/$pkgName.zip $releaseDir/
#app类型构建
else
pkgName=$pkgNameApp
echo $pkgName
cli publish --platform APP --project $proName --name ${pkgName} --type wgt
mv $appBuildDir/$pkgName $releaseDir/
fi
cd $releaseDir
if [[ "$OSTYPE" == "darwin"* ]]; then
open .
else
start .
fi
http-server .
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
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
# 优化不使用HBuildx的cli
直接命令行操作
# 打h5的zip包
rm -rf $appBuildDir/h5
pkgName=$pkgNameH5
echo $pkgName
if [ $isUseHBuildX == true ]; then
cli publish --platform h5 --project $proName --name $pkgName
else
yarn proH5Yss #TODO:先写死
fi
printVersion $h5BuildDir/h5
if [[ "$OSTYPE" == "darwin"* ]]; then
mv $h5BuildDir/h5 $h5BuildDir/$pkgName
else
cp -rf $h5BuildDir/h5 $h5BuildDir/$pkgName
fi
cd $h5BuildDir
zip -rqm $pkgName.zip $pkgName
rm -rf $pkgName
mv $h5BuildDir/$pkgName.zip $distRelease/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 打app的apk及wgt包
rm -rf $appBuildDir/app-plus
pkgName=$pkgNameApp
echo $pkgName
if [ $isHBuildApk == true ]; then
cd $appBuildDir/$pkgName
cli pack --config D:/samy/company/sk/data/pkg-build-info/pkg-apk-config.json
else
if [ $isUseHBuildX == true ]; then
cli publish --platform APP --project $proName --name $pkgName.wgt --type wgt
mv $appBuildDir/$pkgName.wgt $distRelease/
else
yarn proAppYss #TODO:先写死
printVersion $appBuildDir/app-plus
if [[ "$OSTYPE" == "darwin"* ]]; then
mv $appBuildDir/app-plus $appBuildDir/$pkgName #mv在win上拷贝失效
else
cp -rf $appBuildDir/app-plus $appBuildDir/$pkgName
fi
cd $appBuildDir/$pkgName
zip -rqm $pkgName.zip *
mv $appBuildDir/$pkgName/$pkgName.zip $distRelease/$pkgName.wgt
cd ..
rm -rf $appBuildDir/$pkgName
fi
fi
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 全量包方式
# 目前这个启动后,会自动关闭,官方win还是有问题;
# 处理脚本部分
rm -rf $appBuildDir/app-plus
pkgName=$pkgNameApp
echo $pkgName
if [ $isHBuildApk == true ]; then
yarn proApp
mkdir -p $appBuildDir/app-plus/unpackage/res/
cp -rf $curDir/src/static/icons $appBuildDir/app-plus/unpackage/res/
cd $appBuildDir/app-plus
cli pack --config D:/samy/company/sk/data/pkg-build-info/pkg-apk-config.json
else
if [ $isUseHBuildX == true ]; then
cli publish --platform APP --project $proName --name $pkgName.wgt --type wgt
mv $appBuildDir/$pkgName.wgt $distRelease/
else
yarn proApp
printVersion $appBuildDir/app-plus
if [[ "$OSTYPE" == "darwin"* ]]; then
mv $appBuildDir/app-plus $appBuildDir/$pkgName #mv在win上拷贝失效
else
cp -rf $appBuildDir/app-plus $appBuildDir/$pkgName
fi
cd $appBuildDir/$pkgName
zip -rqm $pkgName.zip *
mv $appBuildDir/$pkgName/$pkgName.zip $distRelease/$pkgName.wgt
cd ..
rm -rf $appBuildDir/$pkgName
fi
fi
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
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
pkg-apk-config.json
{
"project": "D:/samy/company/sk/code/supervise-app/dist/build/app-plus",
"platform":"android",
"iscustom": false,
"safemode": false,
"android": {
"packagename":"UNI.xxx",
"androidpacktype":"0",
"certalias":"xxx",
"certfile":"D:/samy/company/sk/data/pkg-build-info/ky__uni__xxx.keystore",
"certpassword":"xxx",
"channels":""
},
"isconfusion":false,
"splashads":false,
"rpads":false,
"pushads":false,
"exchange":false
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# icon问题处理
# 要脚本联动预处理
src\manifest.json
"icons" : {
"android" : {
"hdpi" : "unpackage/res/icons/72x72.png",
"xhdpi" : "unpackage/res/icons/96x96.png",
"xxhdpi" : "unpackage/res/icons/144x144.png",
"xxxhdpi" : "unpackage/res/icons/192x192.png"
},
"ios" : {
"appstore" : "unpackage/res/icons/1024x1024.png",
"ipad" : {
"app" : "unpackage/res/icons/76x76.png",
"app@2x" : "unpackage/res/icons/152x152.png",
"notification" : "unpackage/res/icons/20x20.png",
"notification@2x" : "unpackage/res/icons/40x40.png",
"proapp@2x" : "unpackage/res/icons/167x167.png",
"settings" : "unpackage/res/icons/29x29.png",
"settings@2x" : "unpackage/res/icons/58x58.png",
"spotlight" : "unpackage/res/icons/40x40.png",
"spotlight@2x" : "unpackage/res/icons/80x80.png"
},
"iphone" : {
"app@2x" : "unpackage/res/icons/120x120.png",
"app@3x" : "unpackage/res/icons/180x180.png",
"notification@2x" : "unpackage/res/icons/40x40.png",
"notification@3x" : "unpackage/res/icons/60x60.png",
"settings@2x" : "unpackage/res/icons/58x58.png",
"settings@3x" : "unpackage/res/icons/87x87.png",
"spotlight@2x" : "unpackage/res/icons/80x80.png",
"spotlight@3x" : "unpackage/res/icons/120x120.png"
}
}
}
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
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
build.sh
cp -rf $curDir/src/static/icons $appBuildDir/app-plus/unpackage/res/
cd $appBuildDir/app-plus
1
2
2
# mac中使用
# 安装jq,zip命令
# 使用脚本
sh build.sh 1
#!/bin/bash
###
# @author: samy
# @email: yessz#foxmail.com
# @time: 2023-05-04 09:14:30
# @modAuthor: samy
# @modTime: 2023-05-08 19:10:57
# @desc: 确保hbuildx打开,而且还登录了
# Copyright © 2020~2023 DAM FE
###
if [ $# -eq 0 ]; then # 脚本必须传入参数。1是h5 ,2是app
echo "Please invoke this script with one or more command-line arguments."
exit $E_NO_ARGS
fi
echo "======构建类型==={$1}===="
# curDir=$WORKSPACE
curDir=$(
cd "$(dirname "$0")"
pwd
)
buildType=$1
datetime=$(date "+%Y%m%d%H%M%S")
proName=$(cat manifest.json | jq -r .proName) #proName跟HBuildx的项目名称一致
appid=$(cat manifest.json | jq -r .appid)
versionName=`cat manifest.json | jq -r .versionName`
# versionName=$(cat manifest.json | jq -r .versionName)
echo "---读取manifest.json中相关配置---"
echo "proName=$proName appid=$appid versionName=$versionName"
pkgNameApp=${appid}_${datetime}_${versionName}.wgt
pkgNameH5=h5_${datetime}_${versionName}
appBuildDir=$curDir/unpackage/release
h5BuildDir=$curDir/unpackage/dist/build
mkdir -p $curDir/unpackage/realseDist
releaseDir=$curDir/unpackage/realseDist
replacePro() {}
echo "=====环境依赖初始化==node版本===源配置==isDev==="
# node -v
# npm config set registry https://registry.npm.taobao.org
replacePro ${curDir}/config.js isDev false
echo "=====build==中====="
#h5类型构建,不能指定构建名称和路径,生成h5目录后,要脚本辅助处理
if [ $buildType = 1 ]; then
pkgName=$pkgNameH5
echo $pkgName
cli publish --platform h5 --project $proName --name $pkgName
mv $h5BuildDir/h5 $h5BuildDir/$pkgName
cd $h5BuildDir
zip -r $pkgName.zip $pkgName
rm -rf $pkgName
mv $h5BuildDir/$pkgName.zip $releaseDir/
#app类型构建
else
pkgName=$pkgNameApp
echo $pkgName
cli publish --platform APP --project $proName --name ${pkgName} --type wgt
mv $appBuildDir/$pkgName $releaseDir/
fi
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
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
# 预处理build.js
package.json
"scripts": {
"proApp": "yarn tabbar && cross-env NODE_ENV=production UNI_PLATFORM=app-plus VUE_APP_PLATFORM=app-plus node scripts/build.js && vue-cli-service uni-build --sourcemap",
"proH5Yzy": "cross-env NODE_ENV=production UNI_PLATFORM=h5 VUE_APP_PLATFORM=h5 VUE_APP_PRO_TYPE=yzy node scripts/build.js && vue-cli-service uni-build --sourcemap",
"proH5Yss": "cross-env NODE_ENV=production UNI_PLATFORM=h5 VUE_APP_PLATFORM=h5 VUE_APP_PRO_TYPE=yss node scripts/build.js && vue-cli-service uni-build --sourcemap",
"tabbar": "cross-env VUE_APP_PLATFORM=app-plus node scripts/build.js"
},
# 还是要分开来调用启动
"proApp": "yarn tabbarApp && cross-env NODE_ENV=production UNI_PLATFORM=app-plus VUE_APP_PLATFORM=app-plus vue-cli-service uni-build --sourcemap",
"proH5Yzy": "yarn tabbarH5 && cross-env NODE_ENV=production UNI_PLATFORM=h5 VUE_APP_PLATFORM=h5 VUE_APP_PRO_TYPE=yzy vue-cli-service uni-build --sourcemap",
"tabbarH5": "cross-env VUE_APP_PLATFORM=h5 node scripts/build.js",
"tabbarApp": "cross-env VUE_APP_PLATFORM=app-plus node scripts/build.js",
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
src\conf.js
const baseTabBar = [ {
"text": "首页",
"pagePath": "pages/home/index",
"iconPath": "static/image/tabbar/home.png",
"selectedIconPath": "static/image/tabbar/home-s.png"
},
{
"text": "水库",
"pagePath": "pages/reservoir/index",
"iconPath": "static/image/tabbar/reservoir.png",
"selectedIconPath": "static/image/tabbar/reservoir-s.png"
},
{
"text": "地图",
"pagePath": "pages/map/index",
"iconPath": "static/image/tabbar/map.png",
"selectedIconPath": "static/image/tabbar/map-s.png"
},
{
"text": "我的",
"pagePath": "pages/my/index",
"iconPath": "static/image/tabbar/my.png",
"selectedIconPath": "static/image/tabbar/my-s.png"
}]
const tabBarList = isApp ? baseTabBar.splice(2,1) && baseTabBar : baseTabBar
console.log("------tabBarList------", tabBarList);
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
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
scripts\build.js
/*
* @author: samy
* @email: yessz#foxmail.com
* @time: 2023-08-07 17:57:47
* @modAuthor: samy
* @modTime: 2023-08-09 08:53:08
* @desc:
* Copyright © 2015~2023 DAM FE
*/
const {readFileSync, writeFileSync} = require('fs');
const {tabBarList} = require('../src/conf')
const path = require('path')
const pathFile = path.join(__dirname, '..', 'src', 'pages.json')
try {
const fileData = readFileSync(pathFile)
const jsonData = JSON.parse(fileData.toString())
console.log(jsonData.tabBar.list);
jsonData.tabBar.list = tabBarList
writeFileSync(pathFile, JSON.stringify(jsonData, null, " "))
console.log('----------new全局配置tablist修改成功-------------');
} catch (error) {
console.error(error);
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 加入版本信息
printVersion() {
versionFile=$1/v.json
cd ${curDir}
echo "==================================printVersion===================================="
# curBranch=$(git show-ref | grep $(git log --pretty=%h -1) | sed 's|.*/\(.*\)|\1|' | sort -u | grep -v HEAD)
curBranch=$(git symbolic-ref --short HEAD)
commitID=$(git rev-parse --short HEAD)
commitTime=$(git show --pretty=format:"%ci" | head -1)
buildTime=$(date "+%Y-%m-%d %H:%M:%S")
echo "{
\"version\":{
\"curBranch\": \"$curBranch\",
\"commitID\": \"$commitID\",
\"commitTime\": \"$commitTime\"
},
\"buildCfg\":{
\"buildTime\": \"$buildTime\",
\"isApp\": \"$buildType\"
}
}" > $versionFile
echo "=====版本记录生产$versionFile==成功========"
cat $versionFile
}
yarn build:h5Yss #先写死
printVersion $h5BuildDir/h5
yarn build:appYss #先写死
printVersion $appBuildDir/app-plus
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
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
# 集成JKS构建
sh build.sh 0 #app wgt包
sh build.sh 1 #h5 yzy包
sh build.sh 2 #h5 企业微信包
1
2
3
2
3
# nginx上部署
# 常用的命令
lsof -i tcp:8030
whereis nginx
cd /usr/local/nginx
sudo sbin/nginx -t
sudo sbin/nginx -s reload
1
2
3
4
5
6
7
2
3
4
5
6
7
# 配置
server {
listen 7992;
server_name fileserver;
location / {
add_header 'Access-Control-Allow-Origin' *;
root /data/webs/app/h5;
}
location /api/ {
proxy_pass http://127.0.0.1:8001/;
}
location /resource/ {
# 允许 所有头部 所有域 所有方法
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' '*';
root /data/webs/app/resource/;
#index index.html index.htm;
autoindex on;
}
}
server {
listen 8029;
location / {
# 允许 所有头部 所有域 所有方法
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' '*';
root /data/webs/app/resource;
#index index.html index.htm;
autoindex on;
}
}
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
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
# 线上包替换脚本
replace.sh
#!/bin/bash
###
# @author: samy
# @email: samyit#foxmail.com
# @time: 2023-10-07 15:49:05
# @modAuthor: samy
# @modTime: 2023-10-07 16:15:00
# @desc:
# Copyright © 2015~2023 DAM FE
###
datetime=$(date "+%Y%m%d%H%M%S")
echo "---$datetime---"
mv h5-old h5-$datetime && mv h5-$datetime ./olds/
cp -rf h5 h5-old
sleep 1
unzip h5Y*.zip && mv h5Y*.zip ./olds/
sleep 1
rm -rf h5 && mv h5Y*/ h5/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
加入输入判断参数后
#!/bin/bash
###
# @author: samy
# @email: samyit#foxmail.com
# @time: 2023-10-07 15:49:05
# @modAuthor: samy
# @modTime: 2023-10-13 08:58:07
# @desc:
# Copyright © 2015~2023 DAM FE
###
echo "Determine whether to update?"
read -p "Type Determine:" Enter
replace() {
datetime=$(date "+%Y%m%d%H%M%S")
echo "---$datetime---"
mv h5-old h5-$datetime && mv h5-$datetime ./olds/
echo "---迁移旧数据完成----"
cp -rf h5 h5-old
echo "---备份数据完成----"
sleep 1
unzip h5Y*.zip && mv h5Y*.zip ./olds/
echo "---解压zip完成----"
sleep 1
rm -rf h5 && mv h5Y*/ h5/
echo "---更新替换完成----"
}
case $Enter in
1)
replace
;;
*)
echo "Sounds interesting. I will try that."
echo
;;
esac
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
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
上次更新: 2023/11/17, 05:08:20