React Native开发React Native开发经验集

用 Github Actions 为 React Native

2020-04-20  本文已影响0人  Kenny锅

一、打 Android APK 后,上传至蒲公英

在GitHub项目根目录下创建 .github/workflows/release.yml 文件,代码如下:

name: CI

on:
  push:
    branches: [ release ]
  pull_request:
    branches: [ release ]

env:
  PGYER_API_KEY: 9b8a407f285532d6759289xxxxxxxxxx
  APK_FILE_PATH: android/app/build/outputs/apk/release
  APK_FILE_NAME: app-release.apk

jobs:
  build-and-upload:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Install yarn dependencies
        run: |
          echo Node.js runtime version: $(node -v)
          yarn config set registry https://registry.npm.taobao.org
          yarn

      - name: Run unit test
        run: yarn test

      - name: Build release apk
        run: |
          cd android
          echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
          bash gradlew assembleRelease

      - name: Upload apk to artifact
        uses: actions/upload-artifact@v1
        with:
          name: ${{env.APK_FILE_NAME}}
          path: ${{env.APK_FILE_PATH}}

      - name: Upload apk to Pgyer
        run: |
          curl -F 'file=@${{env.APK_FILE_PATH}}/${{env.APK_FILE_NAME}}' -F '_api_key=${{env.PGYER_API_KEY}}' https://www.pgyer.com/apiv2/app/upload

二、自动发 Code Push 热更新

在GitHub项目根目录下创建 .github/workflows/hotfix.yml 文件,代码如下:

name: CI

on:
  push:
    branches: [ hotfix ]
  pull_request:
    branches: [ hotfix ]

env:
  CODE_PUSH_SECRET_KEY: 9a5bc0c5507d4a3524b9cc76xxxxxxxxx

jobs:
  push-hotfix-bundle:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Get package.json version
        id: version
        uses: notiz-dev/github-action-json-property@release
        with:
          path: ./package.json
          prop_path: version
      - run: echo ${{steps.version.outputs.prop}}

      - name: Install code push service for react native
        run: sudo npm install -g code-push-cli

      - name: Login code push service center
        run: code-push login --accessKey ${{env.CODE_PUSH_SECRET_KEY}}

      - name: Install yarn dependencies
        run: |
          echo Node.js runtime version: $(node -v)
          yarn config set registry https://registry.npm.taobao.org
          yarn

      - name: Run unit test
        run: yarn test

      - name: Set ubuntu fs max user watch
        run: echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

      - name: Push hotfix bundle to every registered apps
        run: |
          code-push whoami
          code-push release-react myApp android --dev false --targetBinaryVersion "${{steps.version.outputs.prop}}" --des "有紧急版本更新,请及时安装" -d Production -m

三、特别说明

1、在哪儿取到 Code Push accessKey

答:先在本地安装 sudo npm install -g code-push-cli,然后执行 code-push login,就能看到获取到 accessKey 了。

2、为什么把 key 直接写在 yml 文件里?好像不安全呢?

答:我这个项目属于 private,项目之外的人没有权限看到,所以偷个懒(如果你的项目是 public 千万别这么做),安全的做法是:创建 GitHub secret,然后在项目中用 ${{secrets.CODE_PUSH_SECRET_KEY}} 这种形式使用,添加 GitHub secret 方法如下图所示:

如果本文对你有帮助,请点赞、分享!

上一篇 下一篇

猜你喜欢

热点阅读