Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save minikin/e2ff0dc74d59125676b9f979cdab9e15 to your computer and use it in GitHub Desktop.
Save minikin/e2ff0dc74d59125676b9f979cdab9e15 to your computer and use it in GitHub Desktop.
Mostly Automated Way to Update FASTLANE_SESSION

fastlane/fastlane#13833

This script may be useful to you if you use fastlane for iOS CI builds and your Apple developer portal account uses 2-factor authentication. There is reason to suppose that Apple may not be persisting these sessions for as long as they used to, and hence you might find yourself needing to update FASTLANE_SESSION for your CI projects more often than usual.

This script is a way to mostly automate the process of updating FASTLANE_SESSION if you use CircleCI as your CI provider. It expects to find your Apple username in env as FASTLANE_USER, your Apple password as FASTLANE_PASSWORD, and your CircleCI API token as CIRCLE_API_TOKEN. It then executes fastlane spaceauth and waits for you to put in your 2FA code. From there, it parses out the session data and then uses the CircleCI API to populate the specified projects with the newly updated FASTLANE_SESSION. You'll of course need to enter the names of your actual projects, as well as your actual GitHub username/org.

source "https://rubygems.org"
gem "circleci"
gem "fastlane", "~> 2.109.0"
#!/usr/bin/env ruby
require "circleci"
# yell and quit if FASTLANE_USER, FASTLANE_PASSWORD, and CIRCLE_API_TOKEN are not present
# in env.
unless ENV.key?('FASTLANE_USER') && ENV.key?('FASTLANE_PASSWORD') && ENV.key?('CIRCLE_API_TOKEN')
puts "Please set FASTLANE_USER, FASTLANE_PASSWORD and CIRCLE_API_TOKEN env vars."
exit 1
end
# do the spaceauth from here and capture the output
# Yes, backticks are insecure but SafeShell won't allow for
# waiting for the 2FA input. Besides, this isn't production code.
puts "When/if the script pauses, type the 2FA code and press Enter."
spaceauth_output = `bundle exec fastlane spaceauth`
# regex the output for the value we need
fastlane_session_regex = %r{Pass the following via the FASTLANE_SESSION environment variable:\n(?<session>.+)\n\n\nExample:\n.+}
new_session = nil
if match = spaceauth_output.match(fastlane_session_regex)
# Strip out the fancy formatting
new_session = match[:session].gsub("\e[4m\e[36m", "").gsub("\e[0m\e[0m", "")
end
# Yell and quit if unable to parse out session from spaceauth output
if new_session.nil?
puts "Unable to obtain new session via fastlane spaceauth"
exit 1
else
# Update FASTLANE_SESSION env var for each specified CircleCI project
# https://circleci.com/docs/api/#add-environment-variables
puts "Updating FASTLANE_SESSION for iOS CircleCI projects"
projects = %w[project1 project2]
env_hash = { name: 'FASTLANE_SESSION', value: new_session }
config = CircleCi::Config.new token: ENV['CIRCLE_API_TOKEN']
projects.each do |p|
project = CircleCi::Project.new("YourUserOrOrg", p, "github", config)
response = project.add_envvar env_hash
puts "Response for #{p}: #{response.body}"
end
puts "Done!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment