Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active December 26, 2023 13:31
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save joshdholtz/cd077ccdddb608af00eb2466259e7ac0 to your computer and use it in GitHub Desktop.
Save joshdholtz/cd077ccdddb608af00eb2466259e7ac0 to your computer and use it in GitHub Desktop.
Using Dotenv and environment variables with fastlane
STUFF = this is some stuff
CONFIG = this is config 1
CONFIG = this is config 2
SOME_API_TOKEN = 123456789
fastlane_require 'dotenv'
before_all do
Dotenv.overload '.env.secret'
end
lane :test do
# Loaded from .env (by fastlane)
puts "STUFF: #{ENV['STUFF']}"
# Loaded from .env.secret (by us in before_all)
puts "SOME_API_TOKEN: #{ENV['SOME_API_TOKEN']}"
# Loaded from .env.(<env>) where <env> is passed in from `fastlane test --env <env>` (by fastlane)
# Ex: `fastlane test --env config` loads .env.config1
puts "CONFIG: #{ENV['CONFIG']}"
end
fastlane_require 'dotenv'
before_all do
Dotenv.overload '.env.secret'
end
$: fastlane test
STUFF: this is some stuff
SOME_API_TOKEN: 123456789
CONFIG:
$: fastlane test --env config1
STUFF: this is some stuff
SOME_API_TOKEN: 123456789
CONFIG: this is config 1
$: fastlane test --env config2
STUFF: this is some stuff
SOME_API_TOKEN: 123456789
CONFIG: this is config 2
@icodebuster
Copy link

Finally its working, here is my code

    # Global Variables
    fabric_api_key = nil
    fabric_build_secret = nil

platform :ios do
    before_all do
        Dotenv.overload('.env', '.env.secret')

        fabric_api_key = ENV['FABRIC_API_KEY']
        fabric_build_secret	= ENV['FABRIC_BUILD_SECRET']
   end
end

The issue was in the way I was declaring my variable. If you are directly going to use ENV[] then everything is working as expected.

@icodebuster
Copy link

Is there a better way of declaring global variables or should I be using ENV[] directly whenever I am accessing environment variables, Any suggestions, thank you.

@joshdholtz
Copy link
Author

Woot, glad you got it working! Using .env files are the best way to go. I only access them through ENV[] in special occasions. The options in fastlane actions have environment variables names that they are associated with so if you are using actions you should be able to just name them as they are defined in the docs and then let all my magic happen from there 🙃

@mgrachev
Copy link

mgrachev commented Jul 7, 2020

In addition to using environment variables, I can recommend the tool dotenv-linter - it’s a lightning-fast linter for .env files. Written in Rust. Maybe it would be useful for you.

@kambala-decapitator
Copy link

@mgrachev FYI you have extra characters in the URL that prevent it from opening directly

@mgrachev
Copy link

@mgrachev FYI you have extra characters in the URL that prevent it from opening directly

Thanks! Fixed.

@endrits079
Copy link

Is it possible to load env files outside of fastlane directory?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment