notarytoolの出力は、既定では人が読むための文章である。スクリプトやCIで提出IDや状態を取り出す場合は、文章をgrepやawkで切り出すより、JSONやplistで出力するほうが扱いやすい。-f(--output-format)にjsonかplistを指定する。
$ xcrun notarytool submit [アプリ名].zip -p "[プロファイル名]" --wait -f json | jq -r .status
Accepted
ここでは、jsonとplistの出力、--progressとの関係、jqとplutilでの取り出し方を紹介する。実行例はXcode 27.0に含まれるnotarytool 1.1.3のもの。
出力形式を指定する
-fには、normal、json、plistを指定できる。既定値はnormalである。submit、info、wait、history、logで指定できる。
infoをjsonで出力すると、1行のJSONが出力される。
$ xcrun notarytool info [提出ID] -p "[プロファイル名]" -f json
{"status":"Accepted","message":"Successfully received submission info","id":"[提出ID]","name":"hello.zip","createdDate":"[提出した日時]"}
plistで出力すると、XML形式のplistが出力される。
$ xcrun notarytool info [提出ID] -p "[プロファイル名]" -f plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>createdDate</key>
<string>[提出した日時]</string>
<key>id</key>
<string>[提出ID]</string>
<key>message</key>
<string>Successfully received submission info</string>
<key>name</key>
<string>hello.zip</string>
<key>status</key>
<string>Accepted</string>
</dict>
</plist>
サブコマンドごとの出力に含まれるキー
出力に含まれるキーは、サブコマンドによって異なる。
| サブコマンド | キー |
|---|---|
submit(--waitなし) | message、path、id |
submit --wait | message、id、status |
info | status、message、id、name、createdDate |
wait | message、id、status |
history | history(配列)、message |
submitに--waitを付けない場合の出力には、statusが含まれない。提出直後の状態は、出力から判断できない。
$ xcrun notarytool submit hello.zip -p "[プロファイル名]" -f json
{"message":"Successfully uploaded file","path":"[hello.zipのパス]","id":"[提出ID]"}
historyのhistory配列の要素は、createdDate、id、name、statusの4つのキーを持つ。
参考: 【notarytool】過去の提出を一覧する (history)
--progressは通常の出力のときだけ有効になる
--progressは、状態の変化を表示する進捗表示である。--helpには、jsonとplistは--progressと互換性がなく、操作の最後に1回だけ出力すると書かれている。
waitで-f jsonと--progressを併用しても、エラーや警告は表示されない。進捗表示が抑制され、最後の結果だけが出力される。
$ xcrun notarytool wait [提出ID] -p "[プロファイル名]" -f json --progress
{"message":"Processing complete","id":"[提出ID]","status":"Accepted"}
通常の出力のときに進捗表示を止めたい場合は、--no-progressを指定する。
$ xcrun notarytool wait [提出ID] -p "[プロファイル名]" --no-progress
Processing complete
id: [提出ID]
status: Accepted
Current status:の進捗表示がなくなり、結果だけが表示される。
jqで値を取り出す
JSONで出力した結果から、jqでidやstatusを取り出せる。submit --waitの結果から、提出IDと状態を取り出す例を示す。
$ xcrun notarytool submit hello.zip -p "[プロファイル名]" --wait -f json | jq -r '.id, .status'
[提出ID]
Accepted
--waitなしでsubmitした場合は、提出IDだけを取り出せる。
$ ID=$(xcrun notarytool submit hello.zip -p "[プロファイル名]" -f json | jq -r .id)
$ echo $ID
[提出ID]
取り出した提出IDは、waitやinfo、logに渡せる。
参考: 【notarytool】提出と待機を分けて公証の完了を待つ (wait)
plutilで値を取り出す
plistで出力した結果は、macOS標準のplutil -extractで値を取り出せる。
$ xcrun notarytool info [提出ID] -p "[プロファイル名]" -f plist | plutil -extract status raw -
Accepted
plutilは、jqをインストールしていない環境でも使える。
CIで公証の成否を判定する
submitとwaitは、公証がInvalidになっても終了コード0で終了する。CIで公証の成否を判定する場合は、statusを確認する。
status=$(xcrun notarytool submit [アプリ名].zip -p "[プロファイル名]" --wait -f json | jq -r .status)
if [ "$status" != "Accepted" ]; then
echo "Notarization failed: $status" >&2
exit 1
fi
Invalidのほか、--timeoutで打ち切った場合も、statusがAcceptedにならない。打ち切った場合の出力にはstatusキーがなく、jqはnullを返す。どちらも判定を満たさないため、スクリプトは失敗として扱える。
$ xcrun notarytool wait [提出ID] -p "[プロファイル名]" -f json --timeout 1
{"id":"[提出ID]","message":"Timeout of 1 second(s) was reached before processing completed."}
$ echo $?
124
参考: 【notarytool】公証に失敗した理由をログから調べる
logは常にJSONで出力される
logは、公証のログをJSONで出力する。-f plistを指定しても、出力はJSONのままだった。
$ xcrun notarytool log [提出ID] -p "[プロファイル名]" -f plist
{
"logFormatVersion": 1,
"jobId": "[提出ID]",
"status": "Accepted",
"statusSummary": "Ready for distribution",
"statusCode": 0,
...
ログの最上位のキーは、archiveFilename、issues、jobId、logFormatVersion、sha256、status、statusCode、statusSummary、ticketContents、uploadDateである。ログの読み方は、公証に失敗した理由を調べる記事を参照。
