一、準備工作,先安裝code-push的行命令工具:
npm install code-push-cli@latest -g
安裝mysql
二、安裝 code-push-server
$ git clone https://github.com/lisong/code-push-server.git
$cd code-push-server
$ npm install
$ ./bin/db init --dbhost localhost --dbuser root --dbpassword#初始化mysql數據庫
需要修改 config/config.js中的配置
//文件存儲在本地配置 當storageType為local時需要配置
local: {
storageDir: "/Users/mac/workspaces/storage",
//文件下載地址 CodePush Server 地址 + '/download' download對應app.js里面的地址
downloadUrl: "http://192.168.1.127:3000/download",
// public static download spacename.
public: '/download'
},
jwt: {
// 登錄jwt簽名密鑰,必須更改,否則有安全隱患,可以使用隨機生成的字符串
// Recommended: 63 random alpha-numeric characters
// Generate using: https://www.grc.com/passwords.htm
tokenSecret: 'INSERT_RANDOM_TOKEN_KEY'
},
common: {
dataDir: "/Users/mac/workspaces/data",
//選擇存儲類型,目前支持local,oss,qiniu,s3配置
storageType: "local"
},
storageDir和dataDir修改成你實際的目錄。事先要創建這兩個目錄
downloadUrl需要換成服務器的IP
訪問 https://www.grc.com/passwords.htm ,生成一個token
$ ./bin/www#啟動服務 瀏覽器中打開 http://127.0.0.1:3000
三、運行demo
git clone https://github.com/lisong/code-push-demo-app.git
cd code-push-demo-app
npm install
用xcode打開ios下的工程文件。在菜單Product->Scheme->Edit Scheme...->Run->Info->Build Configuration中,選擇release
1、登錄服務器
code-push login http://127.0.0.1:3000#login in browser account:admin password:123456
在打開的瀏覽器中,得到一個token,把它填在終端上,就處于登錄狀態了。
2、添加App
code-push app add CodePushDemoApp-ios ios react-native
Successfully added the "CodePushDemoApp-ios" app, along with the following default deployments:
┌────────────┬───────────────────────────────────────┐
│ Name? ? ? │ Deployment Key? ? ? ? ? ? ? ? ? ? ? ? │
├────────────┼───────────────────────────────────────┤
│ Production │ yy4I8rHWHbWYfDkIyzADQwdKoPSS4ksvOXqog │
├────────────┼───────────────────────────────────────┤
│ Staging? ? │ MAZnvAsPe1RxY6TckoXUoci1Vuym4ksvOXqog │
└────────────┴───────────────────────────────────────┘
把這個值,填到ios工程目錄下的Info.plist
CodePushDeploymentKey
yy4I8rHWHbWYfDkIyzADQwdKoPSS4ksvOXqog
CodePushServerURL
http://192.168.1.127:3000/
CodePushServerURL,需要填上剛才服務器的地址
3、發布熱更新。
react-native run-ios
在模擬器中運行App
code-push release-react CodePushDemoApp-ios ios -d Production
執行后,結果如下。
Using the target binary version value "2.0.5" from "ios/CodePushDemoApp/Info.plist".
。。。。。。
Successfully released an update containing the "/var/folders/jn/vz5y4n9j3xs47k21tj4t_8xm0000gn/T/CodePush" directory to the "Production" deployment of the "CodePushDemoApp-ios" app.
此處要注意:熱更新不能修改版本號,修改需要在App Store中重新發布,熱更新針對的是不修改版本號的更新。
我之前的理解有錯誤,是群友幫忙才明白過來,否則在App中點擊時,服務器報304錯誤。
如果App的內容沒有改變,運行code-push release-react CodePushDemoApp-ios ios -d Production
將會報錯:
[Error]The uploaded package is identical to the contents of the specified deployment's current release.
4、android的區別
版本號是在 manifests/AndroidManifests.xml中的android:versionName="2.0.0"
$ cd android
$ ./gradlew assembleRelease
$ cd app/build/outputs/apk #install app-release.apk into your phone
由于 gradle的原因,如果無法通過AndroidStudio自動執行,可以把app-release.apk拷貝到手機上執行。
添加app
code-push app add CodePushReactNativeDemo-android android react-native
生成的key,放在MainApplication.java中
@Override
protected List getPackages() {
return Arrays.asList(
new MainReactPackage(),
new CodePush(
"Yk3NInRhwVMSVcbSxMcs9T6s1ymQ4ksvOXqog",
MainApplication.this,
BuildConfig.DEBUG,
"http://192.168.1.127:3000/"
)
);
}
發布熱更新
code-push release-react CodePushReactNativeDemo-android android -d Production
目前對于Stagin和Production,理解還不清楚,后續搞明白了再補充。