CodePush笔记
官方网站
http://microsoft.github.io/code-push/
1.安装客户端
npm安装命令 npm install -g code-push-cli
Note: On OSX and Linux, you may need to prefix this command with sudo
2.创建账户
命令行运行 code-push register
会打开一个网站,我选择使用github,授权后产生异个秘钥串,记录下来。
在终端输入 code-push login登录,以后再登录就不用注册了,只需要输入code-push login就可以了,然后出来一个key的页面,输入key就可以登录了。
其他的代码如下:
code-push login登陆
code-push logout注销
code-push access-key ls列出登陆的token
code-push access-key rm 删除某个 access-key
3.注册App到服务器
命令行运行 code-push app add
其他的命令如下:
code-push app add在账号里面添加一个新的app
code-push app remove或者 rm 在账号里移除一个app
code-push app rename重命名一个存在app
code-push app list或则 ls 列出账号下面的所有app
code-push app transfer把app的所有权转移到另外一个账号
4.在ios集成 CodePush
npm install –save react-native-code-push@latest
RN 0.39: npm install –save react-native-code-push@1.16.1-beta
手动配置
4.1
Xcode打开项目,从node_modules/react-native-code-push/ios 目录下拖拽CodePush.xcodeproj到项目library目录下。
4.2
Xcode中选中项目 Build Phases标签,“Link Binary With Libraries”选项
从Libraries/CodePush.xcodeproj/Products 拖拽libCodePush.a添加进link
4.3
点击+号,添加ios下libz.tbd库
4.4
打开 AppDelegate.m 添加CodePush头文件:
#import "CodePush.h"
...
// jsCodeLocation = [[RCTBundleURLProvider sharedSettings] //jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
#ifdef DEBUG jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
#else
jsCodeLocation = [CodePush bundleURL];
#endif
4.5
Xcode选中项目 点击General, 修改Version为三位数。
info.plist 添加一项 CodePushDeploymentKey 类型string ,值为Staging(测试key))或者Production(生产key)
查看key方法
code-push deployment ls -k
5.在android集成 CodePush
5.1
settings.gradle添加
include ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
5.2
app/build.gradle添加
apply from: "../../node_modules/react-native/react.gradle" //68行
apply from: "../../node_modules/react-native-code-push/android/ codepush.gradle"dependencies {
...
compile project(':react-native-code-push')
}
5.3
修改MainApplication.java
// 1. Import the plugin class.
import com.microsoft.codepush.react.CodePush; public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
...
// 2. Override the getJSBundleFile method in order to let
// the CodePush runtime determine where to get the JS
// bundle location from on each app start
@Override
protected String getJSBundleFile() {
return CodePush.getJSBundleFile();
}
@Override
protected List<ReactPackage> getPackages() {
// 3. Instantiate an instance of the CodePush runtime and add it to the list of
// existing packages, specifying the right deployment key. If you don't already
// have it, you can run "code-push deployment ls <appName> -k" to retrieve your key.
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new CodePush("deployment-key-here", MainApplication.this, BuildConfig.DEBUG)
);
}
};
}
React-Native 使用
import CodePush from "react-native-code-push";
let codePushOptions = { checkFrequency: codePush.CheckFrequency.ON_APP_RESUME };
class Root extends Component {
...
}
Root=codePush(codePushOptions)(Root);
打包命令
# Release a mandatory update with a changelog
code-push release-react MyApp-iOS ios -m --description "Modified the header color"
# Release an update for an app that uses a non-standard entry file name, and also capture
# the sourcemap file generated by react-native bundle
code-push release-react MyApp-iOS ios --entryFile MyApp.js --sourcemapOutput ../maps/MyApp.map
# Release a dev Android build to just 1/4 of your end users
code-push release-react MyApp-Android android --rollout 25% --dev true
# Release an update that targets users running any 1.1.* binary, as opposed to
# limiting the update to exact version name in the build.gradle file
code-push release-react MyApp-Android android --targetBinaryVersion "~1.1.0"
如下是有效的版本号范围表达式的例子:
范围表达式 谁获得更新
1.2.3 只有1.2.3版本
* 所有版本
1.2.x 主版本为1,小版本为2的任何版本
1.2.3 - 1.2.7 在 1.2.3 (包含) 和 1.2.7 (包含) 之间的版本
=1.2.3 <1.2.7 在 1.2.3 (包含) 和 1.2.7 (不包含)之间的版本
~1.2.3 相当于>=1.2.3 <1.3.0
^1.2.3 相当于>=1.2.3 <2.0.0
测试版本更新:code-push deployment history 应用名 Staging
生产版本更新:code-push deployment history 应用名 Production
一个部署环境的发布历史是不可以改变的,所以一旦被发布你不能删除或移除更新。然而,如果你发布了一个坏的更新或包含计划外的功能,使用rollback命令很容易把它回滚:
code-push rollback <appName> <deploymentName>
code-push rollback MyApp Production
促进更新
一旦测试完指定部署环境的版本更新(如:Staging),你想把它向下游推进(如:dev->staging, staging->production),你可以简单的用如下命令去从一个部署环境拷贝到另一个:
code-push promote <appName> <sourceDeploymentName> <destDeploymentName>
[--description <description>]
[--disabled <disabled>]
[--mandatory]
[--rollout <rolloutPercentage>]
[--targetBinaryVersion <targetBinaryVersion]
你可以用如下命令清除相关的发布历史:
code-push deployment clear <appName> <deploymentName>