コンテンツへスキップ →

fastlaneでAppStoreConnect APIを使うときに気づいたこと

fastlaneでAppStoreConnect APIを使う時にエラーや気づいたことがありましたのでそのメモです。

手順については以下の記事を参考にしました。
https://zenn.dev/moga/articles/752b5f5a8fc001060d1a

この記事の目次(クリックでジャンプ)

環境

  • fastlane 2.205.0

.p8ファイルの書き方

.p8ファイルは以下のように書かれてると思います。

-----BEGIN PRIVATE KEY-----
hoge
fuga
piyo
-----END PRIVATE KEY-----

.envファイルでは以下のように書きます。

// 段落ごとに改行コードを加える。「\n」
ASC_KEY_CONTENT="-----BEGIN PRIVATE KEY-----\nhoge\nfuga\npiyo\n-----END PRIVATE KEY-----"

app_store_connect_api_keyの実装を見てみたら、「key_content.gsub」で改行をしてそうです。
https://github.com/fastlane/fastlane/blob/master/fastlane/lib/fastlane/actions/app_store_connect_api_key.rb#L24-L26

# New lines don't get read properly when coming from an ENV
# Replacing them literal version with a new line
key_content = key_content.gsub('\n', "\n") if key_content

また書き方のサンプルコードもありました。
https://github.com/fastlane/fastlane/blob/3344301a0d6ea9a69450432e2a87aa6cef7922c8/fastlane/lib/fastlane/actions/app_store_connect_api_key.rb#L121-L138

      def self.example_code
        [
          'app_store_connect_api_key(
            key_id: "D83848D23",
            issuer_id: "227b0bbf-ada8-458c-9d62-3d8022b7d07f",
            key_filepath: "D83848D23.p8"
          )',
          'app_store_connect_api_key(
            key_id: "D83848D23",
            issuer_id: "227b0bbf-ada8-458c-9d62-3d8022b7d07f",
            key_filepath: "D83848D23.p8",
            duration: 200,
            in_house: true
          )',
          'app_store_connect_api_key(
            key_id: "D83848D23",
            issuer_id: "227b0bbf-ada8-458c-9d62-3d8022b7d07f",
            key_content: "-----BEGIN EC PRIVATE KEY-----\nfewfawefawfe\n-----END EC PRIVATE KEY-----"
          )'
        ]
      end

.p8ファイルの指定方法

.p8ファイルを指定するには、「key_content」、「key_filepath」のどちらでも大丈夫そうです。

// key_contentの場合
    # api_keyの生成
    app_store_connect_api_key(
      key_id: ENV['ASC_KEY_ID'], # your key id
      issuer_id: ENV['ASC_ISSUER_ID'], # your issuer id
      key_content: ENV['ASC_KEY_CONTENT'], # your secret key body
    )

// key_filepathの場合
    # api_keyの生成
    app_store_connect_api_key(
      key_id: ENV['ASC_KEY_ID'], # your key id
      issuer_id: ENV['ASC_ISSUER_ID'], # your issuer id
      key_filepath: ENV['ASC_KEY_FILE_PATH'],
    )

// envファイル
ASC_KEY_FILE_PATH="./fastlane/AuthKey_xxx.p8"

対応しているActions/Tools

  • 記事内では「match」、「build_app」や「upload_to_app_store」を使っていましたが、「pilot」や「download_dsyms」にも対応しています。(2022年6月時点)
NameApple IDAPI Key
pilotYesYes
deliverYesYes
sighYesYes
certYesYes
matchYesYes
producePartialNo
pemYesNo
precheckYes (except for IAP)Yes (except for IAP)
download_dsymsYesYes
app_store_build_numberYesYes
https://docs.fastlane.tools/app-store-connect-api/

app_store_connect_api_keyの中身

pilotを使っている場合

pilotというのは

  • testflightにアップロードするアクション。
  • parameterで「api_key」を持っている。
  • pilotは「upload_to_testflight」のエイリアス。
  • 「upload_to_app_store」では、「precheck_include_in_app_purchases: false,」がパラメーターに必要だったが、pilotは必要なかったです。
  • https://docs.fastlane.tools/actions/pilot/

使い方

desc "bundle exec fastlane upload_appstore --env release"
lane :upload_appstore do

  # api_keyの生成
  app_store_connect_api_key(
    key_id: ENV['ASC_KEY_ID'], # your key id
    issuer_id: ENV['ASC_ISSUER_ID'], # your issuer id
    key_content: ENV['ASC_KEY_CONTENT'], # your secret key body
  )

  ...
  pilot(
    ...
  )
end

エラー内容

pilotを使う際にエラーが出ました。

[17:03:33]: -------------------
[17:03:33]: --- Step: pilot ---
[17:03:33]: -------------------
[17:03:33]: Creating authorization token for App Store Connect API
...
[17:03:35]: Authentication credentials are missing or invalid. - Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests <https://developer.apple.com/go/?id=api-generating-tokens>

+------+-------------------------+-------------+
|               fastlane summary               |
+------+-------------------------+-------------+
| Step | Action                  | Time (in s) |
+------+-------------------------+-------------+
| 1    | Verifying fastlane      | 0           |
|      | version                 |             |
| 2    | default_platform        | 0           |
| 3    | xcversion               | 0           |
| 4    | app_store_connect_api_  | 0           |
|      | key                     |             |
| 5    | cocoapods               | 5           |
| 6    | match                   | 3           |
| 7    | gym                     | 215         |
| 💥   | pilot                   | 1           |
+------+-------------------------+-------------+

[17:03:35]: fastlane finished with errors

