3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sfdx force:source:deployの出力JSONで成否を判定して終了コードをちゃんと設定する

Posted at

ネットワークエラーが出てもexit statusが0...?

Travis(Ubuntu 16.04.6 LTS)でsfdx force:source:deployコマンドを使ってSandboxでテストを実行しているのですが、更新かけたばかりのSandboxだったからかgetaddrinfo ENOTFOUNDが出て失敗していました。
(ホスト名だけ変えてあります)

$ sfdx force:source:deploy -u sandbox -m ApexClass,ApexTrigger --testlevel RunLocalTests --checkonly --verbose --json -w 9
Unhandled rejection Error: getaddrinfo ENOTFOUND hogehoge-sandbox.cs114.my.salesforce.com hogehoge-sandbox.cs114.my.salesforce.com:443
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
The command "sfdx force:source:deploy -u sandbox -m ApexClass,ApexTrigger --testlevel RunLocalTests --checkonly --verbose --json -w 9" exited with 0.

が、なぜか**exited with 0を返してくるのでCI的には成功してしまうという...**

結果のJSONでちゃんと判定する

--jsonを指定していると、結果をJSONで標準出力に返してくれるので、それでちゃんと判定してみます。

jq --exit-status で結果のJSON内容で終了コードを変える

デプロイコマンドの後に jq --exit-status .result.success でパイプしてjqコマンドで{"result":{...(略)..., "success":false}}が返ってきたときに失敗させます。

エラー出力に非JSONのエラーメッセージだけ出た場合はjqがパースに失敗して非0な終了コードにさせるために2>&1します。

sfdx (略) 2>&1 | jq --exit-status .result.success

参考: http://hoppie.hatenablog.com/entry/2016/07/29/142935

失敗した時は結果のJSONをログとして出力したい

パイプでjqコマンドに流してしまうと失敗したときもJSONが見れなくなってしまうのでteeを使って一時的にファイルにもJSONを吐き出しておいて、失敗の場合だけcatで表示させます。

SUCCESS=$(sfdx (略) 2>&1 | tee result.json | jq --exit-status .result.success)
if [ "$SUCCESS" != "true" ]; then cat result.json; fi

最終的な.travis.ymlの抜粋

  - SUCCESS=$(sfdx force:source:deploy -u sandbox -m ApexClass,ApexTrigger --testlevel RunLocalTests --checkonly --verbose --json -w 9 2&>1 | tee result.json | jq --exit-status .result.success)
  - if [ "$SUCCESS" != "true" ]; then cat result.json; fi
3
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?