【2018年版】iOSアプリ新規で作る時にやること③

これ年に10回くらいはやるんだけど、どうにかならんのかなぁ。シェルスクリプト書けばいけそうだけど、Xcodeについてくのだるいし、労力と比較して迷う。

Carthage / RxSwift / Compass / XCGLogger / Reachability / Alamofire / R.swift / SwiftLint / Generamba / VIPER / fastlaneあたりが技術キーワードでしょうか。箇条書きでいきまっせ。
ぼくはもうCarthage対応してないやつは使わないので、Cocoapodsは出てきません。


③CD編です。fastlaneが主。
CIは対応してません。
あとたぶん設定抜けてたけど、AppIconは設定しておきましょう。
↑に挙げたやつ残はRxSwift / Compass / XCGLogger / Reachability / Alamofire / VIPERとまぁ実装寄りだし、このシリーズは今回で終わりかなぁ。

①はこちら
devdevdev.hatenablog.com

②はこちら
devdevdev.hatenablog.com

とりあえず

Xcode開いて、TARGETS→General→Signingで「Automatically manage signing」を外しておきましょう。

fastlane設定など

順番通りにやってくればfastlaneが使えるようになっているはずなのでインストールは省略。

bundle exec fastlane init

しましょう。あとは質問に答えていくだけ。
とりあえず2. Automate beta distribution to TestFlightやったあとに、3.Automate App Store distributionをやりました。Schemaが2つあるので2つともね。ただ、StagingのApp Store distributionはいらないので計3回。

・・・・・・という風にはうまくいきません。実際にはやっていません。


そして、Generambaとfastlaneの相性がよくない。ってゆーかGenerambaが見てるxcodeprojが古すぎ。本家Generamba更新なさすぎ。つらたん。自作?いや、めんどい。Fork?いや、誰かやってるっしょってわけで以下の方を発見。Xcode9.4で動いてるっぽいことをぼくが確認しました。これでもxcodeproj 1.5.9だからぎり。。fastlaneはxcodeproj 1.5.7以上。。fastlaneをグローバルにいれればいける気がするけど、でもグローバルにはいれたくない。

github.com

とゆーわけでGemfileを更新。

source "https://rubygems.org"
  
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'generamba', github: "Bassov/Generamba", branch: "develop"
gem "fastlane", "2.99.0"

bundle updateとかbundle installとかしましょう。



んで、本題。

とりあえず証明書管理用のgitリポジトリを作りましょう。

で、devices.txtを作成。
バイス追加するときはこいつを更新しましょう。

Device ID	Device Name
A5B5CD50-14AB-5AF7-8B78-AB4751AB10A8	NAME1
A5B5CD50-14AB-5AF7-8B78-AB4751AB10A7	NAME2

fastlane/Appfileとfastlane/Fastfileを作成。
Fastfileは手抜き。ちゃんと書きましょう。

Appfile

itc_team_id("123456789") # iTunes Connect Team ID
team_id("ABCDEFGHIJ") # Developer Portal Team ID

Fastfile

default_platform(:ios)

platform :ios do
  desc "Get the development certifacate files to Local Machine"
  lane :get_development_cert do
    match(type: "development",
          readonly: true,
          git_url: "証明書管理用のgitリポジトリURL",
          app_identifier: ["本番用Bundle Identifier", "ステージング用Bundle Identifier"])
  end

  desc "Push a new Staging beta build to TestFlight"
  lane :staging_beta do
    beta(
      app_identifier: "ステージング用Bundle Identifier",
      scheme: "Hoge_Staging"
    )
  end

  desc "Push a new Production beta build to TestFlight"
  lane :production_beta do
    beta(
      app_identifier: "本番用Bundle Identifier",
      scheme: "Hoge_Production"
    )
  end

  private_lane :beta do |options|
    # テスト
    # run_tests(scheme: options[:scheme])

    # デバイス更新
    register_devices(devices_file: "./devices.txt")

    # 証明書更新
    match(type: "appstore",
          force_for_new_devices: true,
          git_url: "証明書管理用のgitリポジトリURL",
          app_identifier: ["本番用Bundle Identifier", "ステージング用Bundle Identifier"])

    # ビルド番号更新
    increment_build_number

    # ビルド
    build_app(scheme: options[:scheme])

    # Testflightにアップロード
    upload_to_testflight(skip_waiting_for_build_processing: true,
                         app_identifier: options[:app_identifier])
  end

  private_lane :refresh_development_cert do
    match(type: "development",
          force: true,
          git_url: "証明書管理用のgitリポジトリURL",
          app_identifier: ["本番用Bundle Identifier", "ステージング用Bundle Identifier"])
  end
end



refresh_development_cert

bundle exec fastlane refresh_development_cert

開発用の証明書の更新をしまっせ。
なんかあんま頻繁に更新されたくない気がしたので、private。使うときは書き換え。運用でカバー)

get_development_cert

bundle exec fastlane get_development_cert

一般エンジニアが使う用。
初めてrefresh_development_certした時にパスワード設定するので、それを共有しといてあげましょう。

staging_beta

bundle exec fastlane staging_beta

ステージング用のアプリをTestflightへ
バイスとProvisioning Profileの更新(必要なら)もやってます。

production_beta

bundle exec fastlane production_beta

本番用のアプリをTestflightへ
バイスとProvisioning Profileの更新(必要なら)もやってます。

余談

ちなみにぼくはrswiftがArchive対象に入っちゃってたらしく最初エラーになったぉ。
最近のXcodeってほんと親切だよね。
何回もApple IDいれんのめんどくさいよね。user_nameっていう設定がたぶんAppfileにできるよ。



最後に

Xcode開いて、TARGETS→General→Signingで選べるやつ選んでおきましょう。