Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added environments with dotenv #28

Merged
merged 3 commits into from Feb 28, 2015
Merged

Added environments with dotenv #28

merged 3 commits into from Feb 28, 2015

Conversation

joshdholtz
Copy link
Member

This MIGHT fix #8 in a more manual way

Summary

  • Added optional lane parameter onto before_all block
  • Added --env option onto CLI - fastlane beta --env development
    • Storing env in Actions.lane_context[ Actions::SharedValues::ENVIRONMENT ]}
  • Added tests for this and they passed 🎊 🎉

Why

  • Configuration for different environments (dev, staging, prod)
  • before_all was jealous that after_all got more information

How I Use It

  • I have add "dotenv" into my project with 4 different .env files

    • .env
      • This env contains anything NON specific to my environments
      • Ex: WORKSPACE, SLACK_URL
    • .env.development
      • This would contain my development SCHEME, HOCKEYAPP_API_TOKEN, APP_IDENTIFIER, APPLE_ID
    • .env.staging
      • This would contain my staging SCHEME, HOCKEYAPP_API_TOKEN, APP_IDENTIFIER, APPLE_ID
    • .env.production
      • This would contain my production SCHEME, HOCKEYAPP_API_TOKEN, APP_IDENTIFIER, APPLE_ID
  • I modified my Appfile to use my environment variables so they can be determined by my sublane

    • Appfile
    app_identifier ENV['APP_IDENTIFIER']
    apple_id ENV['APPLE_ID']
    

Example

require 'dotenv'

before_all do |lane|
  puts "#{lane} for environment `#{Actions.lane_context[ Actions::SharedValues::ENVIRONMENT ]}`"
  raise "No scheme detected" unless ENV['SCHEME']

  # Increments build number of your project
  increment_build_number

  # Makes sure all cocoapods are up to date
  cocoapods

  # Runs unit tests
  # - argumnets passed as array of strings
  # - Docs on xctool (https://github.com/facebook/xctool)
  xctool ["-workspace #{ENV['WORKSPACE']}", "-scheme #{ENV['SCHEME']}"]

end

@joshdholtz
Copy link
Member Author

⚠️ Adding some new awesome stuff ⚠️ Please hold off from reviewing yet 😇

@KrauseFx
Copy link
Member

KrauseFx commented Feb 4, 2015

Ha, I love your pull request summaries, thank you!

Looking forward to what you're preparing right now 😄

@joshdholtz
Copy link
Member Author

@KrauseFx I think it's ready for you!! 💵 💰 💸 Chat me up if you have any questions on anything

@joshdholtz joshdholtz changed the title Added optional lane onto before_all block Added sublanes Feb 4, 2015
@joshdholtz
Copy link
Member Author

@KrauseFx Updated! fastlane beta__development is now the same thing as fastlane beta:development - its easier to type 😊

@KrauseFx
Copy link
Member

KrauseFx commented Feb 8, 2015

Hey @joshdholtz, thanks for this great pull request.

Why limit it to one sublane?

