Last active
March 1, 2021 23:29
-
-
Save joshdholtz/f1e3ddb4eef8f86b48c07a5f77f50832 to your computer and use it in GitHub Desktop.
Execute fastlane for multiple schemes based off of ".env.scheme_" dotenv files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fastlane do_task | |
# fastlane do_task envs:"scheme_1,scheme_2" | |
lane :do_task do |options| | |
# Execute this lane for all schemes if no | |
# 'SCHEME` environment variable | |
if ENV['SCHEME'] == nil | |
envs = options[:envs].to_s.split(",") | |
execute_for_all_scheme_envs(envs) { do_task } | |
next | |
end | |
puts "DO TASK FOR #{ENV['SCHEME']}" | |
end | |
# Execute a block of code for all schemes | |
# Scheme list is generated by file prefixed with ".env.scheme_" | |
# Example: .env.scheme_dev, .env.scheme_stg, .env.scheme_prod | |
# | |
# Note: Each execution will reset environment variables | |
# and lane context after execution. Handle specific lane context | |
# values in your execution block as they will be cleared | |
# after block execution | |
def execute_for_all_scheme_envs(envs) | |
# Environment and lane context cache for resetting | |
env_cache = ENV.to_hash | |
context_cache = Actions.lane_context.clone | |
schemeList = Dir.glob(".env.scheme*") | |
# Select only given envs | |
if envs && !envs.empty? | |
envs.map! { |e| ".env.#{e}" } | |
schemeList.select! { |s| envs.include?(s) } | |
end | |
schemeList.each do |file| | |
# Overloading environment for scheme | |
Dotenv.overload(file) | |
# Execute only if scheme | |
if ENV["SCHEME"] | |
yield | |
else | |
UI.important("No 'SCHEME' env for #{file} - Skipping...") | |
end | |
# Reset environment variables | |
ENV.clear | |
ENV.update(env_cache) | |
# Reset lane context | |
Actions.lane_context.clear | |
Actions.lane_context.update(context_cache) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment