Skip to content

Instantly share code, notes, and snippets.

@jgautheron
Forked from kennydee/Fastfile
Created August 8, 2017 13:58
Show Gist options
  • Save jgautheron/f6798c331449be7b6ca17e2536aee4ee to your computer and use it in GitHub Desktop.
Save jgautheron/f6798c331449be7b6ca17e2536aee4ee to your computer and use it in GitHub Desktop.
Fastfile for staging environment with Appetize.io on React Native (iOs & Android), with statuses update on Github Enterprise
require 'httparty'
fastlane_version "1.95.0"
default_platform :ios
before_all do
# put here your token and iOs scheme app
ENV["GITHUB_TOKEN"] = "---"
ENV["APPETIZE_TOKEN"] = "---"
ENV["APP_IOS_SCHEME"] = "---"
ENV["GIT_COMMIT"] = last_git_commit[:commit_hash]
# Use ghprbSourceBranch env variable on CI, git_branch actions elsewhere
if !ENV["ghprbSourceBranch"]
ENV["ghprbSourceBranch"] = git_branch
end
end
# Update git status on the current commit.
private_lane :githubStatusUpdate do |options|
response = HTTParty.post(
"https://<yourgithubenterprisedomain.tld>/api/v3/repos/<orga>/<repos>/statuses/#{ENV["GIT_COMMIT"]}?access_token=#{ENV["GITHUB_TOKEN"]}",
:body => {
:context => options[:context],
:state => options[:state],
:description => options[:description],
:target_url => options[:url]
}.to_json,
:headers => { 'Content-Type' => 'application/json' }
)
end
# get the publicKey of the appetizeApp corresponding to your git branch
private_lane :getAppetizePublicKey do |options|
publicKey = ""
response = HTTParty.get("https://#{ENV["APPETIZE_TOKEN"]}@api.appetize.io/v1/apps")
json = JSON.parse(response.body)
# Find branch name in notes
json["data"].each do |value|
if value["note"] == ENV["ghprbSourceBranch"] && value["platform"] == options[:platform]
publicKey = value["publicKey"]
end
end
publicKey
end
# find the path of the last apk build
private_lane :getLastAPKPath do
apk_search_path = File.join('../android/', 'app', 'build', 'outputs', 'apk', '*.apk')
new_apks = Dir[apk_search_path].reject { |path| path =~ /^.*-unaligned.apk$/i}
new_apks = new_apks.map { |path| File.expand_path(path)}
last_apk_path = new_apks.sort_by(&File.method(:mtime)).last
last_apk_path
end
platform :ios do
desc "Deployment iOs lane"
lane :deployAppetizeFeature do
githubStatusUpdate(
context: 'Appetize iOs',
state: 'pending',
url: "https://appetize.io/dashboard",
description: 'iOs build in progress'
)
Dir.chdir "../ios" do
tmp_path = "/tmp/fastlane_build"
#seems not possible tu use gym here because is only made to do ipa archive
xcodebuild_configs = {
configuration: "Release",
sdk: "iphonesimulator",
derivedDataPath: tmp_path,
xcargs: "CONFIGURATION_BUILD_DIR=" + tmp_path,
scheme: "#{ENV["APP_IOS_SCHEME"]}"
}
Actions::XcodebuildAction.run(xcodebuild_configs)
app_path = Dir[File.join(tmp_path, "**", "*.app")].last
zipped_bundle = Actions::ZipAction.run(path: app_path, output_path: File.join(tmp_path, "Result.zip"))
Actions::AppetizeAction.run(
path: zipped_bundle,
api_token: "#{ENV["APPETIZE_TOKEN"]}",
platform: "ios",
note: "#{ENV["ghprbSourceBranch"]}",
public_key: getAppetizePublicKey({platform: "ios"})
)
FileUtils.rm_rf(tmp_path)
end
githubStatusUpdate(
context: 'Appetize iOs',
state: 'success',
url: "#{lane_context[SharedValues::APPETIZE_APP_URL]}",
description: 'iOs build succeed'
)
end
error do |lane, exception|
case lane
when /deployAppetizeFeature/
githubStatusUpdate(
context: 'Appetize iOs',
state: 'failure',
url: "https://appetize.io/dashboard",
description: 'iOs build failed'
)
end
end
end
platform :android do
desc "Deployment Android lane"
lane :deployAppetizeFeature do
githubStatusUpdate(
context: 'Appetize Android',
state: 'pending',
url: "https://appetize.io/dashboard",
description: 'Android build in progress'
)
gradle(
task: "assemble",
build_type: "Release",
project_dir: "android/"
)
Actions::AppetizeAction.run(
path: getLastAPKPath,
api_token: "#{ENV["APPETIZE_TOKEN"]}",
platform: "android",
note: "#{ENV["ghprbSourceBranch"]}",
public_key: getAppetizePublicKey({platform: "android"})
)
githubStatusUpdate(
context: 'Appetize Android',
state: 'success',
url: "#{lane_context[SharedValues::APPETIZE_APP_URL]}",
description: 'Android build succeed'
)
end
error do |lane, exception|
case lane
when /deployAppetizeFeature/
githubStatusUpdate(
context: 'Appetize Android',
state: 'failure',
url: "https://appetize.io/dashboard",
description: 'Android build failed'
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment