preface

Article content:

  • React-native Automation package Android and ios apps with Fastlane and automatically upload them to Dandelion (ios/ Android) or TestFlight(ios)
  • Customize a Fastlane Action

System environment:

  • MacOS

This tutorial is for Mac OS users only, as ios can only be packaged with Mac OS. Windows users can check out the Fastlane official documentation for android tutorials

Pre-knowledge:

  • Fastlane configuration files are written in Ruby, so you need to know Ruby

Suitable for readers:

  • I already have the React-Native project and have carried out the native packaging upload work for Android and ios. I want to use Fastlane to optimize the packaging deployment process

What is Fastlane?

Fastlane is the easiest way to automate Beta deployment and release of iOS and Android apps. 🚀 It handles all the tedious tasks, such as generating screen shots, handling code signing, and publishing applications.

Actual combat tutorial

Step 1: Environment configuration

Install Xcode Command Line Tools

xcode-select --install
Copy the code

Install ruby

The installation

brew install ruby
Copy the code

Check whether Ruby is installed successfully

ruby --version
Copy the code

Install the fastlane

Install Fastlane using RubyGems

sudo gem install fastlane -NV
Copy the code

Step 2: Initialize Fastlane

Go to the ios directory for your React-Native project

cd path/to/your/react-native/project/ios
Copy the code

Run the initialization command

fastlane init
Copy the code

Fastlane will then ask you to enter the relevant configuration:

Option 2

Choose the one without tvOS, which is 2

Enter your Apple ID for development. Password and other information will be required for the first configuration. Fill in as prompted

Fastlane will then automatically generate some files and display some prompts, which can be skipped by pressing Enter. The prompts are as follows:

  • You will be prompted to upload the configuration file to Git
  • It tells you that the file named Fastfile is used to write the configuration
  • Some tutorial addresses

The automatically generated files are as follows:

 - fastlane/
   - Appfile
   - Fastfile
 - Gemfile
 - Gemfile.lock
Copy the code

Step 3: Configure FastLane

Open the FastFile

Let me explain the meaning of automatic configuration generation

default_platform(:ios) # Default platform

platform :ios do
  desc "Push a new beta build to TestFlight" # Description text
  lane :beta do Run the command 'fastlane' to execute the following code
    increment_build_number(xcodeproj: "schema.xcodeproj") Update the build number
    build_app(workspace: "schema.xcworkspace".scheme: "schema") # packaged
    upload_to_testflight Upload to TestFlight
  end
end
Copy the code

Next, we will move the Fastlane folder, Gemfile, gemfile. lock to the project root directory, because later we will write the ios and Android packaging configuration in the same FastFile. Of course, you can also run Fastlane in the Android directory. Generate a separate configuration file.

One might ask here, why not just run it in the project root directoryfastlane init?

Since Faslane will tell you that the ios project cannot be found in the project root directory, make sure to run Fastlane Init in the ios or Androi directory of the project

Modifying a Configuration File

Replace FastFile with the following code

pyger_api_key = "your_pyger_api_key" # Replace the api_key of your dandelion account
pyger_user_key = "your_pyger_user_key" # replace user_key with your dandelion account

before_all do
  Run before lane is executed
end


# packaged ios
platform :ios do
  desc "Pack ios and upload"
  lane :publish do
    FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD = "xxxx"

    # select upload address
    export_method = "ad-hoc"
    upload = UI.select("Select upload address:"["pgyer"."TestFlight"])
    if upload == "TestFlight"
      export_method = "app-store" # Upload to testFlight and export to app Store
    else
      export_method = "ad-hoc" Upload to Pyger and export using ad-hoc
    end

    scheme_name = "scheme_name" # Replace with the scheme name for your ios project to package

    project = "./ios/scheme_name.xcodeproj"
  
    Build and # 1
    increment_build_number(xcodeproj: project)
		Get the latest build number
    build_number = get_build_number(xcodeproj: project)
		Get the app version number
    versoin_number = get_version_number(
      xcodeproj: project,
      target: scheme_name
    )
		# splice package file name
    output_name = "#{scheme_name}_#{versoin_number}_#{build_number}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"

    # packaged
    gym( # build_app alias
      workspace: "./ios/scheme_name.xcworkspace", 
      scheme: scheme_name, 
      export_method: export_method, 
      output_directory: "./ios/build",
      output_name: output_name,
      clean: true.# Clean up items before each pack
      suppress_xcode_output: true.Xcode output is not displayed during packaging
    )

    Upload to TestFlight or dandelion
    if upload === "TestFlight"
      testflight()
    else
      pgyer(api_key: pyger_api_key, user_key: pyger_user_key)
    end
  end
end

# packaged android
platform :android do
  desc "Pack up Android and upload it to Dandelion."
  lane :publish do
    project_dir = "./android/"
  
    gradle(task: 'clean', project_dir: project_dir) # to clean up
    gradle(task: 'assemble', build_type: 'Release', project_dir: project_dir) # packaged

    pgyer(api_key: pyger_api_key, user_key: pyger_user_key) # Upload to dandelion
  end
end

after_all do
  Run after lane is executed
end
Copy the code

Dandelion upload configuration

Run the command to install the Dandelion Fastlane plug-in

fastlane add_plugin pgyer
Copy the code

Enter y

Check the API Key and User Key of the dandelion to replace the content in the configuration file

run

Pack the ios

fastlane ios publish
Copy the code

Package of the android

fastlane android publish
Copy the code

You can also add commands to package.json

{
  "scripts": {
    "ios-publish": "fastlane ios publish"."android-publish": "fastlane android publish"}}Copy the code

Pack the ios

yarn ios-publish
Copy the code

Package of the android

yarn android-publish
Copy the code

Create a custom action

What is action?

Action is the action to be performed when FastLane is running. For example, the use of gym and testFlight in FastFile is the built-in Action of Fastlane. For details, see the built-in Action text of Fastlane

How do I create a custom Action?

Run the command to create an action

faslane new_action
Copy the code

Enter the name of your action. Name it with a small hump or underline link

Here we enter Hello as the name of the action

The following files are automatically generated in the Fastlane folder

- actions
	- hello.rb
Copy the code

To explain the contents of hello.rb, open the file hello.rb

module Fastlane
  module Actions
    module SharedValues
      HELLO_CUSTOM_VALUE = :HELLO_CUSTOM_VALUE # Define variables that can be shared
      
    end

    class HelloAction < Action
      def self.run(params)
        # fastlane will take care of reading in the parameter and fetching the environment variable:
        UI.message "Parameter API Token: #{params[:api_token]}"

        # sh "shellcommand ./path"

        # Actions. Lane_context [SharedValues::HELLO_CUSTOM_VALUE] = "my_val" shared variable assignment
      end

      # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
      # @! group Documentation
      # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

      def self.description
        "A short description with <= 80 characters of what this action does"
      end

      def self.details
        # Optional:
        # this is your chance to provide a more detailed description of this action
        "You can use this action to do cool things..."
      end

      def self.available_options
        # Define all options your action supports.

        # Below a few examples
        [
          FastlaneCore::ConfigItem.new(key: :api_token.env_name: "FL_HELLO_API_TOKEN".# The name of the environment variable
                                       description: "API Token for HelloAction".# a short description of this parameter
                                       verify_block: proc do |value|UI.user_error! ("No API token for HelloAction given, pass using `api_token: 'token'`") unless (value and not value.empty?)
                                          # UI.user_error! ("Couldn't find file at path '#{value}'") unless File.exist? (value)
                                       end),
          FastlaneCore::ConfigItem.new(key: :development.env_name: "FL_HELLO_DEVELOPMENT".description: "Create a development certificate instead of a distribution one".is_string: false.# true: verifies the input is a string, false: every kind of value
                                       default_value: false) # the default value if the user didn't provide one
        ]
      end

      def self.output
        # Define the shared values you are going to provide
        # Example[['HELLO_CUSTOM_VALUE'.'A description of what this value contains']]end

      def self.return_value
        # If your method provides a return value, you can describe here what it does
      end

      def self.authors
        # So no one will ever forget your contribution to fastlane :) You are awesome btw!
        ["Your GitHub/Twitter Name"]
      end

      def self.is_supported? (platform)# you can do things like
        #
        # true
        #
        # platform == :ios
        #
        #  [:ios, :mac].include?(platform)
        #

        platform == :ios
      end
    end
  end
end
Copy the code
  • Self. run: The code to run

  • Self. description: Action Simple function description

  • Self. details: Action Specifies the detailed function description

  • Self. available_options: Parameter definition

  • Self. output: The variable that the current action can share with external use

  • Self. return_value: The return value of the action

  • Self. authors: Action author information

  • self.is_supported? (platform) : supported platform

Then let’s modify the contents of Hello.rb

module Fastlane
  module Actions

    class HelloAction < Action
      def self.run(params)
        UI.message "Hello #{params[:name]}."
      end

      def self.description
        "A Hello action"
      end

      def self.available_options
        [
          FastlaneCore::ConfigItem.new(key: :name.env_name: "FL_HELLO_NAME".# The name of the environment variable
                                       description: "Your name".# a short description of this parameter
                                       verify_block: proc do |value|UI.user_error! ("Name mandatory!") unless (value and not value.empty?)
                                       end),]end
    end
  end
end

Copy the code

Test the

Add test code to FastFlie

lane :testHelloAction do
  hello() # corresponds to the action file name
end
Copy the code

run

fastlane testHelloAction
Copy the code

The results of

Successful run!

Reference article:

docs.fastlane.tools/

Thecodingmachine. Making. IO/react – nativ…

www.pgyer.com/doc/view/fa…