Automate React Native App Builds With Fastlane and Appcenter

0

Automating your application build process brings you speed, reliability and one less thing to worry about. It saves time, resources and effort getting an app from development to distribution.

What is a Fastlane?

  • Fastlane is an open-source platform aimed at simplifying Android and iOS deployment.
  • Easily publish new beta builds to testers so you can get valuable feedback, fast.
  • Automate fully working Continuous Delivery process.
  • It takes care of all the heavy lifting and makes it super easy to generate a signed ipa or app file

Why Fastlane?

What is Appcenter?

Microsoft Visual Studio App Center is an integrated mobile development lifecycle solution for iOS, Android and Windows apps. It brings together multiple services commonly used by mobile developers, including build, test, distribute, monitoring, diagnostics into one single integrated cloud solution.

You May Also Like: Make Your Code Reusable Between React And React Native

Getting started with Fastlane & Appcenter

Let’s begin with a simple Fastlane guide for building and uploading new react-native apps to Visual Studio App Center. There is no official guide available yet we have managed to successfully run it.

  • Install fastlane (Ruby is Prerequisite)

    sudo gem install fastlane
  • Initialize fastlane in your project

    cd YourAppName && mkdir fastlane && cd fastlane && touch Fastfile
  • The Fastfile stores the automation configuration that can be run with fastlane. Now we have an empty Fastfile in fastlane folder. All the available fastlane hooks, actions & lanes are documented here https://docs.fastlane.tools/. Some provided actions listed below which we will apply in this tutorial.
    • appcenter_upload (Distribute new release to App Center)
    • update_fastlane (Makes sure fastlane-tools are up-to-date when running fastlane)
    • ensure_git_branch (Raises an exception if not on a specific git branch)
    • push_to_git_remote (Push local changes to the remote branch)
    • ensure_git_status_clean (Raises an exception if there are uncommitted git changes)
    • increment_build_number (Increment the build number of your project)
    • IOS :
      • gym (Building and packaging apps)
      • cocoapods (Installs pod for project)
    • ANDROID :
      • gradle (Building and packaging apps)

IOS Configuration

Signing

For new projects, we need to create a bundle ID and provisioning profile. To create a new bundle ID, run:

fastlane produce create -i

To setup a provisioning profile, create a GIT repo and then run:

fastlane match init

A Matchfile will be created. Change the profile type (e.g. appstore, adhoc) and add your Apple developer account under username.

Now, add the following to the Fastfile:

platform :ios do    
  desc 'Fetch certificates and provisioning profiles'
  lane :certificates do
    match(app_identifier: 'com.app.bundle')
  end
end

Change com.app.bundle to the bundle ID you created earlier. In the future, pass readonly: true to the match action. This would ensure the command wouldn’t create new provisioning profiles.

Building

Add the following lane to iOS:

lane :bump_build_version do
  increment_build_number(xcodeproj: './ios/YourAppName.xcodeproj')
  commit_version_bump(message: 'Bump iOS build', xcodeproj: './ios/YourAppName.xcodeproj')
  git_pull
  push_to_git_remote
end

lane :build do
  cocoapods(podfile: "./ios/Podfile")
  certificates
  bump_build_version
  gym(
    scheme: 'YourAppName', 
    project: './ios/YourAppName.xcodeproj',
    clean: true, 
    workspace: './ios/YourAppName.xcworkspace, 
    export_method: 'development', 
    output_directory: './builds'
  )
end

Uploading

Add the following lane to iOS:

lane :beta do
  bump_build_version
  build
  appcenter_upload(
    api_token: ENV["APPCENTER_API_TOKEN"],
    owner_name: ENV["APPCENTER_OWNER_NAME"],
    owner_type: ENV["APPCENTER_OWNER_TYPE"],
    app_name: ENV["APPCENTER_APP_NAME_IOS"],
    ipa: ENV["IPA_PATH"]
  )
end

Run fastlane ios beta. Your IOS application is now on App Center! 

Android Configuration

Building

platform :android do   
  desc 'Bump version and Build the Android application.'
  lane :bump_build_version do
    path = '../android/app/build.gradle'
    re = /versionCode\s+(\d+)/ 

    s = File.read(path)
    versionCode = s[re, 1].to_i     
    s[re, 1] = (versionCode + 1).to_s

    f = File.new(path, 'w')     
    f.write(s)     
    f.close  
   
    repo_clean = `git status --porcelain`.empty?     
    if !repo_clean           
      git_commit(path: 'android/app/build.gradle', message: 'Bump android build')     
    end     
    git_pull           
    push_to_git_remote 
  end

  lane :build do     
     gradle(task: 'clean', project_dir: 'android/')     
     gradle(
        task: 'assemble', 
        build_type: 'release', 
        project_dir: 'android/',
        flavor: ENV["ANDROID_FLAVOUR"]
     )   
  end 
end

Generated .apk will be at 
android/app/build/outputs/apk/release/app-release.apk

Uploading

Add the following to the Android lane:

desc 'Build and upload to App Center.'
lane :beta do
  bump_build_version
  build
  appcenter_upload(
    api_token: ENV["APPCENTER_API_TOKEN"],
    owner_name: ENV["APPCENTER_OWNER_NAME"],
    owner_type: ENV["APPCENTER_OWNER_TYPE"],
    app_name: ENV["APPCENTER_APP_NAME_ANDROID"],
    apk: ENV["APK_PATH"]
  )
end

Run fastlane android beta. Your android application is now on App Center!

Leave A Reply

Your email address will not be published.