자동 Android 빌드와 GitLab CI
Capacitor 앱의 CI/CD 설정은 복잡하고 시간이 많이 걸립니다. 알아야 할 내용은 다음과 같습니다.
필수 조건
시작하기 전에 다음을 설정해야 합니다.
- GitLab 계정에 관리자 권한
- Google Play Store에 이미 올려져 있는 __CAPGO_KEEP_0__ 앱
- Android 서명 키와 키스토어 파일
- Google Cloud Console 프로젝트에 Play Store API가 활성화된 상태
- 서비스 계정에 적절한 권한
- GitLab CI/CD 워크플로우에 대한 이해
- Fastlane 구성에 대한 지식
- pipeline 유지 및 디버깅에 소요되는 시간
Capgo CI/CD를 위한 빌드하기 위해 Capgo을 사용하세요.
CI/CD 유지 관리를 생략하세요. Capgo 빌드 __CAPGO_KEEP_0__ 빌드는 CI/CD pipeline에서 이미 존재하는 Android 빌드를 signed native Android 빌드로 생성합니다.
- __CAPGO_KEEP_0__ 빌드는 CI/CD pipeline과 함께 작동합니다.: Trigger Capgo Build from GitLab CI, GitHub Actions, Jenkins, or local scripts after your web build and
npx cap sync. - : GitLab CI, __CAPGO_KEEP_1__ Actions, Jenkins, 또는 로컬 스크립트에서 __CAPGO_KEEP_0__ 빌드를 GitLab CI, __CAPGO_KEEP_1__ Actions, Jenkins, 또는 로컬 스크립트에서 __CAPGO_KEEP_0__ 빌드를 트리거하세요.CI/CD 비밀에서 서명
- : Android 키스토어, 키 별칭, 비밀번호, 및 Play Console 서비스 계정 JSON을 CI/CD 비밀에 저장하세요.: Capgo Build provides maintained Android build environments, so you do not have to manage SDK images, Gradle cache issues, or Fastlane lanes.
- : __CAPGO_KEEP_0__ 빌드는 유지 관리가 된 Android 빌드 환경을 제공하므로, __CAPGO_KEEP_1__ 이미지를 관리하거나 Gradle 캐시 문제, 또는 Fastlane 경로를 관리할 필요가 없습니다.아티팩트 및 제출하기로 가기야야합니다 : Capgo CLI에서 signed 아티팩트를 다운로드하거나 QA에 제출하세요.
__CAPGO_KEEP_0__
- Capgo plans start at $12/month
- __CAPGO_KEEP_0__에 포함된 OTA 업데이트와 매월 약 15개의 네이티브 빌드
- __CAPGO_KEEP_0__ 빌드는 추가 빌드 분량에 따라 분당 요금으로 청구됩니다.
수동 설정 안내서
다음 단계를 따르세요.
__CAPGO_KEEP_0__ Fastlane 파일 복사
- GitLab 암호화된 비밀을 저장하세요.
- Google Play 서비스 계정 키를 생성하고 저장하세요.
- Android 서명 키를 저장하세요.
- __CAPGO_KEEP_0__
- GitLab workflow .yml 파일 설정
1. Fastlane 파일 복사
Fastlane은 안드로이드 개발을 자동화하는 Ruby 라이브러리입니다. Fastlane을 사용하면 Android Studio에서 수행하는 일반적인 작업을 수행하는 '액션'을 묶은 '레인'을 구성할 수 있습니다. Fastlane은 많은 기능을 제공하지만 이 튜토리얼에서는 핵심 액션만 사용할 것입니다.
프로젝트의 루트 폴더에 Fastlane 폴더를 생성하고 다음 파일을 복사하십시오: Fastlane
default_platform(:android)
KEYSTORE_KEY_ALIAS = ENV["KEYSTORE_KEY_ALIAS"]
KEYSTORE_KEY_PASSWORD = ENV["KEYSTORE_KEY_PASSWORD"]
KEYSTORE_STORE_PASSWORD = ENV["KEYSTORE_STORE_PASSWORD"]
platform :android do
desc "Deploy a beta version to the Google Play"
private_lane :verify_changelog_exists do |version_code: |
changelog_path = "android/metadata/en-US/changelogs/#{version_code}.txt"
UI.user_error!("Missing changelog file at #{changelog_path}") unless File.exist?(changelog_path)
UI.message("Changelog exists for version code #{version_code}")
end
private_lane :verify_upload_to_staging do |version_name: |
UI.message "Skipping staging verification step"
end
lane :beta do
keystore_path = "#{Dir.tmpdir}/build_keystore.keystore"
File.write(keystore_path, Base64.decode64(ENV['ANDROID_KEYSTORE_FILE']))
json_key_data = Base64.decode64(ENV['PLAY_CONFIG_JSON'])
previous_build_number = google_play_track_version_codes(
package_name: ENV['DEVELOPER_PACKAGE_NAME'],
track: "internal",
json_key_data: json_key_data,
)[0]
current_build_number = previous_build_number + 1
sh("export NEW_BUILD_NUMBER=#{current_build_number}")
gradle(
task: "clean bundleRelease",
project_dir: 'android/',
print_command: false,
properties: {
"android.injected.signing.store.file" => "#{keystore_path}",
"android.injected.signing.store.password" => "#{KEYSTORE_STORE_PASSWORD}",
"android.injected.signing.key.alias" => "#{KEYSTORE_KEY_ALIAS}",
"android.injected.signing.key.password" => "#{KEYSTORE_KEY_PASSWORD}",
'versionCode' => current_build_number
})
upload_to_play_store(
package_name: ENV['DEVELOPER_PACKAGE_NAME'],
json_key_data: json_key_data,
track: 'internal',
release_status: 'completed',
skip_upload_metadata: true,
skip_upload_changelogs: true,
skip_upload_images: true,
skip_upload_screenshots: true,
)
end
lane :build do
gradle(
task: "clean bundleRelease",
project_dir: 'android/',
print_command: false,
properties: {
"android.injected.signing.store.file" => "#{keystore_path}",
"android.injected.signing.store.password" => "#{KEYSTORE_STORE_PASSWORD}",
"android.injected.signing.key.alias" => "#{KEYSTORE_KEY_ALIAS}",
"android.injected.signing.key.password" => "#{KEYSTORE_KEY_PASSWORD}",
})
end
lane :prod_release do
build_gradle = File.read("../android/app/build.gradle")
verify_changelog_exists(version_code: build_gradle.match(/versionCode (\d+)/)[1])
verify_upload_to_staging(version_name: build_gradle.match(/versionName '([\d\.]+)'/)[1])
supply(
track_promote_to: 'beta',
skip_upload_apk: true,
skip_upload_aab: true,
skip_upload_metadata: false,
skip_upload_changelogs: false,
skip_upload_images: false,
skip_upload_screenshots: false
)
end
end
GitLab CI/CD 변수에 비밀 저장
GitLab은 CI/CD 변수를 암호화하여 저장하는 방법을 제공합니다. 이 기능은 GitHub의 저장소 비밀과 유사합니다._sensitive 정보를 안전하게 저장하려면
- 프로젝트의 설정으로 이동하십시오.
- CI/CD > 변수로 이동하십시오.
- 다음 변수를 추가하십시오:
- ANDROID_KEYSTORE_FILE: Android 빌드에 사용되는 base64-인코딩된
.jks또는.keystore파일입니다. 이 파일은 Play App Signing을 사용하는 경우 업로드 키와 관련된 키스토어 파일이거나 앱 서명 키일 수 있습니다. - KEYSTORE_KEY_PASSWORD: 키 스토어 파일과 관련된 암호
- KEYSTORE_KEY_ALIAS: 키 스토어 별칭
- KEYSTORE_STORE_PASSWORD: 개인 키 암호
- DEVELOPER_PACKAGE_NAME: Android 앱 ID와 같은 com.example.app
- PLAY_CONFIG_JSON: 서비스 계정 키 JSON을 base64로 인코딩한 것.
Google Play 서비스 계정 키를 생성하는 중입니다.
secret를 생성하려면 다음 단계를 따르세요: PLAY_CONFIG_JSON Google Cloud Console로 이동하세요.
- 새 프로젝트를 생성하거나 기존 프로젝트를 선택하세요. Google Play Android Developer __CAPGO_KEEP_0__를 활성화하세요.
- __CAPGO_KEEP_0__
- API
- 서비스 계정 만들기:
- ‘IAM & Admin’ > ‘Service Accounts’로 이동하세요.
- ‘Create Service Account’ 클릭하세요.
- 이름과 설명을 입력하세요.
- ‘Create and Continue’ 클릭하세요.
- 권한 assignment 생략하고 ‘Done’ 클릭하세요.
- JSON 키 생성:
- 서비스 계정 목록에서 찾으세요.
- 세로 세로 세로 메뉴 > ‘Manage keys’ 클릭하세요.
- ‘Add Key’ > ‘Create new key’ 클릭하세요.
- JSON 형식 선택하세요.
- ‘Create’ 클릭하세요.
- Play Console에서 앱에 서비스 계정에 대한 접근 권한을 부여하십시오:
- 로 이동하세요. Play Console
- 사용자 및 권한
- 으로 이동하세요.
- 새로운 사용자 초대하기
- 를 클릭하세요.
- 서비스 계정 이메일 (*.iam.gserviceaccount.com로 끝나야 함)을 입력하세요.
- 프로덕션으로 출시 허용
base64 -i path/to/your/service-account-key.json | pbcopy - 권한을 부여하세요.
PLAY_CONFIG_JSON사용자 초대하기
를 클릭하세요. (이미 초대한 사용자가 있다면 초대한 사용자의 이름을 선택하세요.)
Create a .gitlab-ci.yml file at the root of your project to define your CI/CD pipeline. Below is an example of how you can structure your pipeline:
image: mingc/android-build-box:latest
stages:
- build
- upload_to_capgo
- build_and_upload_android
build:
stage: build
tags:
- saas-linux-xlarge-amd64
cache:
- key:
files:
- bun.lockb
paths:
- .node_modules/
script:
- npm install
- npm run build
artifacts:
paths:
- node_modules/
- dist/
only:
- master
upload_to_capgo:
stage: upload_to_capgo
tags:
- saas-linux-xlarge-amd64
script:
- npx @capgo/cli@latest bundle upload -a $CAPGO_TOKEN -c dev
dependencies:
- build
when: manual
only:
- master
build_and_upload_android:
tags:
- saas-linux-xlarge-amd64
stage: build_and_upload_android
cache:
- key:
files:
- android/gradle/wrapper/gradle-wrapper.properties
paths:
- ~/.gradle/caches/
script:
- npx cap sync android
- npx cap copy android
- bundle exec fastlane android beta # We do create a tag for the build to trigger XCode cloud builds
dependencies:
- build
when: manual
only:
- master
Trigger the Pipeline
Whenever you push a new tag to your GitLab repository, GitLab CI/CD will automatically trigger the defined pipeline, which will build and deploy your Android app using Fastlane.
Make sure to adjust the paths and dependencies according to your project’s structure and requirements. This setup will help you automate the deployment of your Android app on GitLab CI/CD.
Conclusion
By configuring GitLab CI/CD with the mingc/android-build-box Docker image, you can automate the Android app build process, making your development workflow more efficient and reliable. This automation frees up your time to focus on the core aspects of app development, ultimately helping you deliver high-quality Android apps more efficiently.
Keep going from Automatic Capacitor Android build with GitLab
If you are using Automatic Capacitor Android build with GitLab to plan CI/CD automation, connect it with Capgo CI/CD for the product workflow in Capgo CI/CD, Capgo Native Builds Capgo Native Builds를 위한 제품 워크플로우 Capgo Integrations Capgo Integrations를 위한 제품 워크플로우 CI/CD Integration CI/CD Integration의 구현 세부 사항, 그리고 GitHub Actions Integration GitHub Actions Integration의 구현 세부 사항.