0
0
React-nativeHow-ToBeginner ยท 4 min read

How to Use Fastlane with React Native for Automated Builds

To use fastlane with React Native, first install fastlane and initialize it in your iOS and Android project folders. Then create Fastfile lanes to automate building, testing, and deploying your app with simple commands.
๐Ÿ“

Syntax

Fastlane uses a Fastfile to define lanes, which are sets of tasks like building or deploying your app. Each lane is a Ruby method that runs commands.

Basic lane syntax:

lane :lane_name do
  # actions like build, test, deploy
end

Common actions include build_app for building, test for running tests, and upload_to_app_store or upload_to_play_store for deployment.

ruby
lane :build_ios do
  build_app(scheme: "YourAppScheme")
end

lane :build_android do
  gradle(task: "assembleRelease")
end
๐Ÿ’ป

Example

This example shows a simple Fastfile with lanes to build iOS and Android React Native apps and deploy the iOS app to TestFlight.

ruby
default_platform(:ios)

platform :ios do
  desc "Build and upload iOS app to TestFlight"
  lane :beta do
    build_app(scheme: "YourAppScheme")
    upload_to_testflight
  end
end

platform :android do
  desc "Build Android release APK"
  lane :build do
    gradle(task: "assembleRelease")
  end
end
Output
When running `fastlane ios beta`, the iOS app builds and uploads to TestFlight. When running `fastlane android build`, the Android release APK is built.
โš ๏ธ

Common Pitfalls

  • Not installing fastlane correctly or missing dependencies like Ruby.
  • Forgetting to initialize fastlane separately in iOS and Android folders.
  • Using wrong scheme names or Gradle tasks causing build failures.
  • Not setting up app store credentials or environment variables for deployment.

Always test lanes locally before automating CI/CD.

ruby
wrong_lane = "lane :build_ios do\n  build_app(scheme: \"WrongScheme\")\nend"

correct_lane = "lane :build_ios do\n  build_app(scheme: \"YourAppScheme\")\nend"
๐Ÿ“Š

Quick Reference

Fastlane commands for React Native:

  • fastlane init - Initialize fastlane in your project folder.
  • fastlane ios lane_name - Run an iOS lane.
  • fastlane android lane_name - Run an Android lane.
  • build_app - Build iOS app.
  • gradle(task: "assembleRelease") - Build Android release APK.
  • upload_to_testflight - Upload iOS build to TestFlight.
  • upload_to_play_store - Upload Android build to Google Play.
โœ…

Key Takeaways

Install and initialize fastlane separately in iOS and Android folders of your React Native project.
Define lanes in Fastfile to automate building, testing, and deploying your app.
Use correct scheme names for iOS and Gradle tasks for Android to avoid build errors.
Set up app store credentials and environment variables before deployment lanes.
Test your fastlane lanes locally before integrating into CI/CD pipelines.