Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active September 27, 2024 08:50
Show Gist options
  • 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

icodebuster commented Aug 12, 2019

my .env.secret is not getting loaded. The .env.secret file is added inside the fastlane folder.
If I put the same values in .env everything is working as expected.

@joshdholtz
Copy link
Author

my .env.secret is not getting loaded. The .env.secret file is added inside the fastlane folder.
If I put the same values in .env everything is working as expected.

Did you add the following to the top of your Fastfile?

before_all do
  Dotenv.overload '.env.secret'
end

@icodebuster
Copy link

icodebuster commented Aug 12, 2019

Yes, here is my code

fastlane_require 'dotenv'

before_all do
    Dotenv.overload '.env.secret'
    puts Dotenv.overload
end

When I am printing the Dotenv.overload all the values in .env file is correctly getting printed.

Below is the .env.secret file.

APPLE_ID = "[email protected]"
TEAM_ID = "ABCDEF"
MATCH_PASSWORD = "abcdef@123"
FABRIC_API_KEY = "abcdef12345"
FABRIC_BUILD_SECRET = "abcdefghi12345678"

When I run this line puts Dotenv.overload '.env.secret' is prints the data correctly.

@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