GoRapid Blog

Stories from people who build @GoRapid

Author

Rohan Kumar


Blog categories
  • Web Development

tags

How to Automate your React Native App with Fastlane

25 September: Rohan Kumar

What is Fastlane ?

Fastlane is an open-source platform aimed at simplifying Android and iOS deployment. Fastlane lets you automate every aspect of your development and release workflow. Fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. 🚀 It handles all tedious tasks, like generating screenshots, dealing with code signing, and releasing your application.

Responsive image
Why Fastlane ?

e6fe5b40296509fb8e2c00cd16173d92.jpg

Everyone that has been releasing apps manually knows how tiresome and time-consuming process.

These are the following points why we need Fastlane.

  • 🚀 Save hours every time you push a new release to the store or beta testing service
  • ✨ Integrates with all your existing tools and services (more than 400 integrations)
  • 📖 100% open source under the MIT license
  • 🎩 Easy setup assistant to get started in a few minutes
  • âš’ Runs on your machine, it's your app and your data
  • 👻 Integrates with all major CI systems
  • 🖥 Supports iOS, Mac, and Android apps
  • 🔧 Extend and customize Fastlane to fit your needs, you're not dependent on anyone
  • 💭 Never remember any commands anymore, just Fastlane
  • 🚢 Deploy from any computer, including a CI server

Getting started with fastlane for Android

Fastlane can be installed in multiple ways. but we will set up using bundler with. First, we need to install a ruby environment in the system, If you use macOS, system Ruby is not recommended. You may already have Ruby installed on your computer. Fastlane supports Ruby versions 2.5 or newer. You can check inside a terminal emulator by typing:

$ ruby -v 

We'll install ruby by using snap. You can use it like this:

$ sudo snap install ruby --classic 

We have several channels per Ruby minor series. For instance, the following commands switch to Ruby 2.7:

$ sudo snap switch ruby --channel=2.7/stable
$ sudo snap refresh 

Now, we need to install bundler using gem because bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed

$ gem install bundler 

Create a ./Gemfile in the root directory of your project with the content

source "https://rubygems.org"

gem "fastlane" 
Run $ bundle install 

It'll create ./Gemfile and ./Gemfile.lock If you want to update Gemfile with the new version or (update)

Run $bundle update 

after install Gemfile dependencies install Fastlane using gem

$ sudo gem install fastlane 

Go on your current directory where did you create Gemfile (it can be in your android folder ex. projectName/android)

Run $ fastlane init 

You'll be asked to confirm that you're ready to begin, and then for a few pieces of information. To get started quickly:

Provide the package name for your application when asked (e.g. io.fabric.yourapp)

Press enter when asked for the path to your JSON secret file

Answer 'n' when asked if you plan on uploading info to Google Play via fastlane (we can set this up later)

That's it! fastlane will automatically generate a configuration for you based on the information provided.

You can see the newly created ./fastlane directory, with the following files:

Appfile which defines configuration information that is global to your app

Fastfile which defines the "lanes" that drive the behavior of fastlane

The most interesting file is fastlane/Fastfile, which contains all the information that is needed to distribute your app.

Responsive image

Now you can make multiple lanes according to your requirement in the Fastlane file. basically, there are two common lanes first one is for beta, and the second is for release.

you need to define your lane and their task on the Fastlane file

Every time you run Fastlane lane like this

Run $ bundle exec Fastlane [lane name] 

Important Note:

You need a JSON credential file from Google Play Console to upload your APK to the play store.

for beta testing, you can use Firebase App Distribution to share your app in Firebase.

for more information about the setup lane, I attached an important document link.



I'm showing a sample of writing Fastlane files.

# This file contains the Fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want Fastlane to automatically update itself
# update_fastlane

default_platform(:android)

firebase_app_android = "1:734807684046:android:75366f3c20c5ac0a63f19f"
firebase_app_ios = "1:734807684046:ios:24e0154d5508ba4863f19f"

channel = "#astromobile-releases"
slack_url = "your slack hook channel url"
firebase_app_distribution_group = "your firebase  distributor team group name"

before_all do
  update_fastlane
  # Install node packages
  sh 'npm install'
end

