본문 바로가기
iOS_Swift 앱개발👍

[iOS_Swift] Fastlane을 이용해 CI/CD를 구축해보자 - 1_42

by 개발하는윤기사 2023. 11. 22.
728x90
반응형

 

안녕하세요! 개발하는 윤기사입니다!

 

이번 포스팅에서는 제가 Fastlane을 이용해 TestFlight와 FireBase-App-Distribution의 앱 배포 자동화를 구축을 했는데,

 

그 내용을 공유하고자 합니다!

 

여기서는 샘플 앱을 기준으로 설명을 드릴 거고, 제가 겪었던 트러블 슈팅도 공유해 드리겠습니다! 

 

바로 시작해보시죠!

 

 

 

 

 

 

CI/CD가 뭐예요?

CI : Continuous Integration의 약자로 지속적인 통합을 뜻합니다!

  • 정기적으로 코드가 테스트 및 빌드되어 여러 명의 개발자가 협업할 때 충돌 위험성을 줄여줘요.= 빌드&테스트 자동화

 

CD : Continous Deploy(Delivery)의 약자로 지속적인 배포를 뜻합니다!

  • 앞서 CI에서 통과한 코드에 대하여 테스트서버와 운영서버에 곧바로 그 내용을 배포해 반영하는 것.
  • 따라서, 테스트를 거쳐 레포지토리(Github)에 자동으로 업로드되고, 실시간 프로덕션 환경으로 배포됨. = 배포 자동화

CI/CD 파이프라인은 여러 개발자가 협업할 때 정기적으로 빌드, 테스트, 배포 과정을 거쳐 충돌 위험성을 줄임과 동시에 자동화를 통해 시간을 절약해 줍니다.

 

파이프 라인을 형성해 놓으면 중간 과정을 빠트릴 일이 없고 일일이 빌드 > 테스트 > 결과 확인 과정이 생략된다.

 

 

기존 Xcode를 이용한 배포 과정을 잠깐 살펴볼까요?

 

  1. 코드를 작성.
  2. 버전과 빌드버전 설정.
  3. CocoaPods 업데이트 or 설치
  4. Build 실행
  5. Schema 설정
  6. Archive 실행
  7. Validate 체크
  8. AppStore Connect 혹은 Ad-hoc으로 앱 배포
  9. 배포 알려주기

 

위 과정을 fastlane을 설정하면 한 줄의 CLI로 실행할 수 있습니다!

 

 

 

Fastlane은 또 뭐예요??
fastlane

 

  • CD(Continuous Deployment)로 지속적 배포에 해당, 배포 자동화 가능.
  • iOS와 안드로이드 앱을 테스트 배포 or 정식 배포를 도와주는 툴
  • CocoaPods과도 연결이 가능함.

 

1. 자동화 프레임워크

  1. 모바일 앱 배포와 관련된 반복 작업 자동화에 초점을 맞춘 오픈 소스 프레임워크
  2. 빌드, 코드 서명, 스크린샷 생성, 앱 스토어에 앱 출시 등의 작업을 단순화하도록 설계
  3. Fastlane은 일반적인 작업을 캡슐화하는 사전 구축된 작업 모음을 제공
  4. 이러한 작업을 결합하여 "레인"이라는 사용자 정의 워크플로를 만들 수 있음. => 다양한 레인을 정의할 수 있음.

 

2. CI/CD와의 통합

  1. Fastlane은 독립적으로 사용할 수 있지만 배포 프로세스를 더욱 자동화하기 위해 Bitrise, Jenkins 등과 같은 CI/CD 플랫폼에 통합되는 경우가 많음.

 

 

바로 fastlane 적용해보러 가겠습니다!

 

 

실행하기 앞서, homebrew를 통한 fastlane 설치가 필요합니다!

//homebrew를 이용한 fastlane 설치
brew install fastlane

 

1️⃣ 해당 프로젝트 파일 경로 > fastlane init

❯ fastlane init
[✔] 🚀
[✔] Looking for iOS and Android projects in current directory...
[16:26:59]: Detected an iOS/macOS project in the current directory: 'InAppPurchase.xcodeproj'
[16:26:59]: -----------------------------
[16:26:59]: --- Welcome to fastlane 🚀 ---
[16:26:59]: -----------------------------
[16:26:59]: fastlane can help you with all kinds of automation for your mobile app
[16:26:59]: We recommend automating one task first, and then gradually automating more over time
[16:26:59]: What would you like to use fastlane for?
1. 📸  Automate screenshots
2. 👩‍✈️  Automate beta distribution to TestFlight
3. 🚀  Automate App Store distribution
4. 🛠  Manual setup - manually setup your project to automate your tasks
?  2

- 저희는 TestFlight 배포를 해볼 것이기 때문에 2번을 선택하겠습니다!

 

