Skip to content

Instantly share code, notes, and snippets.

@joshdholtz

joshdholtz/.env

Last active Nov 13, 2020
Embed
What would you like to do?
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
.env.secret
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

This comment has been minimized.

Copy link

@icodebuster 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

This comment has been minimized.

Copy link
Owner Author

@joshdholtz joshdholtz 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.

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

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

This comment has been minimized.

Copy link

@icodebuster 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 = "example@example.com"
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

This comment has been minimized.

Copy link

@icodebuster icodebuster commented Aug 12, 2019

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

This comment has been minimized.

Copy link

@icodebuster icodebuster commented Aug 12, 2019

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

This comment has been minimized.

Copy link
Owner Author

@joshdholtz joshdholtz commented Aug 12, 2019

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

This comment has been minimized.

Copy link

@mgrachev mgrachev commented Jul 7, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.