From f710f9d11ea989e4dcb09f24566ed42e179b20bc Mon Sep 17 00:00:00 2001 From: liuyanfeng Date: Thu, 26 Sep 2024 23:23:53 +0800 Subject: [PATCH] feat: android fastlane --- android/fastlane/Fastfile | 102 ++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 14 deletions(-) diff --git a/android/fastlane/Fastfile b/android/fastlane/Fastfile index 19c557cc..d2ac818e 100644 --- a/android/fastlane/Fastfile +++ b/android/fastlane/Fastfile @@ -15,24 +15,98 @@ default_platform(:android) +$env_key_project_root = 'ENV_BUILD_WORKSPACE' +$env_current_branch = 'ENV_BUILD_BRANCH' +$env_current_tag = 'ENV_BUILD_TAG' + +$path_dir_build = File.join(ENV[$env_key_project_root],'') +$current_branch = ENV[$env_current_branch] +$current_tag = ENV[$env_current_tag] + +$path_apk_output_dir = File.join($path_dir_build, 'build/app/outputs/flutter-apk') + + +def print_header header + UI.message "~~~~~~~~~~~~~~~~~~~~~~" + UI.message '📍 ' + header.to_s + UI.message "~~~~~~~~~~~~~~~~~~~~~~" +end + +def print_log(content) + UI.message '📠 ' + content.to_s +end + platform :android do - desc "Runs all the tests" - lane :test do - gradle(task: "test") + before_all do + print_header '🏁 Before All' + print_log $current_branch + print_log $current_tag + print_log $path_file_preview_apk_default + print_log $path_file_release_apk_default + print_log $path_file_preview_apk_copy + print_log $path_file_release_apk_copy + Dir.chdir "../.." do + sh('pwd') + end end - desc "Submit a new Beta Build to Crashlytics Beta" - lane :beta do - gradle(task: "clean assembleRelease") - crashlytics - - # sh "your_script.sh" - # You can also use other beta testing services here + desc "Submit a new Beta Build to Pgy Beta" + lane :beta do |options| + flavor = options[:flavor] + env = options[:env] + UI.user_error!("flavor is required") unless flavor + UI.user_error!("env is required") unless env + print_log "build #{flavor} on #{env}" + build_number = Time.now.strftime("%Y%m%d%H") + print_log "BuildNo #{build_number}" + build_version = '1.0.0' + print_log "build_version #{build_version}" + commit_hash = last_git_commit + short_hash = commit_hash[:abbreviated_commit_hash] + print_log "last_git_commit_short_hash #{short_hash}" + Dir.chdir "../.." do + sh("flutter", "build", "apk", "--no-tree-shake-icons", "--release", "--flavor", "#{flavor}_#{env}", "-t", "lib/main_#{flavor}_#{env}.dart", "--build-number=#{build_number}", "--build-name=#{build_version}") + end + logs = changelog_from_git_commits( + pretty: '- %s (%cn)', + commits_count: 5, + merge_commit_filtering: 'exclude_merges' + ) + upload_file_to_pgy(directory:$path_apk_output_dir,file_extension:".apk",logs:logs) end - desc "Deploy a new version to the Google Play" - lane :deploy do - gradle(task: "clean assembleRelease") - upload_to_play_store + desc "Build & upload a new version to Gitlab Release" + lane :release do |options| + flavor = options[:flavor] + UI.user_error!("flavor is required") unless flavor + print_log "build flavor for: #{flavor}" + build_number = Time.now.strftime("%Y%m%d%H") + print_log "BuildNo #{build_number}" + build_version = $current_tag.match(/^v(\d+\.\d+\.\d+)/).captures[0] + print_log "buildVersion #{build_version}" + commit_hash = last_git_commit + short_hash = commit_hash[:abbreviated_commit_hash] + print_log "last_git_commit_short_hash #{short_hash}" + Dir.chdir "../.." do + # sh("sed", "-i", "s/1.0.0+1/#{appSdkVersion}/g", "pubspec.yaml") + # sh("sed", "-i", "s/upload_source_maps: true/upload_source_maps: false/g", "pubspec.yaml") + sh("flutter", "build", "apk", "--no-tree-shake-icons", "--release", "--flavor", "#{flavor}", "-t", "lib/main_#{flavor}.dart", "--build-number=#{build_number}", "--build-name=#{build_version}") + #sh('cp',$path_file_release_apk_default,$path_file_release_apk_copy) + end + end + + lane :upload_file_to_pgy do |options| + directory = options[:directory] + file_extension = options[:file_extension] + logs = options[:logs] + UI.user_error!("Directory path is required") unless directory + UI.user_error!("File extension is required") unless file_extension + # 使用Dir.glob来查找目录下的所有.apk和.ipa文件 + Dir.glob("#{directory}/*#{file_extension}").each do |file| + # 打印文件名称 + puts "start upload file: #{file}" + pgyer(api_key: ENV['PGY_API_KEY'],apk:file,update_description: logs) + File.delete(file) + end end end