platform :android do
  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

  lane :build do |options|
    build_type = options[:build_type] ? options[:build_type] : 'StagingRelease'

    if build_type != 'StagingRelease' && build_type != 'ProductionRelease'
      UI.user_error!("build_type can only be one of StagingRelease or ProductionRelease")
    end

    # XXX: Remove hardcoded build_type once we have different build_types
    # support
    gradle(
      task: "clean assemble",
      build_type: build_type,
      project_dir: "android",
    )
  end

  desc "Submit a new Beta Build to Crashlytics Beta"
  lane :beta do |options|
    build_type = options[:build_type] ? options[:build_type] : 'StagingRelease'

    if build_type != 'StagingRelease' && build_type != 'ProductionRelease'
      UI.user_error!("build_type can only be one of StagingRelease or ProductionRelease")
    end

    commit = last_git_commit

    # XXX: Remove hardcoded build_type once we have different build_types
    # support
    if build_type == 'StagingRelease'
      version_name = "v-stag-#{commit[:commit_hash][0...8]}"
      version_name_short = "v-#{commit[:commit_hash][0...4]}"
    else
      version_name = "v-prod-#{commit[:commit_hash][0...8]}"
      version_name_short = "v-#{commit[:commit_hash][0...4]}"
    end

    android_set_version_code(
      gradle_file: "android/app/build.gradle",
    )
    android_set_version_name(
      version_name: version_name,
      gradle_file: "android/app/build.gradle",
    )
    add_badge(
      shield: "v-#{version_name_short}-blue",
      glob: "/**/res/**/{ic_launcher,ic_launcher_round}.{png,PNG}"
    )
    slack(
      channel: channel,
      slack_url: slack_url,
      message: "Successfully distributed a new Android beta build to firebase",
    )

    build(
      build_type: build_type,
    )

    changelog = read_changelog(
      changelog_path: './CHANGELOG.md',
      section_identifier: '[Unreleased-android]',
      #excluded_markdown_elements: ['-', '###']
    )

    firebase_app_distribution(
      app: firebase_app_android,
      groups: firebase_app_distribution_group,
      release_notes: changelog,
      # service_credentials_file: ENV['SERVICE_CREDENTIALS_FILE']
    )

    slack(
      channel: channel,
      slack_url: slack_url,
      message: "Successfully distributed a new Android beta build to firebase",
    )
  end

  desc "Deploy a new version to the Google Play"
  lane :playstore do
    app_version = options[:app_version]

    if !app_version
      UI.user_error!("Please specify the version to release")
    end

    android_set_version_code(
      gradle_file: "android/app/build.gradle",
    )
    android_set_version_name(
      version_name: app_version,
      gradle_file: "android/app/build.gradle",
    )
    commit_android_version_bump(
      gradle_file_folder:"android/app"
    )
    gradle(
      task: 'clean assemble',
      build_type: 'ProductionRelease',
      project_dir: "android",
    )

    # Uploads the APK built in the gradle step above and releases it
    # to all production users
    supply(
      release_status: 'draft',
      json_key: ENV['JSON_KEY']
    )

    slack(
      channel: channel,
      slack_url: slack_url,
      message: "Successfully distributed a new Android build to playstore",
    )
  end
end 

How to Automate your React Native App with Fastlane

25 September: Rohan Kumar

What is Fastlane ?

Fastlane is an open-source platform aimed at simplifying Android and iOS deployment. Fastlane lets you automate every aspect of your development and release workflow. Fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. 🚀 It handles all tedious tasks, like generating screenshots, dealing with code signing, and releasing your application.

Responsive image
Why Fastlane ?

e6fe5b40296509fb8e2c00cd16173d92.jpg

Everyone that has been releasing apps manually knows how tiresome and time-consuming process.

These are the following points why we need Fastlane.

  • 🚀 Save hours every time you push a new release to the store or beta testing service
  • ✨ Integrates with all your existing tools and services (more than 400 integrations)
  • 📖 100% open source under the MIT license
  • 🎩 Easy setup assistant to get started in a few minutes
  • âš’ Runs on your machine, it's your app and your data
  • 👻 Integrates with all major CI systems
  • 🖥 Supports iOS, Mac, and Android apps
  • 🔧 Extend and customize Fastlane to fit your needs, you're not dependent on anyone
  • 💭 Never remember any commands anymore, just Fastlane
  • 🚢 Deploy from any computer, including a CI server

Getting started with fastlane for Android

Fastlane can be installed in multiple ways. but we will set up using bundler with. First, we need to install a ruby environment in the system, If you use macOS, system Ruby is not recommended. You may already have Ruby installed on your computer. Fastlane supports Ruby versions 2.5 or newer. You can check inside a terminal emulator by typing:

$ ruby -v 

We'll install ruby by using snap. You can use it like this:

$ sudo snap install ruby --classic 

We have several channels per Ruby minor series. For instance, the following commands switch to Ruby 2.7:

$ sudo snap switch ruby --channel=2.7/stable
$ sudo snap refresh 

Now, we need to install bundler using gem because bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed

$ gem install bundler 

Create a ./Gemfile in the root directory of your project with the content

source "https://rubygems.org"

gem "fastlane" 
Run $ bundle install 

It'll create ./Gemfile and ./Gemfile.lock If you want to update Gemfile with the new version or (update)

Run $bundle update 

after install Gemfile dependencies install Fastlane using gem

$ sudo gem install fastlane 

Go on your current directory where did you create Gemfile (it can be in your android folder ex. projectName/android)

Run $ fastlane init 

