Skip to content

Instantly share code, notes, and snippets.

@thejohnlima
Last active December 31, 2022 01:14
Show Gist options
  • Save thejohnlima/c8950a0eca0752404613d90706cf01b2 to your computer and use it in GitHub Desktop.
Save thejohnlima/c8950a0eca0752404613d90706cf01b2 to your computer and use it in GitHub Desktop.
CI fastlane script
# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
default_platform(:ios)
platform :ios do
before_all do |lane|
sh "sh ./prepare_files.sh"
sh "sh ./prepare_simulators.sh"
sh("bundle install; bundle exec pod install")
end
lane :build do
ENV['FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT'] = '120'
scan_ios()
sh "sh .././install_swiftlint.sh"
swiftlint(
mode: :lint,
path: "Demo",
output_file: "reports/swiftlint.txt",
config_file: ".swiftlint.yml",
raise_if_swiftlint_error: true,
ignore_exit_status: true
)
run_sonar()
end
def scan_ios()
scan(
app_identifier: "com.demo.app",
workspace: ENV["WORKSPACE"],
scheme: "iOS-Staging",
destination: "platform=iOS Simulator,name=iPhone 13 mini",
derived_data_path: "./DerivedData",
cloned_source_packages_path: "./DerivedData/SourcePackages",
output_directory: "./reports",
code_coverage: true
)
end
def upload_codecov()
slather(
cobertura_xml: true,
configuration: "Staging",
scheme: "iOS-Staging",
proj: "demo.xcodeproj",
workspace: "demo.xcworkspace",
build_directory: "reports",
output_directory: "reports",
ignore: [
"DemoTests/*",
"DerivedData/*",
"**/UIColor+Extensions.swift",
"**/*View.swift",
"**/*Cell.swift",
"**/*VC.swift",
"**/*VC+*.swift",
"**/*Controller.swift",
"**/*Controller+*.swift"
]
)
sh "sh ./upload_codecov.sh"
end
def run_sonar()
slather(
sonarqube_xml: true,
configuration: "Staging",
scheme: "iOS-Staging",
proj: "demo.xcodeproj",
workspace: "demo.xcworkspace",
build_directory: "reports",
output_directory: "reports",
ignore: [
"DemoTests/*",
"DerivedData/*",
"**/UIColor+Extensions.swift",
"**/*View.swift",
"**/*Cell.swift",
"**/*VC.swift",
"**/*VC+*.swift",
"**/*Controller.swift",
"**/*Controller+*.swift"
]
)
sh "sh .././sonar_scanner.sh #{git_branch}"
end
end
source "https://rubygems.org"
gem 'fastlane'
gem 'cocoapods'
gem 'slather'
plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
eval_gemfile(plugins_path) if File.exist?(plugins_path)
if [ ! -f "/opt/homebrew/bin/swiftlint" ]; then
brew install swiftlint
fi
if [ -d ../reports ]; then
rm -Rf ../reports
fi
if [ -d ../build ]; then
rm -Rf ../build
fi
if [ -d ../DerivedData ]; then
rm -Rf ../DerivedData
fi
mkdir -p ../reports
touch ../reports/sonarqube-generic-coverage.xml
if [ -f *.xml ]; then
rm -f *.xml
fi
xcrun simctl delete 'iPhone 13 mini';
xcrun simctl create 'iPhone 13 mini' com.apple.CoreSimulator.SimDeviceType.iPhone-13-mini;
export BRANCH_NAME=$1
export SONAR_SCANNER_VERSION=4.7.0.2747
export SONAR_TOKEN=#YOUR_TOKEN_HERE
export SONAR_SCANNER_HOME=$HOME/.sonar/sonar-scanner-$SONAR_SCANNER_VERSION-macosx
curl --create-dirs -sSLo $HOME/.sonar/sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-macosx.zip
unzip -o $HOME/.sonar/sonar-scanner.zip -d $HOME/.sonar/
export PATH=$SONAR_SCANNER_HOME/bin:$PATH
export SONAR_SCANNER_OPTS="-server"
curl --create-dirs -sSLo $HOME/.sonar/build-wrapper-macosx-x86.zip https://sonarcloud.io/static/cpp/build-wrapper-macosx-x86.zip
unzip -o $HOME/.sonar/build-wrapper-macosx-x86.zip -d $HOME/.sonar/
export PATH=$HOME/.sonar/build-wrapper-macosx-x86:$PATH
build-wrapper-macosx-x86 xcodebuild -workspace demo.xcworkspace -scheme iOS-Staging -configuration Staging
sonar-scanner \
-Dsonar.host.url=https://sonarcloud.io \
-Dsonar.organization=#SONAR_ORGANIZATION_NAME_HERE \
-Dsonar.projectKey=#SONAR_PROJECT_KEY_HERE \
-Dsonar.projectBaseDir=./../ \
-Dsonar.inclusions=**/*.swift \
-Dsonar.sources=#PROJECT_SOURCE_FOLDER_NAME \
-Dsonar.branch.name=$BRANCH_NAME \
-Dsonar.swift.swiftlint.report=reports/swiftlint.txt \
-Dsonar.coverageReportPaths=reports/sonarqube-generic-coverage.xml \
-Dsonar.coverage.exclusions=build,DerivedData,Pods,.*Test.*,.*Tests.*,**/*Controller.swift,**/*Controller+*.swift,**/*VC.swift,**/*VC+*.swift,**/*View.swift,**/*Cell.swift
bash <(curl -s https://codecov.io/bash) -f ../reports/cobertura.xml -X coveragepy -X gcov -X xcode -t CODECOV_TOKEN_HERE
@thejohnlima
Copy link
Author

Run fastlane:

bundle exec fastlane build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment