Integrate Fastlane to iOS project: Lesson 5

Create environment file to manage different environments setting

Mark
3 min readSep 4, 2018

Last lesson, we demonstrated how to build the app and upload the ipa, dSYM to Crashlytics and Hockey App. This time, we will handle the multiple environments by using .env file.

Basically, there are 2 ways to configure your environment values.

Delivery the app by using different lane

lane :build_app_dev do

get_certificates(
// ...
)

get_provisioning_profile(
// ...
)
build_app(
// ...
)
crashlytics(
// ...
)

upload_symbols_to_crashlytics(
// ...
)
hockey(
// ...
)
end
lane :build_app_testing do
// copy the actions inside build_app_dev and update the value
end

After that, you can run bundle exec fastlane ios build_app_dev and bundle exec fastlane ios build_app_testing to build the app by different environment setting.

This method is more strictly forward, it just create another lane and specify the value in different lane. There are several drawbacks if you use this approaches.

  1. decrease Fastfile readability, you are hard to read what the lane exactly do and the flow
  2. easy to make mistakes because you need to update all of the parameters or action inside the lane
  3. longer and longer code….

Fastlane does understand this questions, it introduces the .env file to handle that.

Create your project environment file

  1. Go to /<project_directory>/fastlane
  2. create the generic env file, named as .env
  3. create the environment specified files, named as .env_<env_name>_<xcode_configuration>, e.g. .env.dev_adhoc and env.testing_adhoc

.env - use to put the share environment file

.env.dev_adhoc - use to put the dev environment value with Xcode adhoc configuration

.env.testing_adhoc - use to put the testing environment value with Xcode adhoc configuration

Before we move the value from fastfile to our environment, we need to know how environment match to our fastfile

Type fastaction action get_certificates

Output of fastlane action get_certificates

If the action supports the environment variable, it will be shown when you type fastlane action. You can check the other actions by typing this command.

Update the files as following.

fastfile

lane :build_app do

// create the build
get_certificates()
get_provisioning_profile()
build_app()
// upload to Crashlytics
crashlytics()
upload_symbols_to_crashlytics()
// upload to Hockey App
hockey()

end

.env

// empty, all the variables are set in other env files

.env.dev_adhoc

CERT_DEVELOPMENT = false 
CERT_FORCE = false
CERT_USERNAME = <your_apple_id>
CERT_TEAM_NAME = <your_team_name
CERT_FILE_NAME = <you_file_name>
CERT_OUTPUT_PATH = fastlane_delivery/dev/adhoc/
SIGH_AD_HOC = true,
SIGH_APP_IDENTIFIER = <your_app_id>,
SIGH_USERNAME = <your_apple_id>,
SIGH_OUTPUT_PATH = fastlane_delivery/dev/adhoc
SIGH_PROVISIONING_PROFILE_NAME = <your_provisioning_proflie_name>
GYM_SCHEME = Bookshop_dev_adhoc
GYM_WORKSPACE = Bookshop.xcworkspace
GYM_CLEAN = true
GYM_CONFIGURATION = AdHoc
GYM_BUILD_PATH = fastlane_delivery/dev/adhoc/
GYM_ARCHIVE_PATH = fastlane_delivery/dev/adhoc/bookshop_dev_adhoc
GYM_DERIVED_DATA_PATH = fastlane_delivery/dev/adhoc/
GYM_OUTPUT_DIRECTORY = fastlane_delivery/dev/adhoc/
CRASHLYTICS_API_TOKEN = <your_api_token>
CRASHLYTICS_BUILD_SECRET =<your_build_secret>
CRASHLYTICS_IPA_PATH = fast lane_delivery/dev/adhoc/Bookshop_dev_adhoc.ipa
CRASHLYTICS_NOTES = <your_release_note>
CRASHLYTICS_NOTES_PATH = <your_release_note_path>
CRASHLYTICS_EMAILS = <tester email, separated by commas>
CRASHLYTICS_GROUPS = <crashlytics_beta_group_name, separated by commas>
CRASHLYTICS_NOTIFICATIONS = <true or false>
FL_UPLOAD_SYMBOLS_TO_CRASHLYTICS_DSYM_PATH = ./fastlane_delivery/dev/adhoc/Bookshop_dev_adhoc.app.dSYM.zip
CRASHLYTICS_API_TOKEN = <your_api_token>
FL_UPLOAD_SYMBOLS_TO_CRASHLYTICS_PLATFORM = ios
FL_HOCKEY_API_TOKEN = <your_api_token>
FL_HOCKEY_IPA = fastlane_delivery/dev/adhoc/Bookshop_dev_adhoc.ipa
FL_HOCKEY_DSYM = ./fastlane_delivery/dev/adhoc/Bookshop_dev_adhoc.app.dSYM.zip

