最佳实践
前面已经介绍过ci的最近本使用方式了,其实对于最基本的小程序来讲已将满足使用条件了,这篇继续介绍一些常见的问题
在实际开发中可能一个小程序会被套用到不同的appid中,给不同的客户使用,类似于小程序sass系统,那么就会面临很多小程序的问题,如:不用appid发版不同步一系列ci面临的问题
接下来就一步步尝试解决它们
基本配置
项目中最原始普通的一份ci配置文件
js
// mini-ci.config.js
const { defineConfig } = require("@hengshuai/mini-core");
const { Platform, IProjectActionMode } = require("@hengshuai/mini-type");
const config = defineConfig({
platforms: {
[Platform.Wechat]: {
platformSpecific: {
projectPath: "./dist/build/mp-weixin",
privateKeyPath: ".mini-ci/keys/wechat-upload-code.key",
},
subs: [
{
appId: "appId1",
admin: "https://mp.weixin.qq.com",
version: "0.0.1",
mode: IProjectActionMode.UPLOAD_CODE,
},
{
appId: "appId2",
// ...
},
// ...
],
},
},
});
module.exports = config;
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
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
动态配置
面对项目中多个平台多个appid的场景,经常会遇到许多同步问题,而ci只会读取配置文件来执行逻辑,所以最好的方式还是在配置文件中通过环境变量来动态配置
环境变量
假如项目中有很多appid,试想某次只想发版某个appid,那么可以通过环境变量来控制需要的appid
sh
# ci要执行的appid
ciAppIds=wx3434242234
1
2
2
- 通过配置文件选项来控制待执行的appid
js
// mini-ci.config.js
const dotenv = require("dotenv");
// 读取env配置
const envConfig = dotenv.config({
path: ".env"
}).parased;
defineConfig({
// 内部提供了ci.appids属性,如果设置了此属性,ci执行过程中会过滤掉不需要的appid(默认空会执行所有)
ci: {
appids: envConfig.ciAppIds.split(",")
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
- 自定义逻辑
还是上面通过读取环境变量来动态配置,区别就是可以在配置中自定义逻辑来代替ci的选项配置
js
const { defineConfig } = require("@hengshuai/mini-core");
const { definePlatformConfig } = require("@hengshuai/mini-helper");
const { Platform, IProjectActionMode } = require("@hengshuai/mini-type");
const dotenv = require("dotenv");
const envConfig = dotenv.config({
path: resolvePath(".env"),
}).parsed;
const ciAppIds = parsedConfig.ciAppIds?.split(",") || null;
let platformConfig = definePlatformConfig({
[Platform.Wechat]: {
subs: [
{ appId: "appId1" },
{ appId: "appId2" },
// ...
]
}
})
// 自定义逻辑判断(伪代码)
ciAppIds.forEach(appId => {
// do platformConfig something...
})
const config = defineConfig({
platforms: {
...platformConfig
}
});
module.exports = config;
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
以上配置只是简单的演示,具体的逻辑可以根据业务需要自由调节
命令行参数
除了在修改配置文件逻辑外,也可以通过命令行参数来控制ci执行
sh
npx mini-ci start --appids appId1,appId2...
1


你的支持,我的动力🤗