How is it different from just defining a number of lanes when running fastlane (https://github.com/KrauseFx/fastlane#run-multiple-lanes?

Just want to understand the use cases for this and how we can use it 😃

@carlosefonseca
Copy link
Contributor

Hi! I've been playing with @joshdholtz's code and I got it to work in a way that solves the problem raised in #8. In my company, we also use the same codebase with multiple targets that generate multiple apps.

Using @joshdholtz change, I can create .env files for each app and make fastlane load the correct one and run the already defined lanes. Without this, I think I had to set env vars before calling fastlane or duplicate each lane for each app (this way I could use the multiple lanes feature and do fastlane deploy-App1 deploy-App2 but duplicating lane code isn't great).

Although @joshdholtz's code works, I think this feature should be a bit more integrated. It creates the concept of sublanes but then it's just an additional argument for before_all and one still has to load the correct .env files. It would be nicer if fastlane could load them under the hood when specifying some kind of suffix to the lane.

I built scripts for Android that do essentially the same, deploy code loads common config values and overrides them with specific config values for the apps.

Currently, using @joshdholtz's patch, my Fastfile is loading the .env* file specified as the sublane:

before_all do |lane, sublane|

  envfile = '.env.'+sublane.to_s
  raise "No env file for "+sublane.to_s unless File.exist? envfile

  Dotenv.load('.env')
  Dotenv.load(envfile)

  raise "No scheme detected" unless ENV['SCHEME']
  ENV['SNAPSHOT_SCHEME'] = ENV['SCHEME']
end

lane :snapshots do 
  snapshot
end

Then I call fastlane snapshots:App1 and it loads .env, .env.app1 and takes some screenshots of App1.

There are still some hiccups, like, snapshot :noclean doesn't work because it might install a previous app, but I can live with that. Also, if I call fastlane snapshots:App1 snapshots:App2 the second run still uses the envs from the first one. before_all logs the loading of the second .env file but snapshot uses the scheme set on the first…

Cheers

@KrauseFx
Copy link
Member

KrauseFx commented Feb 8, 2015

Thanks @carlosefonseca!

There are still some hiccups, like, snapshot :noclean doesn't work because it might install a previous app, but I can live with that

What are you referring to? Is there an issue with the snapshot integration?

I'll go through the other points later today 👍

@joshdholtz
Copy link
Member Author

@carlosefonseca Thanks for giving this PR a try 😁 I originally did not integrate in the dotenv any further as I wasn't sure if that is hope other people were going to use it (besides me). But since it seems like that may be a common configuration, I will auto load a .env files that matches the sublane.

Example: fastlane beta:development will load .env.development and fastlane beta:production will load .env.production (along with the default .env file for any configuration that is common among all environments).

I just got home from a weekend a trip so I will add that in once I get into the office later today.

cc: @KrauseFx

@joshdholtz
Copy link
Member Author

@KrauseFx @carlosefonseca I updated this PR to autoload .env.<sublane_name> in the runner. I chose to do this in the runner since multiple lanes can be executed at a time. I use Dotenv.overload so that any variables that were already loaded will get overwritten (since it doesn't look like there is an unload method at all).

@KrauseFx I would still like to add more tests yet and I also need to add README stuff. It also looks like there will be a merge conflict so you will need to manually merge this since I do not have write access 😇

@joshdholtz
Copy link
Member Author

@KrauseFx The sublanes addition DOES offer some of the same functionality as running multiple lanes in a row.

Ex: fastlane config_dev beta or fastlane config_staging beta could be a way to configure different environment variables (like schemes or HockeyApp API keys).

However, with my latest additions, the .env files will be automatically loaded with the sublane used - fastlane beta:dev or fastlane beta:staging. The only thing the user would need to do is create a .env.dev and .env.staging email instead of having to also create a lane for config_dev and config_staging` lane

I am open to suggestions though if you would like this reworked 😁

@mathaeus
Copy link

Hi guys,

I've also been playing around with targets, sublanes and .env* files. Works quite well so far and thanks for the input!
There's two little problems I noticed though.

Environment variable for the screenshots path.

The first thing I noticed is that when I use an environment variable for the screenshots path, both in the Snapfile and the Deliverfile, like this:

screenshots_path ENV['SCREENSHOTS_PATH']

the reports_generator.rb complains at the following line:

 html = ERB.new(File.read(html_path)).result(binding) # http://www.rrn.dk/rubys-erb-templating-system

with the error:

in `block (3 levels) in generate': undefined method `[]' for nil:NilClass (NoMethodError)

Creating the screenshots works as expected. Just the HTML generator has a problem obviously. So far I couldn't resolve the issue. Maybe anyone has a hint.

Environment variable for the ipa path.

The second thing I noticed is that when I use an environment variable for the ipa path like this:

ipa do
    system("./build_ipa.sh")
    ENV['IPA_FILE'] 
end

the ipa_uploader.rb complains at the following line:

ipa_path.strip! # remove unused white spaces

I had to dup the string to 'unfreeze' it. Otherwise I got the error "Can't modify frozen string". See http://stackoverflow.com/a/10183477.

So the line looks like this now:

ipa_path.dup.strip! # remove unused white spaces

I could create a Pull Request, but I'm not sure if this occurs if you don't use sublanes and ENV variables.

@KrauseFx Please tell me how you feel about this.

@KrauseFx
Copy link
Member

@mathaeus these are all really good points! Unfreezing the string makes sense.
Regarding the screenshots path: I heard some users running into the same problem.

But both issues don't have anything to do with the sublanes, right?

@mathaeus
Copy link

@KrauseFx I'd say so. I create a PR for the unfreezing of the string.

The problem with the screenshots path is not tied to sublanes, no. But I guess it only occurs using ENV variables or did other users experience the same problem w/o ENV variables?

@KrauseFx
Copy link
Member

@mathaeus Actually I ran into this issue just today, when releasing the new version of fastlane, with built in ipa action 😄

@joshdholtz
Copy link
Member Author

@KrauseFx So this issue isn't related to sublanes, correct? 😇

Also, is the idea of sublanes and this implementation alright? If so, I will work on making changes to the README (unless you want to do that so you have more control).

@KrauseFx
Copy link
Member

@joshdholtz not related.

About the pull request: Loading the .env is great, but I'm not yet sure about sublanes. How are they different from just using ruby methods inside the Fastfile? Trying to understand the use cases.

@joshdholtz
Copy link
Member Author

@KrauseFx They are a bit cleaner to use than separate methods. They keep the Fastfile syntactically consistent.

Ex: I have a specific beta lane for production that goes to Apple TestFlight Beta and the catch all beta lane that goes to HockeyApp

lane :beta, :prod do
    # Does Apple TestFlight stuff
end

lane :beta do
    # Does hockey app stuff
end

Instead of adding another symbol into the lane, I could just change it to passing the sublane through the block as an argument.

I personally prefer the look of this over separating things out with methods but I am pretty biased 😉

Also, I have 5 targets and 3 environments in my project I created this for. This gave me a lot of flexibility for all my different builds.

Also note, this comment was done through my phone so sorry for any markdown errors as I couldn't preview 😬

@carlosefonseca
Copy link
Contributor

@joshdholtz @KrauseFx how about adding just a flag on fastlane instead of messing with the lanes? Something like fastlane --env app1 deploy? Multi-lane would only work with that environment, which isn't as great, but would allow the use of multiple environments while keeping the Fastfile clean…
I'd rather do fastlane --env app1 deploy && fastlane --env app2 deploy than nothing/dup'ing stuff inside Fastfile 😛

@joshdholtz
Copy link
Member Author

@carlosefonseca @KrauseFx I can get behind that. I will make that change.

@KrauseFx
Copy link
Member

You know, you can already run multiple lanes with fastlane:

fastlane test screenshots appstore

@carlosefonseca
Copy link
Contributor

@KrauseFx How about this… can I have a lane run another lane? Can I have a lane called beta-app1 set some (env) vars and then call lane beta and use those vars?

@joshdholtz joshdholtz changed the title Added sublanes Added environments with dotenv Feb 14, 2015
@joshdholtz
Copy link
Member Author

@KrauseFx @carlosefonseca Changed it up. The environment is now passed in through a CLI option. This makes this PR a lot simpler now.

Example:
fastlane beta --env development

Just so you know... 😊
As I would still like to use the environment specific lanes that I created originally, I made a fastlane decorator that does exactly that in which I will install in my own projects 😇 - https://github.com/joshdholtz/fastlane-env-lanes

@carlosefonseca
Copy link
Contributor

@joshdholtz Holy cow! That decorator seems works like a charm! Many thanks!

@joshdholtz
Copy link
Member Author

@carlosefonseca Glad you like it 😁

@dfranzi
Copy link

dfranzi commented Feb 26, 2015

Curious as to the status of this PR. I'm a bit torn myself on the utility of the sublanes, but the addition of dotEnv is very much desired. If there's further discussion about the setup of sublanes, would it be worth splitting this PR into two - one for dotEnv and one for continued work on sublane integration?

@joshdholtz
Copy link
Member Author

@dfranzi The sublanes are completely removed from this PR. This PR only contains the dotenv stuff.

If you want to use dotenv and environment specific lanes until this PR gets merge, you can use my gem to extend this functionality on top of fastlane - https://github.com/joshdholtz/fastlane-env-lanes

@dfranzi
Copy link

dfranzi commented Feb 26, 2015

Thanks for the heads up! That works in the interim.

@joshdholtz
Copy link
Member Author

@dfranzi No problem! That is the reason why I made it 😁

rokkincat added 2 commits February 27, 2015 22:16
Added sublanes

Can call sublane with : now, along with __

Moved common things to helpers and will now load dotenv with sublane name

Makes sure default env files do get loaded

Dotenv was not loading out of the fastfile directory... so I fixed that

Removed sublane and added env option

Moved up setting of ENV

Updated gemfile
@joshdholtz
Copy link
Member Author

@KrauseFx Rebased from master and squashed 8 commits together because there was a lot of back and forth with the sublanes (which no longer exist).

This now only contains..

  1. Passing in lane into the "before"
  2. Loading of environment variables with dotenv

I will now be adding in some README stuff

@joshdholtz
Copy link
Member Author

@KrauseFx I updated the README now. You can view it at https://github.com/joshdholtz/fastlane/blob/before-with-lane/README.md

KrauseFx added a commit that referenced this pull request Feb 28, 2015
@KrauseFx KrauseFx merged commit d9c5da2 into fastlane:master Feb 28, 2015
@KrauseFx
Copy link
Member

Perfect, thank you so much for this @joshdholtz 🎉

I'll update the README to also have the lane_name as parameter.

KrauseFx pushed a commit that referenced this pull request Mar 7, 2016
KrauseFx pushed a commit that referenced this pull request Mar 7, 2016
@fastlane fastlane locked and limited conversation to collaborators Feb 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiple target support
5 participants