.env.testing_adhoc

CERT_DEVELOPMENT = false 
CERT_FORCE = false
CERT_USERNAME = <your_apple_id>
CERT_TEAM_NAME = <your_team_name
CERT_FILE_NAME = <you_file_name>
CERT_OUTPUT_PATH = fastlane_delivery/testing/adhoc/
SIGH_AD_HOC = true,
SIGH_APP_IDENTIFIER = <your_app_id>,
SIGH_USERNAME = <your_apple_id>,
SIGH_OUTPUT_PATH = fastlane_delivery/testing/adhoc
SIGH_PROVISIONING_PROFILE_NAME = <your_provisioning_proflie_name>
GYM_SCHEME = Bookshop_testing_adhoc
GYM_WORKSPACE = Bookshop.xcworkspace
GYM_CLEAN = true
GYM_CONFIGURATION = AdHoc
GYM_BUILD_PATH = fastlane_delivery/testing/adhoc/
GYM_ARCHIVE_PATH = fastlane_delivery/testing/adhoc/bookshop_testing_adhoc
GYM_DERIVED_DATA_PATH = fastlane_delivery/testing/adhoc/
GYM_OUTPUT_DIRECTORY = fastlane_delivery/testing/adhoc/
CRASHLYTICS_API_TOKEN = <your_api_token>
CRASHLYTICS_BUILD_SECRET =<your_build_secret>
CRASHLYTICS_IPA_PATH = fast lane_delivery/testing/adhoc/Bookshop_testing_adhoc.ipa
CRASHLYTICS_NOTES = <your_release_note>
CRASHLYTICS_NOTES_PATH = <your_release_note_path>
CRASHLYTICS_EMAILS = <tester email, separated by commas>
CRASHLYTICS_GROUPS = <crashlytics_beta_group_name, separated by commas>
CRASHLYTICS_NOTIFICATIONS = <true or false>
FL_UPLOAD_SYMBOLS_TO_CRASHLYTICS_DSYM_PATH = ./fastlane_delivery/testing/adhoc/Bookshop_testing_adhoc.app.dSYM.zip
CRASHLYTICS_API_TOKEN = <your_api_token>
FL_UPLOAD_SYMBOLS_TO_CRASHLYTICS_PLATFORM = ios
FL_HOCKEY_API_TOKEN = <your_api_token>
FL_HOCKEY_IPA = fastlane_delivery/testing/adhoc/Bookshop_testing_adhoc.ipa
FL_HOCKEY_DSYM = ./fastlane_delivery/testing/adhoc/Bookshop_testing_adhoc.app.dSYM.zip

By using environment file, it is easy to understand how the lane to do what actions. When there is a new environment set up, it only needs to create a new environment file without update the fastfile.

Now, run bundle exec fastlane ios build_app --env dev_adhoc or bundle exec fastlane ios build_app --env dev_testing to build the app by different environment setting.

What’s next?

Next lesson, we will introduce Fastlane match to manage the iOS certificates and provisioning profile without manual handle.

--

--