튜토리얼: GitLab Mobile DevOps로 Android 앱 만들기

튜토리얼: GitLab Mobile DevOps로 Android 앱 만들기

GitLab CI/CD를 사용해 Android 모바일 앱을 빌드하고, 자격 증명으로 서명하고, 앱 스토어에 배포하는 파이프라인을 만드는 튜토리얼이에요. 빌드 환경 구성부터 코드 서명, Google Play 배포까지 전체 흐름을 하나씩 따라 해 보는 방식으로 진행할게요.

Android 빌드는 Docker 이미지를 사용하므로 여러 Android API 버전을 활용할 수 있어요. fastlane과 Gradle의 조합으로 서명을 구성하고, Google Play 통합으로 배포를 자동화하는 흐름을 익히게 됩니다.

출처: 문서

본문

이 튜토리얼에서는 GitLab CI/CD를 사용해 Android 모바일 앱을 빌드하고, 자격 증명으로 서명하고, 앱 스토어에 배포하는 파이프라인을 만들게 돼요.

모바일 DevOps를 구성하려면:

  1. 빌드 환경 설정
  2. fastlane과 Gradle로 코드 서명 구성
  3. Google Play 통합과 fastlane으로 Android 앱 배포 설정

시작하기 전에

이 튜토리얼을 시작하기 전에 다음이 준비되어 있어야 해요.

  • CI/CD 파이프라인에 접근할 수 있는 GitLab 계정
  • GitLab 저장소에 있는 모바일 앱 코드
  • Google Play 개발자 계정
  • 로컬에 설치된 fastlane

빌드 환경 설정

GitLab 호스팅 러너를 사용하거나, 빌드 환경을 완전히 제어하려면 자체 관리 러너(self-managed runners)를 설정하세요.

Android 빌드는 Docker 이미지를 사용하며, 여러 Android API 버전을 지원해요.

  1. 저장소 루트에 .gitlab-ci.yml 파일을 만드세요.
  2. Fabernovel에서 Docker 이미지를 추가하세요.
    test:
      image: fabernovel/android:api-33-v1.7.0
      stage: test
      script:
        - fastlane test
    

fastlane과 Gradle로 코드 서명 구성

Android 코드 서명을 설정하려면:

  1. 키스토어(keystore)를 만드세요.
    1. 다음 명령을 실행해 키스토어 파일을 생성하세요.
      keytool -genkey -v -keystore release-keystore.jks -storepass password -alias release -keypass password \
      -keyalg RSA -keysize 2048 -validity 10000
      
    2. 키스토어 구성을 release-keystore.properties 파일에 넣으세요.
      storeFile=.secure_files/release-keystore.jks
      keyAlias=release
      keyPassword=password
      storePassword=password
      
    3. 두 파일을 모두 프로젝트 설정의 Secure Files로 업로드하세요.
    4. 두 파일을 .gitignore 파일에 추가해 버전 관리에 커밋되지 않도록 하세요.
  2. 새로 만든 키스토어를 사용하도록 Gradle을 구성하세요. 앱의 build.gradle 파일에서:
    1. plugins 섹션 바로 뒤에 추가하세요.
      def keystoreProperties = new Properties()
      def keystorePropertiesFile = rootProject.file('.secure_files/release-keystore.properties')
      if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
      }
      
    2. android 블록 어디든 다음을 추가하세요.
      signingConfigs {
        release {
          keyAlias keystoreProperties['keyAlias']
          keyPassword keystoreProperties['keyPassword']
          storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
          storePassword keystoreProperties['storePassword']
        }
      }
      
    3. 릴리스 빌드 유형에 signingConfig를 추가하세요.
      signingConfig signingConfigs.release
      

이 구성을 포함하는 샘플 fastlane/Fastfile.gitlab-ci.yml 파일은 다음과 같아요.

  • fastlane/Fastfile:
    default_platform(:android)
    
    platform :android do
      desc "Create and sign a new build"
      lane :build do
        gradle(tasks: ["clean", "assembleRelease", "bundleRelease"])
      end
    end
    
  • .gitlab-ci.yml:
    build:
      image: fabernovel/android:api-33-v1.7.0
      stage: build
      script:
        - apt update -y && apt install -y curl
        - wget https://gitlab.com/gitlab-org/cli/-/releases/v1.74.0/downloads/glab_1.74.0_linux_amd64.deb
        - apt install ./glab_1.74.0_linux_amd64.deb
        - glab auth login --hostname $CI_SERVER_FQDN --job-token $CI_JOB_TOKEN
        - glab securefile download --all --output-dir .secure_files/
        - fastlane build
    

Google Play 통합과 fastlane으로 Android 앱 배포 설정

서명된 빌드는 Mobile DevOps Distribution 통합을 사용해 Google Play 스토어에 업로드할 수 있어요.

  1. Google Cloud Platform에서 Google 서비스 계정을 만들고, Google Play의 프로젝트에 그 계정 접근 권한을 부여하세요.
  2. Google Play 통합을 활성화하세요.
    1. 상단 바에서 Search or go to를 선택하고 프로젝트를 찾으세요.
    2. Settings > Integrations를 선택하세요.
    3. Google Play를 선택하세요.
    4. Enable integration 아래에서 Active 체크박스를 선택하세요.
    5. Package name에 앱의 패키지 이름을 입력하세요. 예를 들어 com.gitlab.app_name이에요.
    6. **Service account key (.JSON)**에 키 파일을 끌어 놓거나 업로드하세요.
    7. Save changes를 선택하세요.
  3. 파이프라인에 릴리스 단계를 추가하세요.

샘플 fastlane/Fastfile:

default_platform(:android)

platform :android do
  desc "Submit a new Beta build to the Google Play store"
  lane :beta do
    upload_to_play_store(
      track: 'internal',
      aab: 'app/build/outputs/bundle/release/app-release.aab',
      release_status: 'draft'
    )
  end
end

샘플 .gitlab-ci.yml:

beta:
  image: fabernovel/android:api-33-v1.7.0
  stage: beta
  script:
    - fastlane beta

개요는 Google Play 통합 데모를 참고하세요.

축하해요! 이제 앱이 자동 빌드, 서명, 배포를 할 수 있게 설정되었어요. 첫 파이프라인을 트리거하려면 머지 리퀘스트를 만들어 보세요.

관련 주제

완전한 Android 빌드·서명·릴리스 파이프라인 예시는 Mobile DevOps Android Demo 프로젝트를 참고하세요.

추가 참고 자료는 GitLab 블로그의 DevSecOps 섹션을 확인하세요.

더 알아보기

서명 자격 증명을 소스 코드에 커밋하지 않고 안전하게 관리하는 방법이 핵심 포인트예요. 관련해서는 Secure Files 문서를 자세히 살펴보고, 배포 단계에서는 GitLab Mobile DevOps 전반을 함께 보시면 좋아요.