Looking for related GitHub issues on fastlane/fastlane...

[!] The request could not be completed because:
	Authentication credentials are missing or invalid. - Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests <https://developer.apple.com/go/?id=api-generating-tokens>

試したこと

durationを設定しないのが問題?

「Authentication credentials are missing or invalid. - Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests https://developer.apple.com/go/?id=api-generating-tokens」

で調べてみたら以下の記事が出てきました。
issuesの中ではdurationの値を”500″や”1200″に設定したら直った方もいましたが、私の場合は当てはまらなかったです。

  app_store_connect_api_key(
    key_id: ENV['ASC_KEY_ID'], # your key id
    issuer_id: ENV['ASC_ISSUER_ID'], # your issuer id
    key_content: ENV['ASC_KEY_CONTENT'], # your secret key body
    duration: 1200,
  )

エラーの原因、解決法

key_idとissuer_idを間違えて入力していたのが原因でした・・・。
なので、それぞれ正しい値を入れたところ正常に動作をしました。

key_id: ENV['ASC_KEY_ID'], # your key id
issuer_id: ENV['ASC_ISSUER_ID'], # your issuer id

成功時のログ

[17:31:30]: -------------------
[17:31:30]: --- Step: pilot ---
[17:31:30]: -------------------
[17:31:30]: Creating authorization token for App Store Connect API
[17:31:31]: Ready to upload new build to TestFlight (App: xxx)...
[17:31:31]: Going to upload updated app to App Store Connect
[17:31:31]: This might take a few minutes. Please don't interrupt the script.
[17:33:14]: iTunes Transporter successfully finished its job
[17:33:14]: ---------------------------------------------------------------------
[17:33:14]: Successfully uploaded package to App Store Connect. It might take a few minutes until it's visible online.
[17:33:14]: ---------------------------------------------------------------------
[17:33:14]: Successfully uploaded the new binary to App Store Connect
[17:33:14]: `skip_waiting_for_build_processing` used and no `changelog` supplied - skipping waiting for build processing

「This might take a few minutes. Please don’t interrupt the script.」というメッセージに到達しない限りは「duration」は関係なさそうです。

download_dsymsを使っている場合

使い方

private_lane :refresh_dsyms_with_version do |options|  
  
  # api_keyの生成
  app_store_connect_api_key(
    key_id: ENV['ASC_KEY_ID'], # your key id
    issuer_id: ENV['ASC_ISSUER_ID'], # your issuer id
    key_content: ENV['ASC_KEY_CONTENT'], # your secret key body
  )
  
	...
  download_dsyms(
     ...
  )
end

今までは、envファイルを指定していなかったが、app_store_connect_api_keyを追加したので、以下のように書く。

// before
bundle exec fastlane refresh_dsyms version:xxx

// after
$ bundle exec fastlane refresh_dsyms version:xxx --env release

envファイルを指定していないとコンソール上で確認される

[17:42:52]: ---------------------------------------
[17:42:52]: --- Step: app_store_connect_api_key ---
[17:42:52]: ---------------------------------------
[17:42:52]: To not be asked about this value, you can specify it using 'key_id'
[17:42:52]: The key ID: hoge
[17:43:58]: To not be asked about this value, you can specify it using 'issuer_id'
[17:43:58]: The issuer ID: fuga

download_dsymsの成功ログ

[17:46:29]: ---------------------------------------
[17:46:29]: --- Step: app_store_connect_api_key ---
[17:46:29]: ---------------------------------------
[17:46:29]: ----------------------------
[17:46:29]: --- Step: download_dsyms ---
[17:46:29]: ----------------------------
[17:46:29]: Creating authorization token for App Store Connect API
[17:46:30]: Looking for dSYM files for 'com.xxx.xxx' on platform IOS xxx
[17:46:31]: 🔑  Successfully downloaded dSYM file for xxx - 1 to 'com.xxx.dSYM.zip'
[17:46:33]: ----------------

app_store_connect_api_keyが呼ばれてない?

ログでは「Step: app_store_connect_api_key」の後に何も出力されていないため、機能しているかよくわからないです。
後々分かったことですが、このログの表示は正常でした。

[16:27:54]: -----------------------
[16:27:54]: --- Step: xcversion ---
[16:27:54]: -----------------------
[16:27:54]: Setting Xcode version to /Applications/Xcode.app for all build steps
[16:27:54]: ---------------------------------------
[16:27:54]: --- Step: app_store_connect_api_key ---
[16:27:54]: ---------------------------------------
[16:27:54]: -----------------------
[16:27:54]: --- Step: cocoapods ---
[16:27:54]: -----------------------
[16:27:54]: $ bundle exec pod install

例えば以下のようにkey_filepathにpath以外の文字列を書くと、「Step: app_store_connect_api_key」の時点でエラーが出ます。

  # api_keyの生成
  app_store_connect_api_key(
    key_id: ENV['ASC_KEY_ID'], # your key id
    issuer_id: ENV['ASC_ISSUER_ID'], # your issuer id
    key_filepath: ENV['ASC_KEY_FILE_PATH'], # your secret key body //
  )

====
// .env
ASC_KEY_FILE_PATH="hogehoge"

失敗時のログ

“hoge”と言うpathに.p8ファイルが見つかりませんというログが出ています。

[09:11:31]: Error setting value 'hoge' for option 'key_filepath'
[09:11:31]: You passed invalid parameters to 'app_store_connect_api_key'.
[09:11:31]: Check out the error below and available options by running `fastlane action app_store_connect_api_key`
...
[09:11:31]: Couldn't find key p8 file at path 'hoge'

カテゴリー: iOS