Skip to main content
Minh Nong
💬
0 discussions

React native - Fastlane - Gitlab CI

Tiền Đề

Tài liệu hướng dẫn cài đặt Chuẩn bị

  • Linux
  • Tài khoản android developer
  • Tài khoản firebase ( enable service Firebase App Distribution - free )
  • Android studio
  • Ruby 2.7.6

Lưu ý :

  • Đối với app android mới tạo, trước khi dùng fastlane phải manual release lần đầu trên Google Play Console
  • Dùng Firebase App distribution vì chúng ta phải manual gửi tester link khi internal release, Firebase app distribution sẽ giúp tự động hóa báo cho tester biết khi có phiên bản mới release
  • Khi push app lên Google Play, chúng ta phải build signed apk/ aab, nếu không đúng key google sẽ không nhận, Key để trong file env.default trong thư mục secure
  • Json secret để fastlane kết nối với google để trong thư mục secure

Nội dung

1. Các bước cài đặt mới từ đầu

Bước 1: Cài đặt React Native Cli ( Development OS Linux -Target OS Android )

Config sdk path

 $ cd android 
$ nano local.properties

sdk.dir='đường dẫn tới sdk file'
note : có thể tìm đường dẫn sdk bằng cách, mở Android Studio -> tìm trong phần sdk manager

Chạy thử :

$ npx react native start
chọn run android
Chạy được -> Pass

Bước 2: Cài đặt fastlane

Cài đặt Ruby

$ sudo apt install ruby-full
$ cd src
$ sudo gem install bundler

$ cd android
$ bundle init
$ nano Gemfile
gem "fastlane"

$ bundle update

Tạo key sign cho apk/abb

keytool -genkey -v -keystore vn.gcalls.demo-key.keystore -alias vn.gcalls.demo-key-alias -keyalg RSA -keysize 2048 -validity 10000

Lưu ý : nhớ password để build cho những lần tiếp theo

Chỉnh lại file build.gradle

$ cd android/app
-> sửa file build.gradle ở các mục sau

signingConfigs {
release {
storeFile file(System.getenv(”RELEASE_STORE_FILE”))
storePassword(System.getenv(”RELEASE_STORE_PASSWORD”))
keyAlias(System.getenv(”RELEASE_KEY_ALIAS”))
keyPassword(System.getenv(”RELEASE_KEY_PASSWORD”))
}
}
buildTypes {
// debug {
// signingConfig signingConfigs.debug
// }
release {
// Caution! In production, you need to generate your own keystore file.
// see https://reactnative.dev/docs/signed-apk-android.
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
}
}

-> cất thông tin ENV
cd secue
nano env.default
Chỉnh sửa các thông tin sau cho đúng sau đó copy và paste vào terminal

export RELEASE_STORE_FILE=”Đường dẫn tới key”
export RELEASE_STORE_PASSWORD=password
export RELEASE_KEY_ALIAS=vn.gcalls.demo-key-alias
export RELEASE_KEY_PASSWORD=password

Build thử

$ cd android
# Build apk
$ ./gradlew assembleRelease → success → pass
# Build aab
$ ./gradlew assembleRelease bundle → success → pass

Sau khi manual release lần 1 trên play console -> Lấy Jsonfile từ google, xem phân Collect your Google credentials

$ cd android
bundle exec fastlane init
-> Nhập các thông tin cần thiết :
- App bundle
- Đường dẫn tới Json file của google play console

Chỉnh sửa deploy logic trong Fastfile Ví dụ 1: viết 1 lane build apk/abb

Cài đặt plugin increment_version_code
$ fastlane add_plugin increment_version_code

desc "Build Android"
lane :buildAndroid do
increment_version_code(
gradle_file_path:"./app/build.gradle"
)

gradle(task: "clean")
# build apk
gradle(task: "assembleRelease")
#build bundle
gradle(task: "assembleRelease bundle")
end

Lane buildAndroid thực hiện nhiệm vụ build bundle và apk

$ cd android
$ fastlane buildAndroid

Ví dụ 2: Viết 1 lane gửi apk cho tester thông qua Firebase App Distribution và đồng thời gửi lên track production trên GOOGLE PLAY CONSOLE

** Lưu ý : Release lên lane PROD là App sẽ Public ra Store, trong trường hợp muốn test thử App có kết nối với store hay không thì nên push vào các track khác như internal, alpha

  desc "distribute"
lane :distribute do

# Internal testing in Google Play does not send out emails and we do not want to share the opt-in link to your internal testers manually, therefore, we use Firebase instead.

# service_credentials_file :
# to get service_credentials_file -> check following document -> Step 2. Authenticate with Firebase -> Use Firebase service account credentials
# https://firebase.google.com/docs/app-distribution/android/distribute-fastlane

firebase_app_distribution(
app: "1:74702886408:android:063f1b02e9ff34d22363bd",
testers: "quocminh29101992@gmail.com,huracan2910@gmail.com",
android_artifact_type: "APK",
release_notes: "Lots of amazing new features to test out!",
service_credentials_file:File.expand_path(File.dirname(__FILE__), '../secure/mobile-dev-13d1a-51e09f8c29bf.json'),
ipa_path:File.expand_path(File.dirname(__FILE__),'../app/build/outputs/apk/release/app-release.apk'),

)


# production, beta, alpha, internal
upload_to_play_store(
track: 'production',
skip_upload_apk: true,
skip_upload_changelogs: true,
skip_upload_images: true,
skip_upload_screenshots:true,
aab: File.expand_path(File.dirname(__FILE__),'../app/build/outputs/bundle/release/app-release.aab'),
)
end

Cài đặt gitlab-ci

Tạo file .gitlab-ci.yml và push lên branch android

stages:
- android

android:
stage: android
dependencies: []

script:
- cd src
- cd android
- fastlane distribute
tags: #tag registered with gitlab-runner
- fastlane
only: #defines the names of branches and tags for which the job will run
- android


2. Các config cần kiểm tra sau khi clone project

  • config đường dẫn sdk trong local.properties (android/local.properties)
  • kiểm tra các file trong thư mục android/secure (keystore, 2 json file, set up đường dẫn trong env.default)
  • kiểm tra đường dẫn tới Json file trong android/fastlane/Appfile
  • kiểm tra ruby version
  • Build thử ở local

Kết luận

  • Google console không tự động gửi email cho tester mà chúng ta phải manual gửi link update version mới cho tester làm chậm quá trình deploy, và để tự động hóa bước này hiện tại phải thông qua firebase

  • Project app build ra quá nặng về dung lượng

  • Nếu tìm cách build và cài đặt môi trường trên gitlab CI thì độ phức tạp sẽ cao

    Các hướng mở rộng

  • Test và xem xét những gì cần và không cần để giảm dung lượng

  • Tìm các giải pháp deploy khác nếu tốt hơn về mặt quy trình và chi phí

  • Nếu có các giải pháp và các hướng xử lý tốt hơn xin vui lòng đóng góp để cải thiện tài liệu