You'll be asked to confirm that you're ready to begin, and then for a few pieces of information. To get started quickly:

Provide the package name for your application when asked (e.g. io.fabric.yourapp)

Press enter when asked for the path to your JSON secret file

Answer 'n' when asked if you plan on uploading info to Google Play via fastlane (we can set this up later)

That's it! fastlane will automatically generate a configuration for you based on the information provided.

You can see the newly created ./fastlane directory, with the following files:

Appfile which defines configuration information that is global to your app

Fastfile which defines the "lanes" that drive the behavior of fastlane

The most interesting file is fastlane/Fastfile, which contains all the information that is needed to distribute your app.

Responsive image

Now you can make multiple lanes according to your requirement in the Fastlane file. basically, there are two common lanes first one is for beta, and the second is for release.

you need to define your lane and their task on the Fastlane file

Every time you run Fastlane lane like this

Run $ bundle exec Fastlane [lane name] 

Important Note:

You need a JSON credential file from Google Play Console to upload your APK to the play store.

for beta testing, you can use Firebase App Distribution to share your app in Firebase.

for more information about the setup lane, I attached an important document link.



I'm showing a sample of writing Fastlane files.

# This file contains the Fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want Fastlane to automatically update itself
# update_fastlane

default_platform(:android)

firebase_app_android = "1:734807684046:android:75366f3c20c5ac0a63f19f"
firebase_app_ios = "1:734807684046:ios:24e0154d5508ba4863f19f"

channel = "#astromobile-releases"
slack_url = "your slack hook channel url"
firebase_app_distribution_group = "your firebase  distributor team group name"

before_all do
  update_fastlane
  # Install node packages
  sh 'npm install'
end

platform :android do
  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

  lane :build do |options|
    build_type = options[:build_type] ? options[:build_type] : 'StagingRelease'

    if build_type != 'StagingRelease' && build_type != 'ProductionRelease'
      UI.user_error!("build_type can only be one of StagingRelease or ProductionRelease")
    end

    # XXX: Remove hardcoded build_type once we have different build_types
    # support
    gradle(
      task: "clean assemble",
      build_type: build_type,
      project_dir: "android",
    )
  end

  desc "Submit a new Beta Build to Crashlytics Beta"
  lane :beta do |options|
    build_type = options[:build_type] ? options[:build_type] : 'StagingRelease'

    if build_type != 'StagingRelease' && build_type != 'ProductionRelease'
      UI.user_error!("build_type can only be one of StagingRelease or ProductionRelease")
    end

    commit = last_git_commit

    # XXX: Remove hardcoded build_type once we have different build_types
    # support
    if build_type == 'StagingRelease'
      version_name = "v-stag-#{commit[:commit_hash][0...8]}"
      version_name_short = "v-#{commit[:commit_hash][0...4]}"
    else
      version_name = "v-prod-#{commit[:commit_hash][0...8]}"
      version_name_short = "v-#{commit[:commit_hash][0...4]}"
    end

    android_set_version_code(
      gradle_file: "android/app/build.gradle",
    )
    android_set_version_name(
      version_name: version_name,
      gradle_file: "android/app/build.gradle",
    )
    add_badge(
      shield: "v-#{version_name_short}-blue",
      glob: "/**/res/**/{ic_launcher,ic_launcher_round}.{png,PNG}"
    )
    slack(
      channel: channel,
      slack_url: slack_url,
      message: "Successfully distributed a new Android beta build to firebase",
    )

    build(
      build_type: build_type,
    )

    changelog = read_changelog(
      changelog_path: './CHANGELOG.md',
      section_identifier: '[Unreleased-android]',
      #excluded_markdown_elements: ['-', '###']
    )

    firebase_app_distribution(
      app: firebase_app_android,
      groups: firebase_app_distribution_group,
      release_notes: changelog,
      # service_credentials_file: ENV['SERVICE_CREDENTIALS_FILE']
    )

    slack(
      channel: channel,
      slack_url: slack_url,
      message: "Successfully distributed a new Android beta build to firebase",
    )
  end

  desc "Deploy a new version to the Google Play"
  lane :playstore do
    app_version = options[:app_version]

    if !app_version
      UI.user_error!("Please specify the version to release")
    end

    android_set_version_code(
      gradle_file: "android/app/build.gradle",
    )
    android_set_version_name(
      version_name: app_version,
      gradle_file: "android/app/build.gradle",
    )
    commit_android_version_bump(
      gradle_file_folder:"android/app"
    )
    gradle(
      task: 'clean assemble',
      build_type: 'ProductionRelease',
      project_dir: "android",
    )

    # Uploads the APK built in the gradle step above and releases it
    # to all production users
    supply(
      release_status: 'draft',
      json_key: ENV['JSON_KEY']
    )

    slack(
      channel: channel,
      slack_url: slack_url,
      message: "Successfully distributed a new Android build to playstore",
    )
  end
end