一、CodePush CLI安裝
$ npm install?-g code-push-cli
二、注冊 CodePush賬號
$ code-push register
當(dāng)執(zhí)行完register命令后,會自動打開一個授權(quán)網(wǎng)頁,進(jìn)行授權(quán)登錄,這里我選擇的是使用GitHub登錄。注冊成功后獲取到一個key值,復(fù)制后輸入終端即可登錄成功。
$ code-push login
輸入此命令驗(yàn)證是否已登錄。
三、在CodePush服務(wù)器注冊App
1、注冊iOS App:
code-push app add <AppName> ios react-native
比如:code-push app add CodePushDemo_iOS ios react-native
2、注冊Android App:
code-push app add <AppName> Android react-native
比如:code-push app add CodePushDemo_android Android react-native
注冊成功后會生成2個Deployment Key,分為<Staging>和<Production>,這2個key需要配置到原生項(xiàng)目中去。
3、查看注冊的App列表
$ code-push app list

4、查看注冊App的Deployment Key
$ code-push deployment ls <AppName> -k

四、添加CodePush組件
1、安裝組件:將codepush添加到node_modules
$ npm install react-native-code-push--save
2、添加依賴:
$?npm link react-native-code-push
五、原生項(xiàng)目中配置CodePush
1、iOS項(xiàng)目配置:
a.使用Xcode打開項(xiàng)目,在Xcode的項(xiàng)目導(dǎo)航視圖中的PROJECT下選擇Info頁簽 ,在Configurations節(jié)點(diǎn)下單擊 + 按鈕 ,選擇Duplicate "Release" Configaration,輸入Staging。

b.選擇Build Settings欄,搜索Build Location -> Per-configuration Build Products Path -> Staging,將之前的值:$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)改為:$(BUILD_DIR)/Release$(EFFECTIVE_PLATFORM_NAME)
注意:Xcode11之后沒有了?Per-configuration Build Products Path 這個配置

c.選擇Build Settings欄,點(diǎn)擊?+?號,選擇Add User-Defined Setting,將key設(shè)置為CODEPUSH_KEY(名稱可自定義,但要與Info.plist里面保持一致),Release和Staging的值為前面創(chuàng)建的Deployment Key,直接復(fù)制進(jìn)去即可。

d.打開Info.plist文件,在CodePushDeploymentKey中輸入$(CODEPUSH_KEY),并修改Bundle versions為三位,CodePush必須要三位(格式:1.1.0)

e.工程Scheme切換到Staging環(huán)境運(yùn)行、調(diào)試

注意:如果報錯:library not found for -lPods ,在target下刪除libPods-xxxx.a,即可運(yùn)行。(多target項(xiàng)目會出現(xiàn))

f.將 'CodePush' Pod到原生項(xiàng)目中
在Podfile文件中添加:pod 'CodePush', :path => '../node_modules/react-native-code-push',然后執(zhí)行pod install。
g.修改項(xiàng)目中bundleURL的加載方式
測試環(huán)境:[[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
改為:[CodePush bundleURL]; <默認(rèn)生成的文件名為:main.jsbundle>
注意:如果打包后codepush熱更新沒有生效,則可能是第<d>步驟配置的codepush_key沒有生效,可以直接將Deployment Key只輸入。

六、js文件中配置CodePush
在index.js文件添加CodePush.sync(),App啟動后就會加載更新。

在其他js界面的componentDidMount()方法中通過CodePush.sync()進(jìn)行更新。

七、發(fā)布更新包
iOS發(fā)布更新:
Staging環(huán)境:code-push?release-react?demo_iOS?ios?--plistFile?./ios/demo/Info.plist?--mandatory?true
Production環(huán)境:code-push?release-react?demo_iOS?ios?--plistFile?./ios/demo/Info.plist?--deploymentName?Production?--mandatory?true
Android發(fā)布更新:
Staging環(huán)境:code-push?release?demo_Android?./bundles?<版本號> --description?'更新內(nèi)容'--mandatory?true
Production環(huán)境:code-push?release?demo_Android?./bundles?<版本號> --deploymentName?Production??--description?'更新內(nèi)容'?--mandatory?true
八、版本控制
1、版本回滾(rollback)
code-push rollback <appName> <deploymentName>? --targetRelease <releaseLabel>?
如:code-push rollback demo_iOS?Staging --targetRelease v4?
? ? ? ? "demo_iOS" 中 "Staging" 部署執(zhí)行回滾,回滾到v4這個標(biāo)簽版本。
注意:當(dāng)部署的版本不同時,不能跨版本回滾!??!
例如:CodePush歷史版本中為7.0.1,此時發(fā)布了7.1.0版本。 當(dāng)從7.1.0發(fā)起回滾操作回到7.0.1時,是不可行的。
2、版本清除(clear)
code-push deployment clear
如:code-push deployment clear demo_iOS Staging
注意:不能指定清除版本,發(fā)布的所有版本會被全部清除?。。?/b>
九、版本更新推送百分比控制(rollout)
code-push promote??<appName> Staging Production
?"App"中"Staging"部署的最新更新發(fā)布到"Production"部署中,推送100%的用戶。
code-push promote?<appName> Staging Production -r 25
"App"中"Staging"部署的最新更新發(fā)布到"Production"部署中,并且只推送25%的用戶。

注意:如果Production版本中有rollout版本存在,則再執(zhí)行code-push promote會失敗??梢韵惹宄暗陌姹驹僦匦峦扑?。
[Error]? Cannot promote to an unfinished rollout release unless it is already disabled.