Skip to content

Instantly share code, notes, and snippets.

@johnkodes
Forked from mcany/Fastfile-2-example
Created April 29, 2023 10:19
Show Gist options
  • Save johnkodes/098f66570a2710bf7be2e08afd0477e3 to your computer and use it in GitHub Desktop.
Save johnkodes/098f66570a2710bf7be2e08afd0477e3 to your computer and use it in GitHub Desktop.
# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Actions.md
# can also be listed using the `fastlane actions` command
# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`
# If you want to automatically update fastlane if a new version is available:
# update_fastlane
# This is the minimum version number required.
# Update this, if you use features of a newer version
require 'active_support/time'
fastlane_version "1.103.0"
default_platform :ios
platform :ios do
before_all do
ensure_git_status_clean
clear_derived_data
FileUtils::mkdir_p '../build'
sh "git log --pretty=format:'+ %s' -n 14 | grep -v 'Merge\\|release\\|Update circle\\|Update build' > ../build/change.log"
ENV['KEYCHAIN_NAME'] = Helper.is_ci? ? "app.keychain" : "login.keychain"
end
after_all do |lane|
clean_build_artifacts
sh "cd .. && ./scripts/reset_appicons.sh"
notification(
title: "Success",
subtitle: "Fastlane",
message: "Fastlane completed executing lane: '#{lane}'"
) unless Helper.is_ci?
build_number = ENV["BUILD_NUMBER"]
if build_number
#slack(message: "<APP_SCHEME> iOS: build " + build_number + " is ready.")
end
end
error do |lane, exception|
clean_build_artifacts
sh "cd .. && ./scripts/reset_appicons.sh"
notification(
title: "Error",
subtitle: "Fastlane",
message: "Fastlane encountered an error while executing lane: '#{lane}'"
) unless Helper.is_ci?
end
desc "Install all certificates and provisioning profiles"
lane :install_all_certificates do
types = ["development", "adhoc", "appstore"]
envs = ["sandbox", "production"]
for type in types
shouldForce = type != "appstore"
for env in envs
certificates(
type: type,
force: shouldForce,
env: env
)
end
end
end
desc "Install certificates and profiles for specified environment and type"
lane :certificates do |options|
env = if options.key?(:env)
options[:env]
else
prompt_for_environment
end
type = if options.key?(:type)
options[:type]
else
prompt_for_type(context: "certificate")
end
id_prefix = "com.kapgel.KapGel"
id_suffixes = env == "sandbox" ? [ "Sandbox" ] : [ "", ".watchkitapp", ".watchkitapp.watchkitextension" ]
for suffix in id_suffixes
match(
type: type,
app_identifier: id_prefix + suffix,
force_for_new_devices: options[:force],
keychain_name: ENV["KEYCHAIN_NAME"]
)
end
end
desc "Upload a build to either Testflight or Fabric"
lane :upload do |options|
# Bump
version_bump_project options
# Prompt for env and type if necessary and release
prompt_and_release options
end
#################
# PRIVATE LANES #
#################
private_lane :version_bump_project do |options|
# Set build number to current date and time
build_number = Time.now.in_time_zone("Europe/Istanbul").strftime("%Y%m%d%H%M")
if options[:pr]
build_number = build_number + "." + options[:pr]
end
ENV["BUILD_NUMBER"] = build_number
increment_build_number build_number: build_number
end
private_lane :prompt_and_release do |options|
type = if options.key?(:type)
options[:type]
else
prompt_for_type(context: "build")
end
env = if options.key?(:env)
options[:env]
else
prompt_for_environment
end
release(
type: type,
env: env,
pr: options[:pr]
)
end
private_lane :prompt_for_type do |options|
context = options[:context]
types = if context == "certificate"
[ "development", "adhoc", "appstore" ]
elsif context == "build"
[ "adhoc", "appstore" ]
else
[]
end
type = prompt(text: "Which release type? arg: 'type', possible values: " + types.inspect)
unless types.include? type
UI.user_error!("Please specify a valid release type! " + types.inspect)
end
type
end
private_lane :prompt_for_environment do
envs = [ "sandbox", "production" ]
env = prompt(text: "Which environment? arg: 'env', possible values: " + envs.inspect)
unless envs.include? env
UI.user_error!("Please specify a valid environment! " + envs.inspect)
end
env
end
private_lane :release do |options|
type = options[:type]
env = options[:env]
destination = options[:type] == "adhoc" ? "fabric" : "testflight"
create_keychain_if_necessary
build_and_submit(
type: type,
env: env,
destination: destination,
pr: options[:pr]
)
delete_keychain_if_necessary
end
private_lane :create_keychain_if_necessary do
if Helper.is_ci?
create_keychain(
name: ENV["KEYCHAIN_NAME"],
default_keychain: true,
unlock: true,
timeout: 3600,
lock_when_sleeps: true,
password: SecureRandom.base64
)
end
end
private_lane :delete_keychain_if_necessary do
if Helper.is_ci?
delete_keychain(name: ENV["KEYCHAIN_NAME"])
end
end
private_lane :build_and_submit do |options|
build(
type: options[:type],
env: options[:env],
pr: options[:pr]
)
submit(destination: options[:destination])
end
private_lane :build do |options|
# Certificates & Profiles
types = ["development", options[:type]]
for type in types
should_force = type != "appstore"
certificates(
type:type,
force: should_force,
env: options[:env]
)
end
is_sandbox = options[:env] == "sandbox"
if is_sandbox
# Badge app icon for sandbox builds
badge(dark:true)
# Update display name for sandbox builds
update_info_plist(
plist_path: "KapGel/Plists/Sandbox-Info.plist",
display_name: options[:pr] ? "α-" + options[:pr] : "KapGel-β"
)
end
# Build
scheme = is_sandbox ? "KapGel Sandbox" : "KapGel"
configuration = if options[:type] == "development"
"Debug"
elsif options[:type] == "adhoc"
"Adhoc"
elsif options[:type] == "appstore"
"Release"
end
puts "using configuration: " + configuration
gym(
scheme: scheme,
configuration: configuration,
suppress_xcode_output: false,
clean: true,
include_symbols: true,
output_directory: "./build/",
buildlog_path:"./build/logs/"
# use_legacy_build_api: true
)
end
private_lane :submit do |options|
if options[:destination] == "testflight"
pilot(
skip_submission: true
)
elsif options[:destination] == "fabric"
crashlytics(
api_token: '44df048f814d9bac88bd3b8c0957a8051a6be1a0',
build_secret: '89001c58cdefc41552a2857bae345346418c416e252a81830a08ab4ed16cd822',
notes: "Build at " + ENV["BUILD_NUMBER"],
notes_path: 'build/change.log',
groups: "all",
notifications: true
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment