background
In the development process, we always have to go through testing, uploading and other links. In this development phase, we need to package different environments and upload the corresponding distribution background (e.g. dandelion, TestFlight, iTunes Connect). In each manual packaging process, we need to select the corresponding environment, the corresponding packaged certificate, the correct target and so on. If one of the above steps is wrong, you may have to go through the above process all over again, or worse, you may transfer the data from the test server to the official server. Careful reflection leads to endless trouble. Is there a way to set up a sequence of tasks and automate some of the operations mentioned above with just one line of code? How about reducing the number of ways that we can do this manually while also reducing the number of ways that choosing an environment certificate can lead to errors? There are! Fastlane can help.
fastlane
Fastlane is an automated build tool that includes testing, packaging, publishing, etc. It is internally implemented by Ruby and is a highly automated scripting tool.
The installation
Sudo gem install Fastlane sudo gem install Fastlane
Initialize the
In the project root directory (the same as your.xcworkspace/Podfile), initialize Fastlane: Fastlane init
After initialization we see a Fastlane folder and a Gemfile file generated in the root directory
-fastlane-fastfile (Core file, mainly used for command line calls and processing specific processes, Lane as opposed to a method or function) -Appfile(stores information about developer accounts) -Gemfile(Gemfile is a file we created to describe dependencies between gems. Gem is a collection of Ruby code that provides us with calls)Copy the code
This completes the first step, initialization of FastLans. Open Fastfile and you can see the core fastlane file.
default_platform(:ios)
platform :ios do
desc "Description of what the lane does"
lane :custom_lane do
# add actions here: https://docs.fastlane.tools/actions
end
end
Copy the code
To perform this custom task, simply type fastlane custom_lane on the command line. But of course since we didn’t set anything, it didn’t have any effect.
Fastfile file writing
Like our iOS ViewController, Fastlane has a life cycle. The life cycle of Fastlane is
Execution order | The method name | instructions |
---|---|---|
1 | before_all | Lane is executed only once before being executed |
2 | before_each | Each execution of lane is preceded by one |
3 | lane | Custom tasks |
4 | after_each | Lane is executed after each execution |
5 | after_all | Execute once after successful execution of lane |
6 | error | Any environment that reports an error during execution will be aborted and executed once |
lane
Lane is a method definition tag in FastFile and can be understood as a function. Fastlane is based on Ruby, so FastFile uses Ruby syntax as well.
Define a simple no-parameter lane
lane :noParam_lane
puts "This is a lane with no parameters."
end
Copy the code
Call the method fastlane noParam_lane
Define a lane with parameters. In FastFile option is like a dictionary set. We can get value from option[: Configuration]
lane :param_lane do |option|
param = option[:param]
puts "This is a lane with parameters."
puts param
end
Copy the code
Call method param_lane(param: ‘I am parameter ‘)
Action
In addition to our custom fastFile methods, Fastlane also provides a number of separate libraries of written methods called Actions. Common Actions can be referred to by Actions or by calling the Fastlane code Fastlane Actions
For example,
Let’s take a simple packing example
APP_NAME = "XXX"
SCHEME = "XXX"
platform :ios do
Run cocoapods once before executing Lane
before_all do
cocoapods
end
# the debug packages
lane :iosDebug do
package(configuration: "Debug",method:"development")
end
# release package
lane :iosRelease do
package(configuration: "Release",method:"ad-hoc")
end
# distribution package
lane :iosAppStore do
package(configuration: "Release",method:"app-store"")
end
#打包函数
lane :package do |option|
#打包
gym(
scheme: "#{SCHEME}",
export_method: option[:method],
configuration: option[:configuration],
include_symbols: true,
include_bitcode: false,
export_xcargs: "-allowProvisioningUpdates"
)
end
Copy the code
First of all, we define several parameters globally in Fastfile to store common parameters such as application name and SCHEME respectively. In the previous chapter we mentioned that there is a before_all in the fastlane lifecycle (executing lane only once before executing lane), and we update cocoaPods in this method first. Then, three lanes were defined, corresponding to the three packaging processes of test service delivery, formal service delivery and appstore uploading respectively in our development process. In the flow, the parameters of Configuration and Method are passed to achieve the effect of configuring different packaging environments.
switch lane
We can see from the code that iosDebug, iosRelease and iosAppStore all call a common Lane package at last. This use of calling one lane from another lane will be named Switch Lane
gym
Gym is actually an alias for ActionBuild_app. According to his official statement, using gym to build IPA files is 30% faster than other building tools.
There are many parameters of gym. Let’s take some commonly used ones and talk about them
Parameter name | | explanation default value | | | : — — — — — — — — : | : — — — — — : | : – : | | workspace | | to the workspace file | | project | | to the project file | | scheme | to pack a scheme for the project | | | clean | Would you like me to clean it before I pack it? The default value is not clean) | false | | output_directory | packaged after export directory | * (root) | | output_name | export ipa name | | | configuration | | packaged configuration “Release” | | include_symbols | ipa file should contain a symbol table | | | include_bitcode | ipa file should contain a include_bitcode | | | export_method | Export the RMS, the method of RMS include: app store, AD hoc, package, enterprise, development, developer – id | | other detailed don’t commonly used don’t copy and paste the everyone can see build_app here.
Perform packaging
With the encapsulation of the packaging method above, we just need to adapt to different packaging requirements. The test server sends a test call iosDebug. Official service release call iosRelease; So if you go to the appstore package and you go to the iosAppStore and you get the packages that you want for different environments.
Upload dandelions
In the process of testing, we also upload the operation of software distribution background, fastlane also helped us package this function as a plug-in. We just need to in the terminal, enter the following command, you can install dandelion fastlane plug-in. fastlane add_plugin pgyer
After the installation, we can add the corresponding command in the lane that needs to be uploaded to dandelion in Fastfile, and then upload dandelion directly after obtaining the IPA package
# the debug packages
lane :iosDebug do
package(configuration: "Debug",method:"development")
pgyer(api_key: "7f15xxxxxxxxxxxxxxxxxx141", user_key: "4a5bcxxxxxxxxxxxxxxx3a9e")
end
# release package
lane :iosRelease do
package(configuration: "Release",method:"ad-hoc")
pgyer(api_key: "7f15xxxxxxxxxxxxxxxxxx141", user_key: "4a5bcxxxxxxxxxxxxxxx3a9e")
end
Copy the code
PS: Api_key and user_key are obtained in the background of the dandelion.
Finally, call lane:
expand
One exciting news: Fastlane support for Swift writing!! Swift is fast becoming the world’s best language! In fact, the usage is similar to the Ruby version, I also wrote according to the documentation, if you are familiar with Swift code writing peers should be very easy to use. But this Fastlane. swift is just a Ruby based wrapper, not a native Swift. But it’s enough to get iOS developers excited. However, the current version of Bate does not support plug-ins, so operations like uploading to Dandelion mentioned above are not yet available at Fastlane.Swift. So the utility is still not comparable to the ruby native version. Maybe fastlane.Swift will fix this one day, and I’ll add swift. Cheer up Fastlane developers! Look forward to the official release!
References:
Fastlane/Jenkins: Continuous Delivery system for one-person teams
Fastlane website
Fastlane (I) : Usage