2️⃣ Apple ID 로그인

- Apple ID 로그인을 하라고 합니다. 여기서는 개발자 계정이 필요해요!

 

- Apple ID를 입력하고 나면, Two-factor Authentication으로 6자리 코드를 입력하라고 합니다! 바로 입력해 줍시다!!

- App Store Connect에 등록되어 있는 teams가 있다면, 사용할 계정을 선택해 주면 됩니다 ^_^

 

 

 

3️⃣ fastlane 폴더와 Gemfile 생성

- 성공적으로 로그인이 되었다면, 프로젝트 폴더 안에 fastlane 폴더가 생겼을 거예요! 그 안에는 Appfile, Fastfile, Gemfile이 들어있습니다!

  • Gemfile, Gemfile.lock : fastlane 프로그램 자체의 버전 관리를 하는 파일
  • Appfile, Fastfile : 앱 프로젝트명, 애플 계정, fastlane에 대한 설정이 담겨있음.

 

 

4️⃣ Appfile 설정 (Feat. ENV 파일 생성)

- Appfile 안에는 app_identifier와 apple_id, team_id가 들어갑니다! 아무래도 소중한 개인정보이다 보니까 이렇게 노출되는 게 안 좋은데요! 그래서 저희는 env.default 파일을 하나 만들 겁니다!

- 터미널에서 vi.env.default를 입력해 주시고, 아래와 같이 입력합니다. 

 

🍎 FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD은 App Store Connect 혹은 TestFlight에 업로드하고 싶을 때 생성하는 겁니다!

  1. appleid.apple.com/account/manage
  2. new application specific password를 생성합니다.
  3. FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD에 그 일련번호를 입력해 주면 됩니다!

 

 

- 다시 Appfile로 돌아와 아래와 같이 설정해 주면 됩니다!

 

 

 

 

5️⃣ fastfile에 lane 설정을 해줍니다!

//✅ 기본 플랫폼을 ios 로 설정하여 fastlane [lane name] 으로 쉽게 실행가능.(원래 fastlane ios [lane name] 으로 실행해야 함)
default_platform(:ios)

platform :ios do
  desc "Push a new beta build to TestFlight"
  //✅ lane 이름은 upload로 정의한다.
  lane :upload do

    // ✅ Cert, Sigh 방식으로 인증할 때 사용하는 인증 함수다.
    // ✅ get_certificates로 인증서를, get_provisioning_profile로 프로필을 가져온다.
    get_certificates
    get_provisioning_profile

    // ✅ 자동으로 빌드 넘버를 증가
    increment_build_number(
        build_number: latest_testflight_build_number + 1
    )

    // ✅ 빌드할 프로젝트와 빌드 스키마 설정
    build_app( 
        project: "InAppPurchase.xcodeproj",
        scheme: "InAppPurchase",
    )
		
    // ✅ TestFlight 업로드
    upload_to_testflight(
        // ✅ 빌드를 업로드 후 처리 중인 상태를 기다리지 않겠다.
        skip_waiting_for_build_processing: true
        )
  end
end

 

 

6️⃣ 빌드 & 배포까지 성공!!

 

 

 

 

💣 Trouble Shooting

1. Export 오류 발생 - https://docs.fastlane.tools/actions/gym/#export-options 

build_app( 
    workspace: "InAppPurchase.xcodeproj", 
    scheme: "InAppPurchase", 
    export_method: "app-store", 
    export_options: { 
    	provisioningProfiles: { 
       // ✅ apple developer 에서 provisioning profile 의 이름을 입력해주어야 한다. 
       "com.example.bundleid" => "Provisioning Profile Name", 
       "com.example.bundleid2" => "Provisioning Profile Name 2" 
       } 
   } 
)

 

 

2. Unable to upload archive. Failed to get authorization for username 'email' and password. 오류 발생

- https://docs.fastlane.tools/getting-started/ios/authentication/

1. 앱스토어 커넥트 API Key 사용하기

2. Two-step or two-factor authentication 사용하기

3. 앱 암호 사용하기 - appleid.apple.com/account/manage

 

 

3. Unable to locate Xcode 오류 발생

  • Xcode Project > Setting > Location > Command Line Tools > 선택

 

 

4. Asset validation failed (90713) Missing Info.plist value. A value for the Info.plist key 'CFBundleIconName' is missing in the bundle 'com.yeojin.InAppP'.

  • 앱 아이콘을 추가함. → appicon

 

 

5. 수출 규정 관련 문서가 누락됨 경고 메시지

- info.plist > App Uses Non-Exempt Encryption > NO로 설정

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

참고. Fastlane 공식 문서

 

fastlane docs

fastlane fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. 🚀 It handles all tedious tasks, like generating screenshots, dealing with code signing, and releasing your application. You can start by creati

docs.fastlane.tools

728x90
반응형