以下是一个简化的GitHub Action工作流代码实例,用于自动构建React Native项目的Android APK:
name: Build React Native Android APK
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Set up Android SDK
uses: android-actions/sdk-manager@v1
with:
packages: build-tools;28.0.3,platforms;android-28
- name: Install dependencies
run: yarn install
- name: Bundle React Native
run: yarn react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
- name: Build APK
run: cd android && ./gradlew assembleRelease
- name: Upload APK
uses: actions/upload-artifact@v1
if: success()
with:
name: apk
path: android/app/build/outputs/apk/release/app-release.apk
这个工作流程在收到push或pull request至main分支时会运行。它会检出代码,设置Java环境,下载并安装Android SDK,安装项目依赖,打包React Native的JavaScript代码,构建APK,并在构建成功的情况下上传APK文件作为工作流程产物。这样可以保证React Native项目的Android APK可以自动构建,提高了开发效率。