rogerluan / arkana

Use dotenv files for Android and iOS projects.
BSD 2-Clause "Simplified" License
380 stars 18 forks source link

Issues with Xcode Build Phases Script #17

Closed jhonny-me closed 2 years ago

jhonny-me commented 2 years ago

Hi, I find arkana very useful and was successfully to switch between different environment sets.

But when I try to integrate this to Xcode Build Phase:

tmp=${CONFIGURATION#*(}   # remove prefix ending in "("
env=${tmp%)*}   # remove suffix starting with ")"

bundle exec arkana -d ".env.${env}"

I get ERROR /Library/Ruby/Site/2.6.0/rubygems.rb:265:infind_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)`

Here is the output of gem environment also in Build Phase:


Showing Recent Messages
RubyGems Environment: - RUBYGEMS VERSION: 3.3.20 - RUBY VERSION: 2.6.10 (2022-04-12 patchlevel 210) [universal.arm64e-darwin22] - INSTALLATION DIRECTORY: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0 - USER INSTALLATION DIRECTORY: /Users/johnny/.gem/ruby/2.6.0 - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby - GIT EXECUTABLE: /Applications/Xcode.app/Contents/Developer/usr/bin/git - EXECUTABLE DIRECTORY: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin - SPEC CACHE DIRECTORY: /Users/johnny/.gem/specs - SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site - RUBYGEMS PLATFORMS: - ruby - universal-darwin-22 - GEM PATHS: - /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0 - /Users/johnny/.gem/ruby/2.6.0 - GEM CONFIGURATION: - :update_sources => true - :verbose => true - :backtrace => true - :bulk_threshold => 1000 - REMOTE SOURCES: - https://rubygems.org/ - SHELL PATH: - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/appleinternal/bin - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/bin - /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/libexec - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/bin - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/appleinternal/bin - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/local/bin - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin - /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/local/bin - /Applications/Xcode.app/Contents/Developer/usr/bin - /Applications/Xcode.app/Contents/Developer/usr/local/bin - /usr/local/bin - /usr/bin - /bin - /usr/sbin - /sbin

Im on MacOS 13.0 Xcode 14.0

rogerluan commented 2 years ago

Hi @jhonny-me 👋

I strongly advise you not to integrate it with Xcode Build Phase (and this is a super valid point I should raise in the README file, thanks for this)

There are two main reasons for this:

  1. Xcode doesn't play well with bundler in general. If you've ever had to deal with any other Run Script in your Build Phase that used any other bundle-powered gem (like rake, fastlane, etc), you'll have those same issues. It's not easy to workaround those issues, and they're unstable, and often environment-specific (meaning not friendly with a larger team, specially if your team uses different setups like different shell versions, ruby version managers, etc).
  2. Arkana encodes your secrets every single time it's run, which means it'll generate new Swift code on every run. If you plug this in in your Build Phase, this means all the builds will have a wider set of changes than they should, thus making your build time slower, because it'd be busting more cache than it really needed.

Unfortunately, this means Arkana must be run outside of your normal build pipeline, when using Xcode UI. When building programmatically this can be easily changed to simply adding a step to your scripts to run Arkana before any build_app in fastlane or xcodebuild.

I hope you find this information useful! Let me know if you have more questions, happy to answer them 😊

jhonny-me commented 2 years ago

Hi, @rogerluan thanks for the quick response. Not sure about your refer to Xcode UI, but here is my setup, the arkana command is in the Generate Keys script, which is required since I need to update the output according to input CONFIGURATION at each run from Xcode, or do you suggest another way to implement?

(I was on a existing project and migrating from a self-implemented openssl bash script to Arkana)

AdidasConfirmed_—_AdidasConfirmed_xcodeproj
rogerluan commented 2 years ago

By Xcode UI I mean just the regular Xcode.app :) (I just wasn't sure if you were building via CLI e.g. fastlane, xcodebuild, etc)

I need to update the output according to input CONFIGURATION at each run from Xcode […] or do you suggest another way to implement?

There are 3 paths here:

  1. If this is a non-negotiable requirement of your project (or DevX - Developer Experience), you might be better off keeping the old logic you had in place.
  2. Manually run bundle arkana …etc in Terminal before running your builds in Xcode.
  3. Attempt to fix the bundler issue you're seeing.

If you really want to attempt to actually fix the bundler issues, this is one of the solutions you can do: update your shell script inside that Generate Keys to something like:

source ~/.zshrc # <-- this is the key

tmp=${CONFIGURATION#*(}   # remove prefix ending in "("
env=${tmp%)*}   # remove suffix starting with ")"

bundle exec arkana -d ".env.${env}"

However, do note that this makes a bunch of assumptions, like that the device running that build uses zsh (default in macOS since Catalina but still a variable), and that the ~/.zshrc will have the right commands to set up your preferred Ruby Version Manager (e.g. set the PATH correctly, startup rvm or rbenv or your other Ruby Version Manager).

This is a flaky setup, I don't recommend it. But it could work out for you and your team.

jhonny-me commented 2 years ago

Yeas, it worked for my case. Much appreciated @rogerluan.

I'm going to close this one.